简体   繁体   English

如何在Java套接字编程中在一台服务器和多个客户端之间进行通信?

[英]How to make communication between one server and multiple client in Java Socket Programming?

I have two java applications, one is web app and another is simple java app, So I am using Socket programming for communication between them. 我有两个Java应用程序,一个是Web应用程序,另一个是简单的Java应用程序,因此我正在使用Socket编程进行它们之间的通信。

I made one SocketServer which is a Thread, in which I created ServerSocket serverSocket = new ServerSocket(6789) And in my web app I created Socket client = new Socket("localhost", 6789); 我做了一个SocketServer,它是一个线程,在其中创建了ServerSocket serverSocket = new ServerSocket(6789)然后在我的Web应用程序中创建了Socket client = new Socket("localhost", 6789); My server sends some data to client and client will start some other work, but if I want to run another client ie server will send different parameters and client have to start processing what should I do? 我的服务器向客户端发送一些数据,客户端将开始其他工作,但是如果我要运行另一个客户端,即服务器将发送不同的参数,客户端必须开始处理该怎么办?

Because server is already started on '6789' port and first client also with the same port. 因为服务器已经在“ 6789”端口上启动,并且第一个客户端也使用相同的端口启动。 How can I start client with another port? 如何用另一个端口启动客户端?

Every time Server must have to started first and then client. 每次必须首先启动服务器,然后再启动客户端。

I think client will not found server till both are having same ports. 我认为在两个端口具有相同端口之前,客户端将找不到服务器。

Am I have to create another server instance with different port and then invoke client??? 我是否必须使用其他端口创建另一个服务器实例,然后调用客户端??? But How can my client will know on which port server is started? 但是我的客户如何知道在哪个端口服务器上启动?

For Example: 例如:

Imagine I have UI like: 想象一下,我有这样的UI:

start MIlind 启动MIlind

start xyz 开始xyz

start abc 开始abc

and click on strart it will call client and start process, If an start Milind first then How will I start xyz? 并单击strart它将调用客户端并启动进程,如果先启动Milind,则如何启动xyz? because 'start Milind' started client and server at port 6789, How will other start process works? 因为'start Milind'在6789端口启动了客户端和服务器,所以其他启动过程将如何工作?

It seems like a lot of overhead to create a server/client app just for a web app to communicate with a local java program (and even more so to duplicate this process to do more than one thing at a time). 创建仅用于Web应用程序以与本地Java程序进行通信的服务器/客户端应用程序似乎有很多开销(甚至更多,因此重复此过程一次要做的事情不只一次)。 If you are looking for concurrent processing in the background of a web app, you can always just create a thread (or multiple threads) to do the work. 如果要在Web应用程序的后台中查找并发处理,则始终可以仅创建一个线程(或多个线程)来完成工作。 Or is there a reason why the simple java app can't be embedded in the web app? 还是有一个原因无法将简单的Java应用程序嵌入到Web应用程序中?

You need to split off threads when accepting your socket connections server side. 接受套接字连接服务器端时,您需要拆分线程。 This is very easily done with serversocket. 使用serversocket可以很容易地做到这一点。 A very rudimentary (untested!) implementation: 一个非常基本的(未经测试!)实现:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class Server {

    private ServerSocket socket;

    public Server() {
        try {
            this.socket = new ServerSocket(6789);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void go() throws IOException {
        while(true) {
            Socket sock = socket.accept();
            new Thread(new ClientSession(sock)).start();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        try {
            server.go();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class ClientSession implements Runnable {

        private final Socket clientsocket;

        ClientSession(Socket sock) {
            this.clientsocket = sock;
        }

        @Override
        public void run() {
            //do stuff, like read from socket.
        }

    }

}

Note that you don't need to change the port at all. 请注意,您根本不需要更改端口。

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

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