【问题标题】:Java Multi Client server socketJava 多客户端服务器套接字
【发布时间】:2017-02-25 20:25:33
【问题描述】:

我正在尝试在服务器(NewServer 类)和客户端(NewClient 类)之间建立通信,接受两个客户端通信。我知道如何使用一个客户端而不是多个客户端连接来做到这一点。

  • 是否必须在 Client 类中创建 2 个阅读器?

  • 我可以以递归方式执行此操作吗?

服务器类:

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

public class NewServer
{
    //Create Server Socket and a Client Socket for each Client.
    static ServerSocket server;
    static Socket client1;
    static Socket client2;

    //Create OutputStreams to send information to each Client.
    static DataOutputStream client1writer;
    static DataOutputStream client2writer;

    static final int PORT = 9999;

    //Main Method
    public static void main(String[]args)
    {
        //Try-Catch Block for Socket Errors.
        try
        {
            //Create the Server Socket as a Host.
            server = new ServerSocket(PORT);

            //Connect Client 1 – First run of the Client class.
            client1 = server.accept();
            client1writer = new    
            DataOutputStream(client1.getOutputStream());

            //Connect Client 2 – Second run of the Client class.
            client2 = server.accept();
            client2writer = new     
            DataOutputStream(client2.getOutputStream());            

            //Assign each Client an ID number – this is how the Client will know
            //    which individual Client it’s representing in RunTime.
            int ID1 = 8675309;
            int ID2 = 8675308;

            //Tell both Clients which one they are representing.
            client1writer.writeInt(ID1);
            client2writer.writeInt(ID2);

            //Close all Sockets when finished.
            client1.close();
            client2.close();
            server.close();
        }
        catch (IOException IOex)
        {
            System.out.println("Server Error.");
        }
    }
}

客户端类:

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

public class NewClient {

    static Socket client = null;                          
    static final int PORT = 9999;                      
    static final String IP = "localhost";                

    public static void main(String[] args) throws IOException {

        int id1;      
        int id2;                      

        try{
            client = new Socket(IP,PORT);      
            System.out.println("Connection successful!");

            reader = new DataInputStream(client.getInputStream());

            id1 = reader.readInt();
            id2 = reader.readInt();       

            System.out.println("The id of the user is " + id);

            //Closing everything
            client.close();
            reader.close();

        }catch(IOException error) {
            System.err.println("Server error.");
        }
    }
}

【问题讨论】:

    标签: java sockets server client serversocket


    【解决方案1】:

    您可以通过为每个新的客户端连接启动一个单独的线程(也称为普通线程,然后调用循环运行的server.accept() 来实现它。它作为一个学习示例很有用,但当您失去对线程的控制时,不会帮助您实现所需的目标。

    第二种方法是使用Executor Service,它为上述解决方案提供了更好的管理控制。

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多