0

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

3
  • I don't know. Is it just me that I think a bit of code samples and more effort on your question are in order? Also, is this a homework? Commented Oct 14, 2010 at 17:32
  • You will need to post some of your code for people to help? are you getting exceptions, does the code compile? Commented Oct 14, 2010 at 17:33
  • Rothan, please edit your question to include the code requested by those answering rather than put it in the comments. It makes it easier for others to read. Thanks. Commented Oct 14, 2010 at 18:30

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

firstly for sending from client to serevr on client side Socket clientSocket = new Socket("127.0.0.1", portNo); OutputStream os = clientSocket.getOutputStream();
and writing to this outputstream os
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
The client will also need to read after it is done writing. clientSocket.getInputStream() and have at it!
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!
1

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?

5 Comments

i am not getting any exception.
and how should i get the address of the client machine?
Socket clientSocket = new Socket("127.0.0.1", portNo); OutputStream os = clientSocket.getOutputStream();
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
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
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.