java - 为什么我必须包含一个 Thread.sleep(10) 来获取要通过套接字发送的数据

标签 java android python sockets

我有以下通过套接字发送数据的代码:

套接字客户端.java

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient    
implements Runnable     
{
    private Socket socket;
    private String ServerIP = "192.168.0.11";
    private static final int ServerPort = 7000;

    @Override
    public void run()
    {
        try 
        {
            socket = new Socket(ServerIP, ServerPort);
        }
        catch(Exception e) 
        {
            System.out.print("Whoops! It didn't work on ip" + ServerIP + "!:");
            System.out.print(e.getLocalizedMessage());
            System.out.print("\n");
        }
    }

    public void Send(String s)
    {
        try
        {
            Thread.sleep(10);
            OutputStream out = socket.getOutputStream();  //Starts the output stream
            PrintWriter output = new PrintWriter(out);   
            output.println(s);  //sends the data over the socket
            output.flush(); //flushes the outputwriter
            output.close(); //closes the outputwriter
            out.close(); //closes the outputstream


        }
        catch (UnknownHostException e) {
            System.out.print(e.toString());
        } catch (IOException e) {
            System.out.print(e.toString());
        }catch (Exception e) {
            System.out.print(e.toString());
        }

    }
}

当我在发送函数中没有 sleep 时,服务器输出看起来像这样(我将它设置为打印每个连接的“conn”和“addr”),服务器是用 python 编码的

Connected with 192.168.0.11:52578
in client thread
Connected with 192.168.0.11:52579
in client thread
Connected with 192.168.0.11:52609
in client thread

服务器连接数据接收/主连接线程是这样的: def clientthread(conn):

#Sending message to connected client
#Receiving from client
data = conn.recv(4096)
print data
#came out of loop
conn.close()

我对服务器的目标是每次我想发送数据时打开/关闭客户端的套接字,因为我希望每个接收者使用我创建的套接字类创建自己的连接。

在 java 中通过 TCP 套接字发送字符串之前必须添加 thread.sleep() 的原因是什么?

此外,这就是我使用 Socketclient 类的方式:

    SMSClient = new SocketClient();
    Thread thread = new Thread(SMSClient);
    thread.start();
    SMSClient.Send(smsData);

最佳答案

当您实例化一个新的 SocketClient 对象时,您并没有运行新线程。您应该在 socket = new Socket(ServerIP, ServerPort); 之后从 run 方法中调用您的 Send(String s) 方法。 要了解您正在运行的代码中的当前线程,请输入如下日志:Log.d("label", "thread id: "+android.os.Process.myTid())。例如,尝试评估 run 方法内的当前线程,以及 Send(String s) 方法内的当前线程,当您正在执行此操作时以及在移动调用后调用后者到 run 中的方法。

我建议使用 IntentService为了您的目的,因为在需要时,您可以在单独的线程中轻松管理套接字连接和传输。

关于java - 为什么我必须包含一个 Thread.sleep(10) 来获取要通过套接字发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23933513/

相关文章:

java - 是否存在可以透明地构建请求但缺少的本地 Artifact 的 Maven 存储库?

java - 使用 Eclipse 显示解决错误的建议表的键盘快捷方式

基于 Java 的正则表达式允许字母数字字符和 ', 和

python - 如何在 python 中读取原始图像的数据 - 方法

javascript - 如何将 base64 编码的图像发送到 FastAPI 后端?

python - VS Code 中未安装 HuggingFace 变形金刚

java - 如何在netbeans中更改java数据库应用程序的标题

android - Google Analytics tracker.enable Exception Reporting(true) 不在logcat中显示异常

android - 在android模拟器上设置GPS位置

Android - AVD 管理器和 SDK 管理器未加载