I am attempting to create a very simple Socket/ServerSocket program in Java. My two classes are listed below, both of which run on the same machine. The problem is, the connection seems to be established just fine, but when I attempt to write over the socket from the client to the server, nothing seems to happen. The server never returns from in.readLine(), and the program comes to a halt. I cannot figure out why this is happening.
Here are my classes:
TheServer.java:
public class TheServer {
public static void main(String[] args) {
BufferedReader in = null;
PrintWriter out = null;
try {
ServerSocket serv = new ServerSocket(2255);
Socket sock = serv.accept();
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Connection established by Server");
try {
String line;
while ((line = in.readLine()) != "Bye") {
System.out.println(line + "Received");
out.println(line.toUpperCase());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
TheClient.java:
public class theClient {
public static void main(String[] args) {
BufferedReader in = null;
PrintWriter out = null;
try {
Socket sock = new Socket("localhost", 2255);
in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Connection established by Client");
try {
while (true) {
BufferedReader read = new BufferedReader(new InputStreamReader(
System.in));
String line = read.readLine();
out.println(line.trim());
System.out.println(in.readLine().trim());
if (line == "Bye")
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
println()appends an EOL to every line it sends