socket语句后的代码不执行

时间:2021-04-25 23:56:47


currently I'm working on a project which need socket programming, but unfortuanatly when i use to call the socket statement and read something from the input stream of the socket or write something to it the application crash and nothing will work, in fact the problem is that the code which handles the action of buttons and some other things will not work, the execution will stop at the line which calls the method that create the reads the input stream of socket. I solved this problem some times ago with the thread and by putting the statement which works with the socket inside the run method. the UI works correctly but i still do not have the good functionality. here is the two line of calling the method

目前我正在开发一个需要套接字编程的项目,但是当我用来调用套接字语句并从套接字的输入流中读取内容或者写一些内容时,应用程序崩溃并且什么都行不通,实际上是问题是处理按钮操作的代码和其他一些东西不起作用,执行将停止在调用创建读取socket输入流的方法的行。我几次用线程解决了这个问题,并在run方法中放入了与套接字一起使用的语句。用户界面工作正常,但我仍然没有良好的功能。这是调用方法的两行

Conection_Manager cm = new Conection_Manager(jTextField1.getText());
jTextArea1.setText(cm.getMessage());

in the first line I use to call the method in my connection manager class there the data on the socket will be read correctly and I can see that by printing those data, but when in the next line I want to set the text area with the string every thing crash. if it makes sense I use those two lines of statement inside a Jdialog Thanks in advance

在第一行我用来调用我的连接管理器类中的方法,那里将正确读取套接字上的数据,我可以看到通过打印这些数据,但是在下一行中我想设置文本区域字符串每一个崩溃。如果有意义我在Jdialog中使用这两行语句提前谢谢

2 个解决方案

#1


0  

Probably it is blocking waiting for something to be written to the socket. That's why it appears crashed, when in fact it is just waiting for data. have you checked that data is actually being written to the socket?

可能它阻止等待写入套接字的东西。这就是它出现崩溃的原因,实际上它只是在等待数据。你检查过数据是否真正被写入套接字?

Also, Server Sockets block until a connection is received if you are doing everything in one thread (without an Executor, e.g)

此外,如果您在一个线程中执行所有操作(例如,没有Executor),服务器套接字会阻塞,直到收到连接

#2


0  

I am guessing that by crashing you mean it hangs. This is because you were trying to execute lengthy operations inside so called event dispatching thread, which handles all Swing events. Your solution with spawning a new thread for socket connectivity is correct, but instead of waiting for thread to finish (once again, blocking EDT), you need to update the GUI from the spawned thread.

我猜你崩溃就意味着它会挂起。这是因为您试图在所谓的事件调度线程中执行冗长的操作,该线程处理所有Swing事件。产生套接字连接的新线程的解决方案是正确的,但不是等待线程完成(再次阻止EDT),您需要从生成的线程更新GUI。

But be careful, this is tricky, as Swing is in general not thread-safe, which means you cannot simply update controls from other threads. Try SwingWorker and make sure you read about EDT, for instance: http://java.sun.com/developer/technicalArticles/Threads/swing.

但要小心,这很棘手,因为Swing通常不是线程安全的,这意味着你不能简单地从其他线程更新控件。试试SwingWorker并确保阅读有关EDT的内容,例如:http://java.sun.com/developer/technicalArticles/Threads/swing。

#1


0  

Probably it is blocking waiting for something to be written to the socket. That's why it appears crashed, when in fact it is just waiting for data. have you checked that data is actually being written to the socket?

可能它阻止等待写入套接字的东西。这就是它出现崩溃的原因,实际上它只是在等待数据。你检查过数据是否真正被写入套接字?

Also, Server Sockets block until a connection is received if you are doing everything in one thread (without an Executor, e.g)

此外,如果您在一个线程中执行所有操作(例如,没有Executor),服务器套接字会阻塞,直到收到连接

#2


0  

I am guessing that by crashing you mean it hangs. This is because you were trying to execute lengthy operations inside so called event dispatching thread, which handles all Swing events. Your solution with spawning a new thread for socket connectivity is correct, but instead of waiting for thread to finish (once again, blocking EDT), you need to update the GUI from the spawned thread.

我猜你崩溃就意味着它会挂起。这是因为您试图在所谓的事件调度线程中执行冗长的操作,该线程处理所有Swing事件。产生套接字连接的新线程的解决方案是正确的,但不是等待线程完成(再次阻止EDT),您需要从生成的线程更新GUI。

But be careful, this is tricky, as Swing is in general not thread-safe, which means you cannot simply update controls from other threads. Try SwingWorker and make sure you read about EDT, for instance: http://java.sun.com/developer/technicalArticles/Threads/swing.

但要小心,这很棘手,因为Swing通常不是线程安全的,这意味着你不能简单地从其他线程更新控件。试试SwingWorker并确保阅读有关EDT的内容,例如:http://java.sun.com/developer/technicalArticles/Threads/swing。