tags:

views:

85

answers:

3

i have a txt file with students name and marks for subjects. i send this file from client to server using

Socket clientSocket = new Socket("127.0.0.1",5432);            
OutputStream os = clientSocket.getOutputStream();            
os.write(clientWriteArr,0,clientWriteArr.length);

and read this file at server using

ServerSocket sock = new ServerSocket(5432);
Socket serverSocket = sock.accept();
InputStream is = serverSocket.getInputStream();
is.read(serverReadArr,0,serverReadArr.length);

i am modifying the file contents upto this all is working fine. after this i want to send back this file back to client but i am not getting file at the client and also not getting any exception

+1  A: 

You need the the "server" to open a socket connection back to the "client" to send data back. The "client" has to be listening on the port that the "server" wants to connect to.

"Client" and "server" have dual roles in this case.

What exception do you get?

duffymo
i am not getting any exception.
Rohan Thakare
and how should i get the address of the client machine?
Rohan Thakare
Socket clientSocket = new Socket("127.0.0.1", portNo); OutputStream os = clientSocket.getOutputStream();
Rohan Thakare
os.write(clientWriteArr,0,clientWriteArr.length); this is on client side and ServerSocket sock = new ServerSocket(5432);Socket serverSocket = sock.accept();InputStream is = serverSocket.getInputStream();is.read(serverReadArr,0,serverReadArr.length); this is on server side
Rohan Thakare
using this i am able to do one way communication but using the same connection i am unable to transfer the file back to client
Rohan Thakare
+2  A: 

You can leave the original socket open from which you read the file, and then write the result to the same socket before closing it. This would be a standard request/response model like what is used for HTTP, and is convenient because the server does not need to know how to connect back to the client. Give us some code for more detailed advice.

Peter DeWeese
firstly for sending from client to serevr on client sideSocket clientSocket = new Socket("127.0.0.1", portNo); OutputStream os = clientSocket.getOutputStream();
Rohan Thakare
and writing to this outputstream os
Rohan Thakare
and reading on server side for reading ServerSocket sock = new ServerSocket(5432); Socket serverSocket = sock.accept(); InputStream is=serverSocket.getInputStream(); for reading now without closing the socket i am modifying the file trying to send back the file but i am not getting it at the client and also not getting any exception
Rohan Thakare
The client will also need to read after it is done writing. clientSocket.getInputStream() and have at it!
Peter DeWeese
It sounds like it is waiting for input, and you could only really tell that for sure with a debugger. Did you flush the OutputStream on the other side? If you could edit your question to include more of the code, it might help!
Peter DeWeese
A: 

Your server side code should be like:

ServerSocket serverSocket = new ServerSocket(8999);
Socket socket = serverSocket.accept();

DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Here, in : you can read the data sent by client. out: you can write data to client

Your client code should be like:

Socket socket = new Socket("localhost", 8999);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Here, in you can send data to server. out, you can read the data sent by server.

Reading data from input stream:

while (true) {
    int c = in.read();
}

when you call in.read(), it will block current thread until it reads something.

Writing data to output stream:

out.write(data);
feridcelik