Web Hosting Talk







View Full Version : java two-way chat?


palmer24
06-05-2004, 03:03 PM
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!

DanPhx
06-07-2004, 01:43 AM
Do you really want you server sending before it receives?

When you test are person A and person B on different computers? or just different windows of the same computer?

ilyash
06-07-2004, 05:30 PM
your loop is poorly constructed

while(true)
{

send(connectionSocket, outToServer, inFromUser, inFromServer);

receive(connectionSocket, outToServer, inFromUser, inFromServer);



}



Thats why it is waiting for them to send ...
try:

if (inFromServer.readLine())!=null){
send(connectionSocket, outToServer, inFromUser, inFromServer);
}
if (outGoingMessage=inFromUser.readLine())!=null){
receive(connectionSocket, outToServer, inFromUser, inFromServer);
}


And then takes those loops out of the servers send and receive methods sine you already checked..