客户端程序可以具有服务器套接字以与其他客户端程序进行通信吗?

时间:2018-11-14 18:47:14

标签: java multithreading sockets serversocket distributed-system

我正在尝试实现一个多线程套接字程序,该程序模拟五台计算机和一个控制器通过套接字相互通信。控制器具有一个服务器套接字,该套接字可以接受来自计算机的传入套接字。一旦达到系统的最大容量(比方说五个),它将使用接受传入套接字时创建的线程向这些计算机发送启动消息。

当每台计算机从控制器接收到“开始”消息时,我希望每台计算机都能够与其他计算机通信(无需依赖控制器(从计算机到控制器到计算机之间传递消息))。每台计算机实例化一个服务器套接字,以便它可以接受来自计算机的传入套接字;还实例化另一个客户端套接字,以便来自其他计算机的其他服务器套接字可以接受它。

我知道这听起来似乎很混乱,但是基本上我想在每个客户端程序(计算机)上使用服务器套接字,以便它可以在不依赖控制器的情况下侦听其他客户端(计算机)。

这可能吗?我可以实例化服务器程序套接字的每个客户端程序(计算机),以便它可以侦听其他计算机吗?它是否需要与控制器的服务器插槽不同的IP地址和/或端口号?我是否需要为x数量的计算机实例化x数量的套接字?

也许我的Java代码可以解决这个问题。

Controller.java

    package timetableexchange;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Controller {

final int MAX_SYSTEMS = 2;

static ArrayList<ServerConnection> conns = new ArrayList<ServerConnection>();   // connections to computer
static int finishedCount = 0;                                                   // number of finished computers

ServerSocket ss;            // server socket

public static void main(String[] args) throws IOException {
    // Instantiate controller
    new Controller(8000);
}

public Controller(int port) throws IOException {
    // Instantiate server socket
    ss = new ServerSocket(8000);
    int i = 0;
    // Listen and accept clients (1 for testing)
    while (i < MAX_SYSTEMS) {
        Socket s = ss.accept();
        // add to list
        ServerConnection conn = new ServerConnection(i++, s);
        conns.add(conn);
    }
    // start server connection thread
    for (i = 0; i < conns.size(); ++i) {
        conns.get(i).start();
    }
    ss.close();
}

// Thread for communicating between controller and computer
private class ServerConnection extends Thread {

    Socket socket;
    BufferedReader in;
    PrintWriter out;
    int identifier;

    // constructor
    public ServerConnection(int i, Socket s) throws IOException {
        this.identifier = i;
        this.socket = s;
        this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.out = new PrintWriter(socket.getOutputStream(), true);
        System.out.println("Client Connected");
    }

    @Override
    public void run() {
        System.out.println("ServerConnection started");
        // send ID to computers
        sendAll(identifier + "");
        // send Start message to computers
        sendAll("Start");
        // Check if a computer sent back a Finish message
        // If all computers are finished, then send out Tear Down message.
        while (true) {
            try {
                String clientInput = in.readLine();
                if (clientInput.equals("Finish")) {
                    finishedCount += 1;
                    if (finishedCount == conns.size()) {
                        sendAll("Tear Down");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    private void sendAll(String text) {
        for (int i = 0; i < conns.size(); ++i) {
            ServerConnection conn = conns.get(i);
            conn.out.println(text);
        }
    }
}

}

Computer.java

package timetableexchange;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;

public class Computer {

final int MAX_SYSTEMS = 2;

int identifier;                                         // Computer ID
int eventCount;                                         // Number of Generated Events
ArrayList<Socket> sockets = new ArrayList<Socket>();    // List of (client) sockets
Vector<Integer> timestamp = new Vector<Integer>();      // Time-stamp vector

Socket socketToServer;                                  // Socket to Connect to Controller
BufferedReader inputFromServer;                         // Input Stream from Controller
PrintWriter outputToServer;                             // Output Stream to Controller
String textFromServer;                                  // String sent from Controller

ServerSocket ss;                                        // Server Socket to communicate with other clients (computers)
Socket socketToClient;                                  // Socket to Connect to Computer
BufferedReader inputFromClient;                         // Input Stream from Computer
PrintWriter outputToClient;                             // Output Stream to Computer

public static void main(String[] args) throws IOException {
    // Instantiate Computer
    new Computer("127.0.0.1", 8000);
}

// Constructor
public Computer(String hostname, int port) throws IOException {
    // Instantiate Socket (to Controller) and Streams (to Controller)
    socketToServer = new Socket(hostname, port);
    inputFromServer = new BufferedReader(new InputStreamReader(socketToServer.getInputStream()));
    outputToServer = new PrintWriter(socketToServer.getOutputStream(), true);

    // Check if Controller sent the computer its ID
    while (true) {
        try {
            textFromServer = inputFromServer.readLine();
            // set identifier
            identifier = Integer.parseInt(textFromServer);
            System.out.println(identifier);
            break;  // break loop
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    // Check if Controller sent the start message
    while (true) {
        textFromServer = inputFromServer.readLine();
        if (textFromServer.equals("Start"))
            System.out.println("Start message received");
            break;  // break loop
    }

    // Instantiate Server Socket (for Clients)
    ss = new ServerSocket(port + identifier + 1);

    // Instantiate Client Socket for Other Clients to Hear
    socketToClient = new Socket();
    inputFromClient = new BufferedReader(new InputStreamReader(socketToClient.getInputStream()));
    outputToClient = new PrintWriter(socketToClient.getOutputStream());

    // listen to server socket and add accepted socket to list  
    for (int i = 0; i < MAX_SYSTEMS - 1; ++i) {
        Socket s = ss.accept();
        System.out.println("Client accepted");
        sockets.add(s);
    }

    Thread readEvent = new Thread(new Runnable() {
        @Override
        public void run() {
            /**
             * Read from input stream
             * Read the vector inside the input stream
             * Compare vectors and choose the largest integer (synchronized)
             * Add 1 to the corresponding socket. (synchronized)
             */
        }
    });

    Thread writeEvent = new Thread(new Runnable() {
        @Override
        public void run() {
            /**
             * Generate random number between 0 and 4.
             * If zero, then add 1 to its own socket in vector.
             * Else, send vector to random socket via output stream
             */
        }
    });

    readEvent.start();
    writeEvent.start();
}

}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

简而言之:

  

这可能吗?

是-为什么不呢?

  

我可以实例化每个客户端程序(计算机)服务器套接字吗,这样   可以听其他电脑吗?

是的-您可以执行所需的任何操作

  

是否需要与IP地址不同的IP地址和/或端口号?   控制器的服务器套接字?

IP地址属于“物理计算机” /混合工作接口,与您要尝试执行的操作无关紧要-所有IP地址都可以在一台计算机上运行,​​并且都可以在同一IP地址上运行(例如127.0.0.1)。您必须为要打开的每个服务器套接字都具有专用端口,并且客户端套接字必须知道要与之通信的IP和端口号。 对于客户端套接字,您无需关心IP或端口号。

  

我是否需要实例化x个数量的套接字?   电脑?

问题对我来说意义不大-这是您的设计决策,需要实例化多少个服务器套接字-一个端口一个服务器套接字就可以了。