简体   繁体   English

如何在socket编程中制作多个消息?

[英]How to make multiple message in socket programming?

I have a project in which i make a chat server, where people(client) connect and choose a partner to chat with. 我有一个项目,我在其中创建一个聊天服务器,人们(客户端)连接并选择要与之聊天的合作伙伴。 However, they chat only when both clients send a message they see what the other typed. 但是,只有当两个客户端都发送消息时,他们才会聊天,他们会看到其他类型的消息。 What I want is something like Whatsapp where we send many messages one after the other without waiting for the other client to send. 我想要的是类似Whatsapp的东西,我们一个接一个地发送许多消息而不等待其他客户端发送。

I tried to create a thread class where I invoke the chat and other stuff, but never worked. 我试图创建一个线程类,我调用聊天和其他东西,但从来没有工作过。

//this is the client.java and this is the part of the code where they start chatting
do {
        System.out.print(dis.readUTF());
        System.out.println(dis.readUTF());
        send = in.next();
        dos.writeUTF(send);
        b = dis.readBoolean();
    } while (b);

//this is part of the chatserver.java where the connection is done they start chatting
class handleClient implements Runnable {

    private Socket s1;
    private Socket s2;
    private String name1;
    private String name2;

    public handleClient(Socket s1, Socket s2, String n1, String n2) {
        this.s1 = s1;
        this.s2 = s2;
        this.name1 = n1;
        this.name2 = n2;
    }

    @Override
    public void run() {
        String msg1 = "", msg2 = "";
        boolean b;
        try {
            DataOutputStream dos1 = new DataOutputStream(s1.getOutputStream());
            DataInputStream dis1 = new DataInputStream(s1.getInputStream());

            DataOutputStream dos2 = new DataOutputStream(s2.getOutputStream());
            DataInputStream dis2 = new DataInputStream(s2.getInputStream());

            do {
                dos1.writeUTF(name1 + ": ");
                dos2.writeUTF(name2 + ": ");
                dos2.writeUTF(name1 + ": " + msg1);
                msg1 = dis1.readUTF();
                dos1.writeUTF(name2 + ": " + msg2);
                msg2 = dis2.readUTF();

                b = !msg1.equals(name1 + " is out") && !msg2.equals(name2 + " is out");
                dos1.writeBoolean(b);
                dos2.writeBoolean(b);
            } while (b);

            dos1.close();
            dis1.close();
            dos2.close();
            dis2.close();
            s1.close();
            s2.close();
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}

As @Mohsen fallahi said use websockets (you get the non blocking functionality implicit) 正如@Mohsen fallahi所说使用websockets(你得到非阻塞功能隐含)

For a pure javascript Solution see: https://javascript.info/websocket 有关纯JavaScript的解决方案,请参阅: https//javascript.info/websocket

readUTF() , well, reads an UTF string. readUTF() ,读取一个UTF字符串。 It does not return until it has read one. 它只读了一个才返回。 That is why you want to read from the different clients in parallel, so it would not matter which one sends a message first, it would be read and could be forwarded afterwards, independently from others. 这就是为什么你想要从不同的客户端并行读取的原因,因此首先发送消息的哪个是无关紧要的,它将被读取并且可以在之后独立于其他客户端进行转发。 One way to do that is starting a separate reader thread for each client. 一种方法是为每个客户端启动一个单独的读者线程。
Here is a simple chatroom server (I did not want to deal with selecting clients, sorry): 这是一个简单的聊天室服务器(我不想处理选择客户端,抱歉):

public class ChatServer {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(5555);
        List<DataOutputStream> doss = new ArrayList<>();
        while (true) {
            Socket client = server.accept();
            synchronized(doss) {
                doss.add(new DataOutputStream(client.getOutputStream()));
            }
            new Thread(new Runnable() {
                public void run() {
                    try (DataInputStream dis = new DataInputStream(client.getInputStream())) {
                        while (true) {                    // <---------  per-client loop
                            String message=dis.readUTF();
                            synchronized (doss) {
                                for (DataOutputStream dos : doss)
                                    dos.writeUTF(message);
                            }
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

It has a central storage of outgoing streams ( doss ), and one thread per client for reading the inbound streams. 它有一个输出流的中央存储( doss ),每个客户端有一个线程用于读取入站流。 Then the threads read a string from their client and forward it to all of the outgoing streams, forever (the inner while(true) loop). 然后线程从其客户端读取一个字符串并将其转发到所有传出流,永远(内部while(true)循环)。
On the client side the loops are practically one-liners, one for reading message and sending it to the server, and an other one for getting something from the server and printing it on the screen: 在客户端,循环实际上是一行,一个用于读取消息并将其发送到服务器,另一个用于从服务器获取内容并将其打印在屏幕上:

public class ChatClient {
    public static void main(String[] args) throws Exception {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter your nick: ");
        String name = s.nextLine();
        Socket socket = new Socket("localhost", 5555);
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        new Thread(new Runnable() {
            public void run() {
                try {
                    while (true)                              // <------- receiver loop, in thread
                        System.out.println(dis.readUTF());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
        dos.writeUTF(name + " has joined the conversation");
        while (true)                                          // <------- sender loop
            dos.writeUTF(name + ": " + s.nextLine());
    }
}

The thing works with hardcoded localhost on port 5555 , provides no error handling at all (server dies when a client leaves). 这个东西适用于端口5555上的硬编码localhost ,根本不提供任何错误处理(服务器在客户端离开时死机)。 It was meant to be short. 它本来就是短暂的。

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

相关问题 如何在 Java 套接字编程上做出回应? - How to make echo on Java socket programming? 如何在Java套接字编程中在一台服务器和多个客户端之间进行通信? - How to make communication between one server and multiple client in Java Socket Programming? Java-套接字编程-如何使客户端从多个服务器接收消息? - Java - Socket Programming - How can I make a client receive msg from multiple servers? 如何使用套接字编程将消息发送到另一个Android移动设备? - How to send message to another android mobile using socket programming? 如何在Java套接字编程中用单个服务器创建多个客户端? - how to create multiple clients with single server in java socket programming? 如何为多个客户端创建套接字服务器 - how to make a socket server for multiple clients 如何防止套接字在具有多个线程的程序中变为空(Java套接字线程编程) - How to prevent socket from becoming null in a program with multiple threads (java socket thread programming) 如何在JAVA套接字编程中使用UDP来实现两个端口之间的简单消息通信? - How can I use UDP in JAVA Socket Programming to implement simple message communication between two ports? ssl插座编程怎么做 - how to do ssl socket programming 通过套接字将多个客户端编程到一台服务器 - socket programming multiple client to one server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM