File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sockets" Watch "Sockets" New topic
Author

Sockets

Vim Win
Greenhorn

Joined: May 01, 2004
Posts: 21
I am having the following probem with Socket Programming :
I am writing a application (resembles to a chat application). In this I have a Server and a client. The Server has to talk to the client and the client has to talk to the server. I have one Serversocket (with one socket on its end) and another socket at the client end. I have write messages from the client to the server. Simulataneously I have listen to what server sends. I have only one socket and dont want to create another. Can I do this by anymeans ?
JVRN.
------------------
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
I am not sure what you are asking. But server and client can communicate with each other. If you created say,
BufferedReader serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter serverOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
And,
BufferedReader clientIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter clientOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
You can have server and client communicate with one another with,
serverIn.readLine();
serverOut.println();
And,
clientIn.readLine();
clientout.println();
Thanks.
Vim Win
Greenhorn

Joined: May 01, 2004
Posts: 21
Hi ricky,
Thank you for your response. I have done the things that you have quoted. I have created BufferedReader and PrintWriter for the purpose. My problem is somewhat different. Consider a chat application between server and client. One person is sitting at the server end and other at the client end. Both persons should be receiving messages from the other irrespective of whether he/she responds to the message or not. In the condition that I have done (the same which you have quoted), I would send a message from client to the server. The server would read the message and print it in its end. The client know has two options. Either keep getting input from client-user and sending messages or waiting for response from the server. Both have to be done simultaneously (since the communication is two-way). So If iam waiting for a communication from server in the client end, I cannot receive input at this end.
[ Consider that I am having a GUI environment for the user at both server and client ends ]
I came to this solution : I created a thread which keeps on getting input at the client end and sending it to the server and another thread which keeps on listening to the communication from the server. I have to switch between the two threads on a time slice to print the messages. I have the problem of scheduling of threads.
I hope I have explained the problem. Is there any other solution to the above problem ???
Kindly explain.
Thanks
JVRN.
Originally posted by ricky gonzalez:
I am not sure what you are asking. But server and client can communicate with each other. If you created say,
BufferedReader serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter serverOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
And,
BufferedReader clientIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter clientOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
You can have server and client communicate with one another with,
serverIn.readLine();
serverOut.println();
And,
clientIn.readLine();
clientout.println();
Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sockets