因此,我正在使用C#中的Tcp协议,并认为可以连接到服务器。
当我连接到服务器时,它会通知我。但是然后我去检查ReceivedBufferSize就是这个receivedBuffer={byte[65536]}
实际上,它只返回几个字节。
为什么会这样? 将其发送回客户也没有任何作用,因此我删除了该部分。 我想是因为包太大了。
这部分在这里
byte[] receivedBuffer = new byte[client.ReceiveBufferSize];
返回receivedBuffer={byte[65536]}
public partial class MainWindow : Window
{
public static IPAddress remoteAddress = IPAddress.Parse("127.0.0.1");
public TcpListener remoteServer = new TcpListener(remoteAddress, 7777);
public TcpClient client = default(TcpClient);
public MainWindow()
{
InitializeComponent();
}
private void BtnListen_OnClick(object sender, RoutedEventArgs e)
{
if (StartServer())
{
client = remoteServer.AcceptTcpClient();
MessageBox.Show("Connected");
byte[] receivedBuffer = new byte[client.ReceiveBufferSize];
NetworkStream clientStream = client.GetStream();
while (client.Connected)
{
if (client.Connected)
{
if (client.ReceiveBufferSize > 0)
{
receivedBuffer = new byte[100];
clientStream.Read(receivedBuffer, 0, receivedBuffer.Length);
}
}
}
}
}
private bool StartServer()
{
try
{
remoteServer.Start();
MessageBox.Show("Server Started...");
return true;
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
throw;
}
}
}