简体   繁体   English

从一个客户端连接到多个套接字

[英]Connecting to multiple sockets from one client

Hi I am new to java client server programming, and I have this modification of a simple program. 嗨,我是Java客户端服务器编程的新手,并且我对一个简单程序进行了修改。 What I am trying to do is create 2 sockets within the same client, which I then want to use to access 2 sockets on 2 different servers. 我想做的是在同一个客户端中创建2个套接字,然后我想用它来访问2个不同服务器上的2个套接字。 The server responds with a very simple String, which is then written into a file and displayed to console. 服务器以非常简单的String进行响应,然后将其写入文件并显示到控制台。 This works fine for whichever sockets code I put first. 无论我先放置哪个套接字代码,此方法都可以正常工作。 ie if i put clientsocket1 code on top, it receives correctly, but clientsocket2 doesn't get anything. 即,如果我将clientsocket1代码放在最上面,它会正确接收,但是clientsocket2没有任何东西。 Vice versa if I flip the order. 反之亦然,如果我取消订单。 Im testing it all on my own computer so I'm using default IP address "0.0.0.0" and different port numbers for the servers. 我在自己的计算机上进行了全部测试,因此我在使用默认IP地址“ 0.0.0.0”和服务器的不同端口号。 What could be the issue here? 这里可能是什么问题?

public static class CustomerClient extends Thread {
    private Socket clientSocket1;
    private Socket clientSocket2;
    String serverInput1;
    String serverInput2;
    String clientCustomerId;

    public CustomerClient(String customerId, String IPAdress1,
            int portNumber1, String IPAdress2, int portNumber2) {
        try {
            clientCustomerId = customerId;
            clientSocket1 = new Socket(IPAdress1, portNumber1);
            clientSocket2 = new Socket(IPAdress2, portNumber2);
        }

        catch (UnknownHostException ex) {
            System.out.println("connection error " + ex);
        } catch (IOException ex) {
            System.out.println("Some other exception" + ex);
        } catch (Exception ex) {
            System.out.println("Some other final exception" + ex);
        }

    }

    public void run() {

        try {



            DataOutputStream outToServer = new DataOutputStream(
                    clientSocket1.getOutputStream());

            BufferedReader inFromServer = new BufferedReader(
                    new InputStreamReader(clientSocket1.getInputStream()));

            while ((serverInput1 = inFromServer.readLine()) != null) {
                BufferedWriter br = new BufferedWriter(new FileWriter(
                        tempFile, true));
                br.write(serverInput1);
                br.newLine();
                // br.append(modifiedSentence);
                System.out.println("FROM SERVER: " + serverInput1);
                br.close();

            }

            DataOutputStream outToServer2 = new DataOutputStream(
                    clientSocket2.getOutputStream());

            BufferedReader inFromServer2 = new BufferedReader(
                    new InputStreamReader(clientSocket2.getInputStream()));

            while ((serverInput2 = inFromServer2.readLine()) != null) {
                BufferedWriter br = new BufferedWriter(new FileWriter(
                        tempFile, true));
                br.write(serverInput2);
                br.newLine();
                // br.append(modifiedSentence);
                System.out.println("FROM SERVER: " + serverInput2);
                br.close();
            }

        } catch (IOException ex) {

        } catch (Exception e) {
            System.out.println("Error" + e);
        } finally {
            try {
                clientSocket1.close();
                clientSocket2.close();
            } catch (Exception e) {
                System.out.println("Error in closing the connection" + e);
            }
        }
    }
}

Isn't, in your case, the client also a server? 在您的情况下,客户端不是服务器吗? Because a normal client only connects to one server and that server can handle multiple clients. 因为普通客户端仅连接到一台服务器,并且该服务器可以处理多个客户端。 So, if you want your client to have multiple "connections" to your server, you have two possibilities: 因此,如果希望客户端与服务器建立多个“连接”,则有两种可能性:

  1. Make your client a server itself by giving it a ServerSocket object or 通过将ServerSocket对象赋予客户端,使客户端本身成为服务器
  2. Create a kind of protocol, which serializes data in this connection and distributes it correctly 创建一种协议,该协议可以序列化此连接中的数据并正确分发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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