带套接字的Java多线程管理|无法将正确信息从服务器发送到特定客户端

带套接字的Java多线程管理|无法将正确信息从服务器发送到特定客户端,java,multithreading,sockets,Java,Multithreading,Sockets,我的目标是让两个客户端通过这个服务器相互通信。然而,当我尝试向client1发送信息时,client2会吞噬打印流,让client1等待输入(我相信这就是为什么我会出现IndexOutOfBounds错误)。我尝试使用同步来解决这个问题,但似乎没有效果。我想知道我是否做错了什么,或者我是否可以添加一些代码来指定向哪个客户端发送信息(这样我就可以手动向client1或client2发送信息)。我在代码中留下了一些关于我的意图的评论。提前谢谢 *附带问题:如何在不使用imgur的情况下将屏幕截图放在

我的目标是让两个客户端通过这个服务器相互通信。然而,当我尝试向client1发送信息时,client2会吞噬打印流,让client1等待输入(我相信这就是为什么我会出现IndexOutOfBounds错误)。我尝试使用同步来解决这个问题,但似乎没有效果。我想知道我是否做错了什么,或者我是否可以添加一些代码来指定向哪个客户端发送信息(这样我就可以手动向client1或client2发送信息)。我在代码中留下了一些关于我的意图的评论。提前谢谢


*附带问题:如何在不使用imgur的情况下将屏幕截图放在这里?

您正在覆盖第一次连接的
printStream
scan
字段以及第二次连接的流。您需要在
ServerThread
中保留输入/输出流的单独副本。您正在覆盖
printStream
scan
字段,从第一次连接到第二次连接的流。您需要在
ServerThread
中保留输入/输出流的单独副本。
package General;

import java.io.*;
import java.net.*;
import java.util.Scanner;

// can ignore these instance variables
class Server
{
    public static final int PORT = 7777;

    protected static final int HEIGHT = 525;

    protected static final int WIDTH = 1680;

    private double y1 = 300;

    private double y2 = 300;

    private Scanner scan;

    private PrintStream printStream;

    ServerSocket serverSocket = null;

    private int numClients = 2;

    HardPart hp = new HardPart();

    // STARTING THE SERVER
    public static void main( String args[] ) throws IOException
    {
        System.out.println( "THIS IS THE RIGHT SERVER" );
        System.out.println( "Server up & ready for connection..." );

        Server s = new Server();
        s.runServer();
    }

    //WAITS ON THE 2 CLIENTS AND ASSIGNS SEPERATE THREADS FOR THEM
    public void runServer() throws IOException
    {
        ServerThread one = null;
        ServerThread two = null;
        System.out.println( "Hi from runServer" );
        System.out.println( "Creating Servers..." );
        ServerSocket serverSocket = new ServerSocket( PORT );
        while ( true )
        {
            Socket socket = serverSocket.accept();
            if ( numClients == 2 )
            {
                one = new ServerThread( socket );
                numClients--;
                System.out.println( "1. created " + one.getName() );
            }
            else if ( numClients == 1 )
            {
                two = new ServerThread( socket );
                System.out.println( "2. created " + two.getName() );
                one.start();
                two.start();

                // I dont get why the "hi" doesnt get reached

                System.out.println( "hi" );
            }
        }
    }

    /SERVER THREAD CLASS
    public class ServerThread extends Thread
    {
        Socket socket;


        ServerThread( Socket socket )
        {
            this.socket = socket;
        }


        public void run()
        {
            try
            {
                synchronized ( hp )
                {
                    hp.intro( socket );
                }

                String message = scan.nextLine();
                {
                    synchronized ( hp )
                    {
                        hp.doPart1( message );
                        System.out.println( " DONE WITH PART one." );
                    }

                    // I always get an error here
                    // Exception in thread "Thread-0" 
                    // java.lang.IndexOutOfBoundsException: end

                    message = scan.nextLine();
                    synchronized ( hp )
                    {
                        hp.doPart1( message );
                        System.out.println( " DONE WITH PART two." );
                    }
                }
                socket.close();
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
        }
    }


    class HardPart
    {
        public void intro( Socket socket ) throws IOException, InterruptedException
        {
            System.out.println( "Running threads...");
            scan = new Scanner( socket.getInputStream() );
            printStream = new PrintStream( socket.getOutputStream() );
            System.out.println( "User " + scan.nextLine() + " has connected to the server." );
            System.out.println( "IM OUT" );
            printStream.println( "go" );
            System.out.println( "go sent" );
        }


        public void doPart1( String message ) throws InterruptedException
        {
            System.out.println( "Incoming client message: " + message );
            String position = message.substring( 10 );
            double positionDouble = Double.parseDouble( position );
            String info = message.substring( 0, 8 );
            if ( info.equals( "p1 y pos" ) )
            {
                y1 = positionDouble;
                System.out.println( y1 );
            }
            else if ( info.equals( "p2 y pos" ) )
            {
                y2 = positionDouble;
                System.out.println( y2 );
            }
            if ( info.equals( "p1 y pos" ) )
            {
                printStream.println( y2 );
            }
            else if ( info.equals( "p2 y pos" ) )
            {
                printStream.println( -y1 );
            }
        }
    }
}