简体   繁体   English

如何使用TCP连接同时接受多个客户端?

[英]How to accept multiple clients at the same time using a TCP connection?

I have a Server that create a TCP connection via Socket with a Client. 我有一个服务器,可通过与客户端的套接字创建TCP连接。 Since my app should be a Chat I need the server to accept multiple clients simultaneously using the same port, so that they can comunicate in real-time. 由于我的应用程序应该是“聊天”,因此我需要服务器使用同一端口同时接受多个客户端,以便它们可以实时通信。 My Server-side is a Java application, my Client-side is an Android app. 我的服务器端是Java应用程序,我的客户端是Android应用程序。

Is possible to do something like this? 可以做这样的事情吗? If yes how can I do it? 如果可以,我该怎么办?

This is my server code: 这是我的服务器代码:

public class Server {

public static void main(String[] args){

    ServerClass server = new ServerClass();
    server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    server.inizia();
     }
}




public class ServerClass extends JFrame {

JTextArea testoarea;
String messaggio;
ObjectOutputStream output;
DataInputStream input;
ServerSocket server;
Socket connessione;

public ServerClass(){
    super("Server in Ascolto");
    testoarea = new JTextArea();
    add(new JScrollPane(testoarea));
    setSize(600, 700);
    setVisible(true);


}
public void inizia()
{
    try {

        server = new ServerSocket(7100);
        while(true)
        {
            try {

                iniziaConnessione();
                sistemaCanali();
                chatta();
                //closeCrap();

            } catch (EOFException eofException) {
                // TODO: handle exception
                showMessage("Il Server ha perso la connessione..\n");
            }
        }
    } catch (IOException ioException) {
        // TODO: handle exception
        ioException.printStackTrace();
    }


}

private void iniziaConnessione() throws IOException {
    // TODO Auto-generated method stub
    showMessage("Aspetto qualcuno per connettermi.... \n");
    connessione = server.accept();
    showMessage("Mi sono connesso a qalcuno... \n");

}

private void sistemaCanali() throws IOException {
    // TODO Auto-generated method stub
    output = new ObjectOutputStream(connessione.getOutputStream());
    output.flush();
    input = new DataInputStream(connessione.getInputStream());
    showMessage("I canali sono apposto...  \n");
}


private void chatta() throws IOException {
    // TODO Auto-generated method stub

    String messaggio = "Sei connesso e pronto a chattare... \n";
    showMessage(messaggio);
    messaggio = (String) input.readUTF();
    showMessage("Client - " + messaggio);
    sendData(messaggio);


}
private void sendData(String messaggio2) {
    // TODO Auto-generated method stub
    try {
        output.writeUTF(messaggio2);
        output.flush();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        showMessage("ERRORE: non riesco a inviare il messaggio...  \n");
    }
}
private void showMessage(final String text) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        testoarea.append(text);
                    }
                }
            );
} 


}

Once you have accepted a client, use another thread to manage the comunications with it. 接受客户端后,请使用另一个线程来管理与其进行的通信。 Don't manage your communications in the same thread that that running your accept() loop. 不要在与运行accept()循环相同的线程中管理通信。 Your client thread(s) can then pass data back and forth between the clients as needed, without interfering with the server's ability to accept new clients at the same time. 然后,您的客户端线程可以根据需要在客户端之间来回传递数据,而不会影响服务器同时接受新客户端的能力。

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

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