C# tcp 套接字(networkstream.read 不适用于 8.1) [英] C# tcp socket (networkstream.read won't work with 8.1)

查看:37
本文介绍了C# tcp 套接字(networkstream.read 不适用于 8.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行 tcp 套接字的程序.它在 Windows 8.1 以下的任何东西上运行良好.我已经关闭了我的防火墙并设置了所有内容以允许此连接.

I've got a program that runs a tcp socket. It runs fine on anything below windows 8.1. I've already turned off my firewall and have set everything to allow this connection.

1.首先服务器运行在windows 8.1
2.客户端套接字确实连接.
3.客户端发送字符串login",触发初始交互.
- 该字符串确实被发送.
4.服务器设置networkstream.read()时连接失败;
- 我在调试器中没有看到任何东西,而且它在其他方面也能正常工作
-系统

1.First the server does run on windows 8.1
2.The client socket does connect.
3.The client sends a string "login", to trigger the initial interaction.
-that string does get sent.
4.The connection fails when the server setup the networkstream.read();
-I'm not seeing anything in debugger, and again it works fine on other
-systems

这是我设置服务器以进行侦听的方式.很传统.

here is how the I setup the server to listen. It's pretty traditional.

//倾听客户的声音

 public String listen() {

            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;


            NetworkStream networkStream = this.clientSocket.GetStream();//get client input


         This is the line that fails{
            networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);//read client info  } -end fail

            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);//pass client input to String
            dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));//parse to end
            //MessageBox.Show("data:" + dataFromClient);
            networkStream.Flush();
          //  networkStream.Close();
            MessageBox.Show("data:" + dataFromClient);
            return dataFromClient;
        }

推荐答案

使用 clientSocket.ReceiveBufferSize 创建缓冲区,因此更改:

create the buffer with clientSocket.ReceiveBufferSize so change:

byte[] bytesFrom = new byte[10025];

进入:

byte[] bytesFrom = new byte[clientSocket.ReceiveBufferSize];

end 从这个数组中获取缓冲区大小:

end get the buffersize from this array:

networkStream.Read(bytesFrom, 0, bytesFrom.Length);

这篇关于C# tcp 套接字(networkstream.read 不适用于 8.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆