带套接字的Java多线程管理|无法从服务器向特定客户端

时间:2017-10-29 17:31:19

标签: java multithreading sockets

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 );
            }
        }
    }
}

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

*方面问题:如何在不使用imgur的情况下在此处放置屏幕截图?

0 个答案:

没有答案