简体   繁体   English

线程套接字处理程序,用于在TCP端口中进行双向通信

[英]Threading socket handler for two sided communication in tcp port

I want to make a chat application which will be written in java. 我想制作一个将用Java编写的聊天应用程序。

One computer will host the server and the other one will initiate the socket [tcp port]. 一台计算机将托管服务器,另一台计算机将启动套接字[tcp端口]。

From what I read there should be a loop that will constantly read the socket which means it will make the code is stuck. 从我读到的内容来看,应该有一个循环会不断读取套接字,这意味着它将使代码卡住。

I have a button that is 'actionperformed' on mouse release, I want to know if it will work along with the loops that constantly reads the socket so that it will also send the infromation I wrote. 我有一个在释放鼠标时“动作执行”的按钮,我想知道它是否可以与不断读取套接字的循环一起使用,以便它也可以发送我编写的信息。

If I must thread it, I want to know if the run() method must be void because if I thread it it will mean creating a new class, and the whole GUI is one big class which includes a text area, and it's private. 如果必须对其进行线程化,我想知道run()方法是否必须为空,因为如果对它进行线程化将意味着创建一个新类,并且整个GUI都是一个大类,其中包括文本区域,并且是私有的。

Also how can I extract the information from the socket directly to the text area (lets say the textarea variable is called " chatOutput ")? 另外,我如何从套接字直接将信息提取到文本区域(可以说textarea变量称为“ chatOutput ”)?

I presume that this is your situation: 我认为这是您的情况:

You have a GUI with a text area for typing in chat messages, and a button that will submit new messages to the chat room. 您有一个GUI,该GUI带有用于输入聊天消息的文本区域,以及一个将新消息提交到聊天室的按钮。 You want to keep some panel updated with the stream of chat messages by looping on the Socket's InputStream, but also want to be able to write to the Socket's OutputStream while the InputStream is being looped upon. 您希望通过在Socket的InputStream上循环来保持某些面板与聊天消息流的更新,而且还希望能够在InputStream被循环时写入套接字的OutputStream。

This would be my suggestion for the client side code: 这是我对客户端代码的建议:

Socket con = new Socket(host, port);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
PrintWriter out = new PrintWriter(con.getOutputStream(), true);
...
Thread t = new Thread(new Runnable() {
    public void run() {
        while(con.isConnected()) {
            String line = in.readLine();
            // update your chat panel with line
        }
    }
});
t.start();
...

And in your action: 在您的行动中:

...
out.println(/*contents of the text box*/);
// clear contents of text box
// ?update panel?
...

Your question is perhaps a bit vague, but I think the answer to your first question is no,it won't, your infinite while will interfere with normal operation and prevent the event dispatching thread from servicing your listeners. 您的问题可能有点含糊,但我认为您的第一个问题的答案是否定的,不会,您的无限时间会干扰正常操作并阻止事件分派线程为您的侦听器服务。 (I am not 100% sure about this but I am trying find out more so long). (对此我不是100%肯定,但是我想找出更多的时间)。

The second answer is yes, you need to thread your application as Finbarr suggests. 第二个答案是肯定的,您需要按照Finbarr的建议对应用程序进行线程化。 The run method is void, yes, as it is specified by the Runnable interface run方法是空的,是的,因为它由Runnable接口指定

Not quite sure what you meant here: 不太清楚您的意思是什么:

... I thread it it will mean creating a new class, and the whole GUI is one big class which includes a text area, and it's private. ...我将其线程化,这意味着创建一个新类,并且整个GUI是一个大类,其中包括一个文本区域,并且是私有的。 ... ...

Do you want to keep it one class, or what? 您要保持一堂课,还是什么? You can define private classes inside a class if you want to keep one .java file? 如果要保留一个.java文件,可以在一个类中定义私有类? for some reason. 由于某些原因。

For communication with your GUI, you can just use something such as a call like this inside your thread that reads the socket: 为了与GUI进行通信,您可以在读取套接字的线程内使用类似这样的调用:

java.awt.EventQueue.invokeLater(new Runnable() {
  public void run() {
     chatoutput.setText("output text text");
  }
});

You will need to create a class that implements the Runnable interface, and pass your 'chatoutput' variable to it in a constructor, so it can be used to call the above little piece of code. 您将需要创建一个实现Runnable接口的类,并在构造函数中将您的'chatoutput'变量传递给它,以便可以使用它来调用上面的小代码。

If you require more complex GUI interaction, see this idea here. 如果您需要更复杂的GUI交互,请参见此处。 GUI/application communication GUI /应用程序通信

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

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