简体   繁体   中英

Java - establish a chat between two clients

Im trying to code a simple server that creates a chat between any two clients that connect to the server. (for any two new clients the server will open a new chat thred/s and then will wait to the next two clients)

i have tried the followןng solution:

Server:

public class Server 
{
    public Server() throws IOException
    {
        ServerSocket sc = new ServerSocket(7777);
        Socket s1, s2;
        
        while(true)
        {
            s1 = sc.accept();
            s2 = sc.accept();
            
            new ServerThread(s1, s2).start();
            new ServerThread(s2, s1).start();
        }
    }
}

Server threads (two threads as explained in the comment above the class)

 /*
  Receives message from sender socket and pass them to the recipient socket
 */
public class ServerThread extends Thread 
{
    Socket sender;
    Socket recipient;
    
    public ServerThread(Socket sender, Socket recipient) 
    {
        this.sender = sender;
        this.recipient = recipient;
    }
    

    @Override
    public void run()
    {
        try {
            handle();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    public void handle() throws IOException 
    {
         String msg;
         // Create output stream for the recipient
         OutputStream outputStream = recipient.getOutputStream();
         ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStream);
         
         // Create input stream for the recipient
         InputStream inputStream = sender.getInputStream();   
         ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
         
         while(true) //sender.isConnected())
         {
             msg = objectInputStream.readUTF();
             
             if(true) //recipient.isConnected())
                 objOutputStream.writeUTF(msg);
             else
                 break;
         }
         
         sender.close();
         recipient.close();
    }
}

(Please note that i removed the condotion in the "while" and "if" since i wanted to eliminate anything that will might appear because of it)

Clients main:

public class ClientMain 
{
    public static void main(String[] args) 
    {
        String s;
        Scanner scan = new Scanner(System.in);
            
        try {
            ClientChat chat = new ClientChat("localhost", 7777);

            //while true gets messages if any and print them to the console
            chat.getMessages(); 
            s = scan.nextLine();
            while(!s.equals("1"))
            {
                chat.sendMessage(s);
                s = scan.nextLine();
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }   
}

Clients Thread:

    public class ClientChat 
    {
    Socket socket;
    String ip;
    int  port;
    OutputStream outputStream;
    ObjectOutputStream objOutputStream;
    InputStream inputStream;
    ObjectInputStream objectInputStream;
    
    public ClientChat(String ip, int port)  throws UnknownHostException, IOException
    {
        this.ip = ip;
        this.port = port;
        socket = new Socket(ip, port);
        
         // Create output stream for the recipient
         outputStream = socket.getOutputStream();
         objOutputStream = new ObjectOutputStream(outputStream);
         
         // Create input stream for the recipient
         inputStream = socket.getInputStream();   
         objectInputStream = new ObjectInputStream(inputStream);
    }
    
    
    public void getMessages() throws IOException
    {
         
         Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                 while(true)//socket.isConnected())
                 {
                     String msg;
                     try {
                        msg = objectInputStream.readUTF();
                         System.out.println(msg);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                 }
            }
         });
         
         t.start();  
    }
    
    
    public void sendMessage(String message)
    {
        try {
            objOutputStream.writeUTF(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Well, this code doesn't work, I have run the server and two clients, the clients do connect to the server, and you can enter input to readLine() method, but nothing happen.

i have tried to dubug the server, what i found is that it stack on the line:

             msg = objectInputStream.readUTF();

that all the relevant info i have, if you need some more information please comment

You need to call the flush() method from the ObjectOutputStream object. If you don't the data to send are stored in an internal buffer. It will get only send when the buffer is full or when you explicit call the flush() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM