Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C服务器仅在套接字关闭时接收成功_C#_Sockets - Fatal编程技术网

C# C服务器仅在套接字关闭时接收成功

C# C服务器仅在套接字关闭时接收成功,c#,sockets,C#,Sockets,安卓: btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub strMessage = textSend.getText().toString(); new Thread(sendThread).start();

安卓:

btnSend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        strMessage = textSend.getText().toString();
        new Thread(sendThread).start();
        }
});

Runnable sendThread = new Runnable() {      
    @Override
    public void run() {
        // TODO Auto-generated method stub
        byte[] sendBuffer = null;
        try {
            sendBuffer = strMessage.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            outStream = socket.getOutputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            outStream.write(sendBuffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};
C:


我认为您需要客户端的刷新流。若不调用flush方法,套接字将在关闭时激发

try {
           outStream.write(sendBuffer);
           outStream.flush();

     } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

恐怕这是一个质量很低的职位。除非您能向其他用户解释代码的作用,否则查看代码毫无意义。另外,了解您希望它做什么以及问题是什么也很有帮助。请查看并编辑您的问题,以提高质量。对不起,我没有经常这样做,但下次我会做得很好
try {
           outStream.write(sendBuffer);
           outStream.flush();

     } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }