Java socket programming problem?

The program is using a BufferedWriter but this is not flush()ed (it will sit in the buffer until it is flushed, the buffer fills or closed) and there is no newline sent (the readLine will wait until you send a newline) try bw. Write("I am the Client! \n"); bw.flush().

You are right, I'm so used to PrintWriter with autoflush I didn't even see it. – Ronnis Jan 3 at 22:32 tanx a lot! But I have to remove "while" from read section to get it work – mk.

Persia Jan 3 at 22:48.

Both threads hang on while((line =br.readLine())! = null).

Yes, I know that, there is no changes if I change "while" to "if" or even do the printing unconditional. – mk. Persia Jan 3 at 22:19.

Now it works package sockettest; import java.io. *; import java.net. *; class sevr implements Runnable{ public void run() { ServerSocket sSkt = null; Socket skt = null; BufferedReader br = null; BufferedWriter bw = null; try{ System.out.

Println("Server: is about to create socket"); sSkt = new ServerSocket(6666); System.out. Println("Server: socket created"); } catch(IOException e){ System.out. Println("Server: socket creation failure"); } try{ System.out.

Println("Server: is listening"); skt = sSkt.accept(); System.out. Println("Server: Connection Established"); } catch(IOException e){ System.out. Println("Server: listening failed"); } try{ System.out.

Println("Server: creating streams"); br = new BufferedReader(new InputStreamReader(skt.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); System.out. Println("Server: stream done"); } catch(IOException e){ System.out. Println("Server: stream failed"); } System.out.

Println("Server: reading the request"); try{ String line = null; line = br.readLine(); System.out. Println("Server: client said-> "+ line); } catch(IOException e){ System.out. Println("Server: reading failed"); } System.out.

Println("Server: reading fished"); System.out. Println("Server: responding"); try{ bw. Write("I am server!

\n"); bw.flush(); } catch(IOException e){ System.out. Println("Server: responding failed"); } System.out. Println("Server: responding finished"); System.out.

Println("Server: is finishing"); try { br.close(); bw.close(); skt.close(); sSkt.close(); } catch (IOException e) { System.out. Println("Server: finishing failed"); } System.out. Println("Server: done"); } } class clnt implements Runnable{ public void run() { Socket skt = null; BufferedReader br = null; BufferedWriter bw = null; try{ System.out.

Println("Client: about to create socket"); skt = new Socket(InetAddress.getLocalHost(),6666); System.out. Println("Client: socket created"); } catch(IOException e){ System.out. Println("Client: socket creation failure"); } try{ System.out.

Println("Client: creating streams"); br = new BufferedReader(new InputStreamReader(skt.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); System.out. Println("Client: stream done"); } catch(IOException e){ System.out. Println("Client: stream failed"); } System.out.

Println("Client: requesting"); try{ bw. Write("I am Client! \n"); bw.flush(); } catch(IOException e){ System.out.

Println("Client: requesting failed"); } System.out. Println("Client: requesting finished"); System.out. Println("Client: reading the respond"); try{ String line = null; line =br.readLine(); System.out.

Println("Client: server said-> "+ line); } catch(IOException e){ System.out. Println("Client: reading failed"); } System.out. Println("Client: reading fished"); System.out.

Println("Client: is finishing"); try { br.close(); bw.close(); skt.close(); } catch (IOException e) { System.out. Println("Client: finishing failed"); } System.out. Println("Client: done"); } } public class Main { public static void main(String args) { System.out.

Println("Main started"); Thread sThread = new Thread(new sevr()); Thread cThread = new Thread(new clnt()); sThread.start(); cThread.start(); try { sThread.join(); cThread.join(); } catch (InterruptedException ex) { System.out. Println("joining failed"); } System.out. Println("Main done"); } } output: Main started Server: is about to create socket Client: about to create socket Client: socket created Client: creating streams Server: socket created Server: is listening Server: Connection Established Server: creating streams Client: stream done Server: stream done Server: reading the request Client: requesting Client: requesting finished Client: reading the respond Server: client said-> I am Client!

Server: reading fished Server: responding Server: responding finished Server: is finishing Client: server said-> I am server! Client: reading fished Client: is finishing Client: done Server: done Main done.

Don't post your solution to your own question as an answer. Edit the question and pick the answer that helped you most, so that's it closed now. – Chochos Jan 3 at 23:56.

You should note that a socket can go into more than one list. Call is blocking, but you can give it a timeout. Reason to do otherwise.

The program is using a BufferedWriter but this is not flush()ed (it will sit in the buffer until it is flushed, the buffer fills or closed) and there is no newline sent (the readLine will wait until you send a newline).

Now it works package sockettest; import java.io. *; import java.net. *; class sevr implements Runnable{ public void run() { ServerSocket sSkt = null; Socket skt = null; BufferedReader br = null; BufferedWriter bw = null; try{ System.out.

Println("Server: is about to create socket"); sSkt = new ServerSocket(6666); System.out. Println("Server: socket created"); } catch(IOException e){ System.out. Println("Server: socket creation failure"); } try{ System.out.

Println("Server: is listening"); skt = sSkt.accept(); System.out. Println("Server: Connection Established"); } catch(IOException e){ System.out. Println("Server: listening failed"); } try{ System.out.

Println("Server: creating streams"); br = new BufferedReader(new InputStreamReader(skt.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); System.out. Println("Server: stream done"); } catch(IOException e){ System.out. Println("Server: stream failed"); } System.out.

Println("Server: reading the request"); try{ String line = null; line = br.readLine(); System.out. Println("Server: client said-> "+ line); } catch(IOException e){ System.out. Println("Server: reading failed"); } System.out.

Println("Server: reading fished"); System.out. Println("Server: responding"); try{ bw. Write(" I am server!

\n"); bw.flush(); } catch(IOException e){ System.out. Println("Server: responding failed"); } System.out. Println("Server: responding finished"); System.out.

Println("Server: is finishing"); try { br.close(); bw.close(); skt.close(); sSkt.close(); } catch (IOException e) { System.out. Println("Server: finishing failed"); } System.out. Println("Server: done"); } } class clnt implements Runnable{ public void run() { Socket skt = null; BufferedReader br = null; BufferedWriter bw = null; try{ System.out.

Println("Client: about to create socket"); skt = new Socket(InetAddress.getLocalHost(),6666); System.out. Println("Client: socket created"); } catch(IOException e){ System.out. Println("Client: socket creation failure"); } try{ System.out.

Println("Client: creating streams"); br = new BufferedReader(new InputStreamReader(skt.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); System.out. Println("Client: stream done"); } catch(IOException e){ System.out. Println("Client: stream failed"); } System.out.

Println("Client: requesting"); try{ bw. Write(" I am Client! \n"); bw.flush(); } catch(IOException e){ System.out.

Println("Client: requesting failed"); } System.out. Println("Client: requesting finished"); System.out. Println("Client: reading the respond"); try{ String line = null; line =br.readLine(); System.out.

Println("Client: server said-> "+ line); } catch(IOException e){ System.out. Println("Client: reading failed"); } System.out. Println("Client: reading fished"); System.out.

Println("Client: is finishing"); try { br.close(); bw.close(); skt.close(); } catch (IOException e) { System.out. Println("Client: finishing failed"); } System.out. Println("Client: done"); } } public class Main { public static void main(String args) { System.out.

Println("Main started"); Thread sThread = new Thread(new sevr()); Thread cThread = new Thread(new clnt()); sThread.start(); cThread.start(); try { sThread.join(); cThread.join(); } catch (InterruptedException ex) { System.out. Println("joining failed"); } System.out. Println("Main done"); } } output: Main started Server: is about to create socket Client: about to create socket Client: socket created Client: creating streams Server: socket created Server: is listening Server: Connection Established Server: creating streams Client: stream done Server: stream done Server: reading the request Client: requesting Client: requesting finished Client: reading the respond Server: client said-> I am Client!

Server: reading fished Server: responding Server: responding finished Server: is finishing Client: server said-> I am server! Client: reading fished Client: is finishing Client: done Server: done Main done.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions