SOCKETS让客户倾听

时间:2016-01-19 14:27:04

标签: java sockets

我认为问题是client 2没有进入接收模式,客户端之间的连接在第一个客户端向第二个客户端发送内容之前关闭......但我可能错了。

以下是serverclients的代码。程序基于令牌环算法。我不熟悉Java,所以我想我没有用最好的方法解决它。

服务器:

public class TokenServer 
   {
    ServerSocket clientConn;

    public TokenServer(int port){
        System.out.println("Server connecting to port "+port);
        try {
            clientConn = new ServerSocket(port);
        }
        catch (Exception e) {
            System.out.println("Exception:"+e);
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        int port = 8000;
        if (args.length > 0) {
            try {
                port = Integer.parseInt(args[0]);
            }
            catch (Exception e) {
                port = 8000;
            }
        }
    TokenServer server = new TokenServer(port);



    System.out.println("Server running on port "+port);
    server.listen();
    }

    public void listen(){
        try{
            System.out.println("Waiting for clients...");
            while(true) {
                Socket clientReq = clientConn.accept();
                System.out.println("Connection from "+clientReq.getInetAddress().getHostName());
                serviceClient(clientReq);
            }
        }
        catch (IOException e) {
            System.out.println("Exception:"+e);
        }
    }

    public void serviceClient(Socket s){
        System.out.println("New request from "+s.getInetAddress());
        ObjectInputStream inStream = null;
        ObjectOutputStream outStream = null;
        int message_id;



        Object message = null;
        try{
            outStream = new ObjectOutputStream(s.getOutputStream());
            inStream = new ObjectInputStream(s.getInputStream());
            message_id = inStream.readInt();
            System.out.println("Got message "+message_id);

            int zmienna;
            zmienna = message_id + message_id;
            outStream.writeObject(zmienna);

            outStream.flush();
        }
        catch (Exception e) {
            System.out.println("Exception:"+e);
        }
        System.out.println("Done.");
    }
}

客户1:

    public class TokenClient1{
    int sendport,recport;
    boolean hasToken = true;
    boolean setSendData = false;
    String host;


    public TokenClient1(String host){
        this.host = host;
    }

    void setSendPort(int sendport)
    {
        this.sendport = sendport;
    }

    void setRecPort(int recport)   
    {
        this.recport = recport;
    }

    public Object sendMessage(int message_id) throws Exception
    {
        Socket serverConn;
        ObjectInputStream inStream = null;
        ObjectOutputStream outStream = null;
        Object response = null;
            serverConn = new Socket(host, sendport);
            outStream = new ObjectOutputStream(serverConn.getOutputStream());
            inStream = new ObjectInputStream(serverConn.getInputStream());
            outStream.writeInt(message_id);
            outStream.flush();
            response = inStream.readObject();
            serverConn.close();
            //hasToken = false;
        return response;
    }

    public Object recData() throws Exception
    {
        Socket serverConn;
        ObjectInputStream inStream = null;
        Object response = null;
            serverConn = new Socket(host, recport);
            inStream = new ObjectInputStream(serverConn.getInputStream());
            response = inStream.readObject();
            serverConn.close();


        if(response.toString().equals("Token"))
            {
                hasToken = true;
            }
        return response;
    }



    public static void main (String[] args){

        TokenClient1 client = new TokenClient1("localhost");
        TokenClient1 server = new TokenClient1("localhost");

        client.setSendPort(8002);
        client.setRecPort(8002);
        server.setSendPort(8000);

        Scanner scan = new Scanner(System.in);
        int wybor, zmienna;
        try{

        while(true)
            {    
        if(client.hasToken == true){
            System.out.println("Do you want to enter the Data --> \n1. YES \n0. NO");
            wybor = scan.nextInt();
            switch (wybor) {
                case 1:
                    {
                        System.out.println("ready to send \n Podaj liczbe do dodania: ");
                        //server.setSendData = true;
                        zmienna = scan.nextInt();
                        System.out.println("odpowiedz servera:");
                        System.out.println((Integer)server.sendMessage(zmienna));
                        break;
                    }
                case 0:
                {
                    System.out.println("i m in else");
                    client.sendMessage(0);
                    client.recData();
                    System.out.println("i m leaving else");
                    break;
                }


            }

        }
        else {
            System.out.println("ENTERING RECEIVING MODE...");
            client.recData();
        }

            }


        }
        catch(Exception e) {
            System.out.println("Esception:"+e);
        }
    }
}

客户2:

    public class TokenClient2
    {
    int sendport,recport;
    boolean hasToken = false;
    String host;
    ServerSocket clientConn;


    public TokenClient2(String host){
        this.host = host;
    }

    void setSendPort(int sendport)
    {
        this.sendport = sendport;
    }

    void setRecPort(int recport)   
    {
        this.recport = recport;
    }

    public Object sendMessage(int message_id) throws Exception
    {
        Socket serverConn;
        ObjectInputStream inStream = null;
        ObjectOutputStream outStream = null;
        Object response = null;
            serverConn = new Socket(host, sendport);
            outStream = new ObjectOutputStream(serverConn.getOutputStream());
            inStream = new ObjectInputStream(serverConn.getInputStream());
            outStream.writeInt(message_id);
            outStream.flush();
            response = inStream.readObject();
            serverConn.close();
            //hasToken = false;
        return response;
    }

    void recData() throws Exception
    {
        try{
            System.out.println("Waiting for clients...");
          //  while(true) {
        Socket serverConn;

        ObjectInputStream inStream = null;
        Object response = null;
        clientConn = new ServerSocket(8002);
        serverConn = new Socket(host, 8000);
        Socket clientReq = clientConn.accept();
        System.out.println("Connection from "+clientReq.getInetAddress().getHostName());


            inStream = new ObjectInputStream(serverConn.getInputStream());
            response = inStream.readObject();


            clientConn.close();
        System.out.println("Waiting for cents...");

        if(response.equals(0))
            {
                System.out.println("tdfss...");
                hasToken = true;
            }
        //return response;
            }
        //}
        catch (IOException e) {
            System.out.println("Exception:"+e);
        }
    }



    public static void main (String[] args){

        TokenClient2 client = new TokenClient2("localhost");
        TokenClient2 server = new TokenClient2("localhost");

        client.setSendPort(8002);
        client.setRecPort(8002);
        server.setSendPort(8000);

        Scanner scan = new Scanner(System.in);
        int wybor, zmienna;
        try{

        while(true)
            {    
        if(client.hasToken == true){
            System.out.println("Do you want to enter the Data --> \n1. YES \n0. NO");
            wybor = scan.nextInt();
            switch (wybor) {
                case 1:
                    {
                        System.out.println("ready to send \n Podaj liczbe do dodania: ");
                        //server.setSendData = true;
                        zmienna = scan.nextInt();
                        System.out.println("odpowiedz servera:");
                        System.out.println((Integer)server.sendMessage(zmienna));
                        break;
                    }
                case 0:
                {
                    System.out.println("i m in else");
                    client.sendMessage(0);
                    client.recData();
                    System.out.println("i m leaving else");
                    break;
                }


            }

        }
        else {

            client.hasToken=true;
            System.out.println("ENTERING RECEIVING MODE...");
            client.recData();

        }

            }


        }
        catch(Exception e) {
            System.out.println("Esception:"+e);
        }
    }
}

0 个答案:

没有答案