I have a simple socket
/serversocket
example that I'm trying to get running, but both the client and the server hang when their BufferedReader
s try to read. Here is the code for each:
SERVER
package picturePerfect;
--imports--
public class PictureServer implements Runnable{
static ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
serverSocket = new ServerSocket(2342);
Thread firstSessionThread = new Thread(new PictureServer());
firstSessionThread.start();
}
@Override
public void run() {
Socket socket = null;
try {
socket = serverSocket.accept();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String clientRequest = bufferedReader.readLine();
System.out.println(clientRequest);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println("Sent from server!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
CLIENT
package picturePerfect;
--imports--
public class PictureClient {
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
Socket socket = new Socket("localhost", 2342);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.write("Sent from client!");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = bufferedReader.readLine();
System.out.println(response);
socket.close();
}
}
This is the barest I could simplify my code to. I have a sample program that I've been following, which seems nearly exactly the same. This is the sample server and client (that does work):
SAMPLE SERVER
--imports--
public class Server implements Runnable{
static ServerSocket ss;
public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}
@Override
public void run() {
while ( true ) {
Socket s = null;
try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}
SAMPLE CLIENT
--imports--
public class Server implements Runnable{
static ServerSocket ss;
public static void main(String[] args) throws IOException {
ss = new ServerSocket(3142);
Thread thread = new Thread(new Server());
thread.start();
}
@Override
public void run() {
while ( true ) {
Socket s = null;
try {
s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String operands = br.readLine();
System.out.println(operands + " was received");
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(operands + " right back!");
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}
I've tried putting the while loop into my server and moving my client and server to the default package, but neither helped. I also tried using read()
instead of readLine()
, and ending the printWriter's lines with \r\n
, but was just as unsuccessful there.
Why does my code hang on readLine()
, especially when the sample code doesn't?