写一个C/S的socket app,实现通信和一些自定义的基本命令, 以下代码可以正常编译,但是无论是server还是client,命令行下一运行就出错,显示
package ClientServer;
//client
import java.io.*;
import java.net.*;
public class myClient {
public static void main(String[] args) throws IOException {
String servername = args[0];
int port = Integer.parseInt(args[1]);
Socket clientsocket = null;
PrintStream out = null;
BufferedReader in = null;
boolean connected = false;
boolean isEcho = false;
try {
clientsocket = new Socket(servername, port);
connected = true;
out = new PrintStream(clientsocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
}catch (IOException e) {
System.err.println("Error:Cannot connect to server!");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader (new InputStreamReader(System.in));
String userInput;
//out.print("client");
while (connected) {
System.out.println("Session has been established.");
System.out.print("operation>");
userInput = stdIn.readLine();
if (userInput == "whoAMi") {
out.println(userInput);
System.out.println(in.readLine());
} else if (userInput == "bye") {
out.println(userInput);
System.out.println(in.readLine());
connected = false;
} else if (userInput == "startEcho") {
isEcho = true;
out.println(userInput);
System.out.println(in.readLine());
String echoInput;
while (isEcho) {
System.out.print("echo>");
echoInput = stdIn.readLine();
if (echoInput == "stopEcho") {
isEcho = false;
}
out.println(echoInput);
System.out.println(in.readLine());
}
}
}
System.out.println("Session is terminated.");
out.close();
in.close();
stdIn.close();
clientsocket.close();
}
}
package ClientServer;
//server
import java.io.*;
import java.net.*;
public class myServer{
ServerSocket serversocket;
String inputLine, outputLine;
String message;
public myServer(int portNum){}
void run()
{
try{
FileWriter fstream = new FileWriter("Log.txt", true);
PrintWriter log = new PrintWriter(fstream);
System.out.println("Waiting for client on port " + serversocket.getLocalPort());
Socket clientsocket = serversocket.accept();
System.out.println("Connected to client on " + clientsocket.getRemoteSocketAddress());
log.println("\r\n************SocketLog**************");
log.println("Connected to client on " + clientsocket.getRemoteSocketAddress());
PrintStream out = new PrintStream(clientsocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));