gpt4 book ai didi

.net - .NET中的套接字损坏?

转载 作者:行者123 更新时间:2023-12-03 11:53:27 25 4
gpt4 key购买 nike

这是一个非常奇怪的问题:显然,转换为ASCII "PUttttt"的特定字节序列(如果通过TCP套接字发送)根本无法传递给客户端。

我已经包含了用于说明以下问题的示例代码,以期希望有人对为什么会发生这样的想法有所了解。服务器和客户端应用程序已被故意简化为简单的控制台同步版本,以使故障排除更加容易。

问题的关键方面是进入发送缓冲区(“PUttttt”字符串)的数据。

服务器:

using System;      
using System.Net.Sockets;
using System.Net;

namespace ConsoleServer
{
private static Socket mServer;
private static byte[] fileBytes;
private static int sent_total;

static void Main(string[] args)
{
string hostName = "localhost"; //Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
IPAddress iaddr = null;
for(int k = 0; k < ipHostInfo.AddressList.Length; k++)
if (ipHostInfo.AddressList[k].AddressFamily == AddressFamily.InterNetwork)
{
iaddr = ipHostInfo.AddressList[k];
break;
}

if (iaddr == null)
{
Console.WriteLine("Can not bind to any interface.. Server can not start");
Console.ReadKey();
return;
}

mServer = new Socket(iaddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
mServer.Bind(new IPEndPoint(iaddr, 8000));

Console.WriteLine("Server: Bound to " + hostName + " (" + iaddr.ToString() + "):8000");
mServer.Listen(1);

Console.WriteLine("Server: Started. Awaiting connection.");

Socket handler = mServer.Accept();
handler.LingerState = new LingerOption(true, 5);

Console.WriteLine("Server: A Client connected\r\nServer: sending data");

string str = @"PUttttt";
fileBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(str);

SocketError errCode = SocketError.Success;
int sent = handler.Send(fileBytes, 0, fileBytes.Length, SocketFlags.None, out errCode);
sent_total += sent;
Console.WriteLine("Server: Done sending. " + sent_total + " bytes (" + errCode.ToString() + ")");

handler.Close();

Console.WriteLine("Server: CLient Disconnected");

Console.ReadKey();
}
}
}

客户端:
using System;
using System.Net.Sockets;
using System.Net;

namespace ConsoleClient
{
class Program
{
private static int BUFFER_SIZE = 1024 * 1024;
private static string SERVER_ADDR = "localhost"; //Dns.GetHostName();
private static int SERVER_PORT = 8000;

static void Main(string[] args)
{
Console.WriteLine("Client: connecting to " + SERVER_ADDR + " server");

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveTimeout = 30000;
socket.ReceiveBufferSize = BUFFER_SIZE;
IPHostEntry ipHostInfo = Dns.GetHostEntry(SERVER_ADDR);

IPAddress ipAddress = null;
for (int k = 0; k < ipHostInfo.AddressList.Length; k++)
if (ipHostInfo.AddressList[k].AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ipHostInfo.AddressList[k];
break;
}

socket.Connect(new IPEndPoint(ipAddress, SERVER_PORT));
Console.WriteLine("Client: connected");

byte[] buffer = new byte[BUFFER_SIZE];
int total = 0;
int read = socket.Receive(buffer, buffer.Length, SocketFlags.None);
total += read;

Console.WriteLine("Client: read " + read + " bytes. " + total + " total");
while (read > 0)
{
try
{
read = socket.Receive(buffer, buffer.Length, SocketFlags.None);
total += read;

Console.WriteLine("Client: read " + read + " bytes. " + total + " total: ");
}
catch (Exception se)
{
Console.WriteLine(se.ToString());
break;
}
}

Console.WriteLine("Client: received " + total + " bytes");

socket.Shutdown(SocketShutdown.Both);
socket.Close();

Console.WriteLine("Connection Closed.");

Console.ReadKey();
}
}
}

我已经使用Visual Studio 2010和.NET 4客户端配置文件对此进行了编译,但是我怀疑它在其他版本的.NET中均已损坏。

这是服务器的输出:
Server: Bound to localhost (127.0.0.1):8000
Server: Started. Awaiting connection.
Server: A Client connected
Server: sending data
Server: Done sending. 7 bytes (Success)
Server: CLient Disconnected

这是客户端的输出
Client: connecting to localhost server
Client: connected
Client: read 0 bytes. 0 total
Client: received 0 bytes
Connection Closed.

注意从服务器发送的内容和由客户端接收的内容之间的区别。另外,如果关闭服务器上套接字的行已被注释掉,则客户端将挂起以等待接收数据。

最佳答案

似乎不太可能,但我可以确认此问题:我尝试在打开卡巴斯基反病毒软件的情况下运行它。但是,当我关闭它时,它工作正常。

关于.net - .NET中的套接字损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4000002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com