my dog learned polymorphism*
The moose likes Sockets and Internet Protocols and the fly likes Two way chat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Two way chat?" Watch "Two way chat?" New topic
Author

Two way chat?

Colonel Mustard
Greenhorn

Joined: Jun 05, 2004
Posts: 1
Hi, I'm relatively new to Java, so I'm not entirely sure what I'm doing. What I am TRYING to do is use sockets to make essentially a two-way chat where each person can send and receive messages at the same time. Below is the code which has gotten me the closest, but what it does is it waits for the person to send the message before receiving any new ones.

For example:

Person A types "hello" and presses enter.

Person B would see nothing.

Person B types "hi" and presses enter.

NOW Person B sees A's "hello" AND A sees B's "hi."

I guess what must be happening is it's getting stalled on sending a message instead of executing the receiving command at the same time. Any ideas on how to fix this would be greatly appreciated.
--------------------------------------------------------------------------
Here is the client side:
---------------------------------------------------------------------------
import java.net.*;
import java.io.*;

public class myClient


{

public static void main(String[] args) throws Exception
{
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
System.out.print("IP: ");
String server=inFromUser.readLine();
Socket socket=new Socket(server,9999);
DataOutputStream outToServer=new DataOutputStream(socket.getOutputStream());
BufferedReader inFromServer=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true)
{


receive(socket, outToServer, inFromUser, inFromServer);



send(socket, outToServer, inFromUser, inFromServer);

}
}

public static void send(Socket socket, DataOutputStream outToServer, BufferedReader inFromUser, BufferedReader inFromServer) throws Exception
{
System.out.print("You: ");
String outGoingMessage;
if((outGoingMessage=inFromUser.readLine())!=null)
{
outToServer.writeBytes(outGoingMessage+'\n'+'\n');
}
}

public static void receive(Socket socket, DataOutputStream outToServer, BufferedReader inFromUser, BufferedReader inFromServer) throws Exception
{
String incomingMessage;
if((incomingMessage=inFromServer.readLine())!=null)
{
System.out.println("Server: "+incomingMessage);
}
}
}
------------------------------------------------------------------------------------
And the server:
------------------------------------------------------------------------------------
import java.net.*;
import java.io.*;

public class myServer
{
public static void main(String[] args) throws Exception
{
String line;
BufferedReader in;
PrintStream out;
InetAddress ip=InetAddress.getLocalHost();
System.out.println("Your IP is: "+ip.getHostAddress());
ServerSocket welcomeSocket=new ServerSocket(9999);
Socket connectionSocket=welcomeSocket.accept();
DataOutputStream outToServer=new DataOutputStream(connectionSocket.getOutputStream());
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
BufferedReader inFromServer=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
while(true)
{

send(connectionSocket, outToServer, inFromUser, inFromServer);

receive(connectionSocket, outToServer, inFromUser, inFromServer);



}
}

public static void send(Socket socket, DataOutputStream outToServer, BufferedReader inFromUser, BufferedReader inFromServer) throws Exception
{
String outGoingMessage;
System.out.print("You: ");
if((outGoingMessage=inFromUser.readLine())!=null)
{
outToServer.writeBytes(outGoingMessage+'\n'+'\n');
}
}

public static void receive(Socket socket, DataOutputStream outToServer, BufferedReader inFromUser, BufferedReader inFromServer) throws Exception
{
String incomingMessage;
if((incomingMessage=inFromServer.readLine())!=null)
{
System.out.println("Client: "+incomingMessage);
}
}
}

-------------------------------------------------------------
Thanks so much for any help!
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
When you call inFromUser.readLine(), it's not going to return until the user has typed in a line. That means that once you call send(), receive() cannot be executed until the client has pressed return. You can get around this to some degree using Threads, but the standard Java libraries don't give you enough control over the command line to do that cleanly (what happens if someone is in the middle of typing a line an a message arrives? In Java you'd have to cut the partially-typed message in half or wait until it's done).

I think you'd find it easier to implement your chat clients with a simple GUI that seperates the text-entry box from the text-display area. You also won't have to worry about starting a seperate thread. In case you're not familiar with Swing, I've written the setup code for an extremely simple GUI that you might want to use (just add the networking part):


[ June 05, 2004: Message edited by: David Weitzman ]
 
 
subject: Two way chat?