Socket Programming - Client / Server

In socket programming, I have a client which sends a message to server, the server receives the message and returns back a acknowledge message the client has to receive it. When I tried I my client socket is getting closed after sending the message to the server. Its not getting the acknowledge ment from server. What could be the problem ? Can anyone help me ?

The source codes for Server and Client is bellow,

import java.io.*;

import java.net.*;

class Server extends Thread

{

ServerSocket ss = null;

Socket so;

public void run()

{

try

{

System.out.println("Server ready waiting for client message.... ");

ss = new ServerSocket(4567);

while(true){

so = ss.accept();

BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream(),"8859_1"));

String s = br.readLine();

while(s != null){

System.out.println(s);

s=br.readLine();

}

br.close();

System.out.println("Server Preparing ack......");

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(so.getOutputStream(),"8859_1"));

bw.write("Ack : Got the file succesfully .....");

bw.flush();

bw.close();

}

}

catch(Exception ee)

{

}

}

public static void main(String args[])

{

Server p = new Server();

p.start();

}

}

--

import java.io.*;

import java.net.*;

class Client

{

public static void main(String args[]) {

try {

Socket so = new Socket("",4567);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(so.getOutputStream(),"8859_1"));

String msg1 = "This is a sample message send to server from client......after reading this message the client will acknowledge .....";

bw.write(msg1,0,msg1.length());

bw.flush();

System.out.println("Message send to server .......");

bw.close();

BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream(),"8859_1"));

String msg = br.readLine();

while(msg != null) {

System.out.println(msg);

msg = br.readLine();

}

}

catch(Exception e) {

e.printStackTrace();

}

}

}

-

Thanks,

Regards

Jayakumar, Chennai

email : jjkumar_73@rediffmail.com

[2421 byte] By [JJKUMAR_73] at [2007-9-27 4:13:28]
# 1

Jayakumar,

While I haven't tried your code, it looks like the problem is with the client. You are closing bw in the middle of your code, and never even closing br.

Here is my suggestion, keep both the br and bw references open until the end of your function, then close them both. The order should not matter. I think this will make your client work as expected.

Good Luck,

John

cajo at 2007-7-5 3:37:42 > top of Java-index,Archived Forums,Socket Programming...
# 2
I have come across the same type of problem, a server closes the connection before sending anything back to the client or vise-versa. It would appear that the output streams are being closed before the ack is sent.
watertownjordan at 2007-7-5 3:37:42 > top of Java-index,Archived Forums,Socket Programming...
# 3
cajo is right. Closing either stream closes the whole socket. Keep 'em open.
williamseverin at 2007-7-5 3:37:42 > top of Java-index,Archived Forums,Socket Programming...
# 4
simply closing an output/input -stream will close a socket. How? The Socket is independent of any streams attached to it (external to the Socket class). I close output streams on sockets and it does not stop me from reading from the socket. Thats weird.
watertownjordan at 2007-7-5 3:37:42 > top of Java-index,Archived Forums,Socket Programming...