简体   繁体   中英

Sending/Receiving Strings to trigger methods in chat application using sockets

I have a chat application that passes a string on a button press to a text file. To make it work in a network, I thought about having the server's run receive strings from another user and use a method to write the string to a file.

The chat application is completely peer-to-peer, so there are no actual servers running or anything of the sort.

In the end, I want both users to basically share one textfile which is the chat content. If I were to write a message, my message would get written in my local textfile and the other user would receive the string (message) and append my string to his own textfile.

If he writes a string, he would append it to his textfile and send me the string, which would be appended to my textfile. This way we "simulate" a type of server that logs the chat between two users.

The problem is that I have absolutely no idea how to do this using sockets and what the best approach is.

Currently, I have a class that triggers the writing of the text file with the method:

        // Button Send Message
    view.btnSendMessage.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent arg0){
            String userInput = view.chatMessage.getText();
            // clears the chat history
            view.chatWindow.clear();
            try {
                model.writeNewChatMessage(user.getUserName() +  ": " + userInput);
            }
            catch (Exception e) {
                System.out.println("unable to send");
            }
            // sends textfield message to model method

            // updates view's chat history textarea
            view.updateChatHistory(model.getChatHistory());
            // clears textfield
            view.chatMessage.clear();
        }
    });

The other class is my class where all the chat logic is happening, it implements Runnable and has a run() method which (currently) does nothing. I tried to do this in a few ways but I just can't seem to get a fix for this.

Each instance of your program needs a way to publish messages to other instances (peers), and also receive messages from its peers.

You can do this either with multicast or with TCP.

If you use TCP, you need to have some mechanism for discovering peers so that instance A can connect to the host+port of instance B, for example. In this scenario your program would use a ServerSocket to listen for incoming connections.

If you use multicast, you can publish your chat messages to a multicast group. Your process can subscribe to traffic on that group and write the messages to the local file. There is the possibility that multicast datagrams get lost, but that may be fine for your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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