java - Java中的聊天未同步(套接字,线程)

我正在尝试找出一种方法来获取服务器的实例,以通过在两个客户端之间建立聊天线程来进行协商。

我创建了这个项目,它“几乎”可以工作……但似乎存在同步问题的缓冲区。

当在一侧(即Client#1)编写行时,它不会传递到另一侧(即Client#2),但只有在Client#2尝试通过行之后也不会传递。

我知道可能有更好的方法来实现此目的,但我想了解我的代码有什么问题。

您的帮助将非常棒!

代码:

服务器

import java.io.*;
import java.net.*;

public class Server
{
    public static void main(String[] args)
    {
        int id = 1;

        System.out.println();
        System.out.println("Server");

        try
        {
           ServerSocket serverSocket = new ServerSocket(4321);

            while (true)
            {
                Socket client1Socket = serverSocket.accept();
                Socket client2Socket = serverSocket.accept();

                System.out.println("clients connected from ports: \n"
                        + client1Socket.getPort() + ", " + client2Socket.getPort());

                Thread client1Thread = new ServerThread(client1Socket, client2Socket, id);
                client1Thread.start();
                id++;

                Thread client2Thread = new ServerThread(client2Socket, client1Socket, id);
                client2Thread.start();
                id++;
            }
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }
}

服务器线程
import java.io.*;
import java.net.*;
import java.util.*;

public class ServerThread extends Thread 
{
    Socket sourceSocket;
    Socket destSocket;
    int id;

    public ServerThread(Socket src, Socket dst, int n) 
    {
        sourceSocket = src;
        destSocket = dst;
        id = n;
    }

    public void run() 
    {
        try
        {
            Scanner clientInput = new Scanner(sourceSocket.getInputStream());
            PrintStream destOutput = new PrintStream(destSocket.getOutputStream());
            destOutput.println("You are chatting with Client " + id);

            boolean more = true;
            while (more) 
            {
                String input = clientInput.nextLine();
                destOutput.println(input);

                if (input.equals("Q"))
                {
                    more = false;
                }
            }
            sourceSocket.close();
            destSocket.close();
        }
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
}

客户
import java.io.*;
import java.net.*;
import java.util.*;

public class Client 
{
    public static void main(String[] args) 
    {
        System.out.println();
        System.out.println("Client");

        try
        {
            Socket clientSocket = new Socket("localhost", 4321);

            System.out.println("Connection Established");
            Scanner input = new Scanner(clientSocket.getInputStream());
            PrintStream output = new PrintStream(clientSocket.getOutputStream());
            Scanner in = new Scanner(System.in);

            System.out.println(input.nextLine());

            boolean more = true;

            while (more) 
            {
                String text = in.nextLine();
                output.println(text);
                String nextInput = input.nextLine();

                if (nextInput == null) 
                {
                    more = false;
                }
                else 
                {
                    System.out.println(nextInput);
                }
            }
        }
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
}

最佳答案

在您的客户端代码中,String text = in.nextLine();行将阻塞您的线程。这意味着,如果您从不在客户端中输入任何内容,那么您将收不到任何东西。因此,解决方案是将您的消息接收代码放入另一个线程中。如:

    Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        while (!Thread.interrupted()) {
                            System.out.println(input.nextLine());
                        }
                    }

                });
                thread.start();

                while (true) 
                {
                    String text = in.nextLine();
                    output.println(text);
//                String nextInput = input.nextLine();
                    ......................
                }