gpt4 book ai didi

c# - 客户端刷新连接上的C#套接字ErrorCode 10054

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

我一直在尝试为我正在构建的应用程序编写一个使用TCP的通信层。我控制服务器和客户端的操作。

我遇到的问题是,当我使用“localhost”作为我的IP时,我没有任何问题。但是,当我使用互联网进行连接时,一切都神奇地……第一次。当客户端尝试建立与服务器的另一个套接字连接时,在ReadCallback期间,尤其是在执行socket.EndReceive操作时,服务器端会收到SocketException ErrorCode 10054。

我已经看过我的套接字未正确处理的可能性-可以肯定,因为我看不到客户端或服务器端应用程序中没有导致关闭的路径。

我的代码非常类似于MSDN中的代码,可以在here (server)here (client)中找到

这是我的客户代码的相关部分:

public class ClientStateObject
{
// Client socket.
public Socket workSocket = null;
// Receive buffer.
public byte[] buffer = new byte[Constants.ClientBuffer];
// Received data string.
public StringBuilder sb = new StringBuilder();

public List<byte[]> Data = new List<byte[]>();
}

public class AsynchronousClient
{

// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);

// The response from the remote device.
private static byte[] response = null;

public static object StartClient(object sendObj)
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is ...
IPHostEntry ipHostInfo = Dns.Resolve(Constants.Hostname); // localhost
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Constants.Port);

// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);



LingerOption lo = new LingerOption(false, 0);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);



// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

// Send test data to the remote device.
object[] s;
s = new object[2] { sendObj, "<EOF>" };
Send(client, s);
sendDone.WaitOne();

// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();

// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
//Console.ReadKey();


// Release the socket.
client.Shutdown(SocketShutdown.Both);

client.Close();


//object res = Deserialize(Deserialize(response) as byte[]);
object res = Deserialize(Deserialize(response) as byte[]);
return res;

}
catch (Exception e)
{
return e;
}
}

private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;

// Complete the connection.
client.EndConnect(ar);

Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());

// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void Receive(Socket client)
{
try
{
// Create the state object.
ClientStateObject state = new ClientStateObject();
state.workSocket = client;

// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, Constants.ClientBuffer, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
ClientStateObject state = (ClientStateObject)ar.AsyncState;
Socket client = state.workSocket;

// Read data from the remote device.
int bytesRead = client.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
state.Data.Add(state.buffer); // Concat byte[] to received packet

// Get the rest of the data.
client.BeginReceive(state.buffer, 0, Constants.ClientBuffer, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = ConcatByteListToArray(state.Data);
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static byte[] ConcatByteListToArray(List<byte[]> b)
{
List<byte> bList = new List<byte>();
foreach (byte[] bA in b)
{
foreach (byte by in bA)
{
bList.Add(by);
}
}

return bList.ToArray();
}

private static void Send(Socket client, object data)
{
// Convert the string data to byte data using ASCII encoding.

byte[] byteData = Serialize(data);

// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}

private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);

// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

/*
* Function: Serialize(object obj)
* Requires: Arbitrary object.
* Returns: MyMessage object, which constants a byte[] (Data) representing an object in TCP-friendly form.
* Description: Private function used to turn an object into a serialized byte[].
*/
private static byte[] Serialize(object obj)
{
using (var memoryStream = new MemoryStream())
{
(new BinaryFormatter()).Serialize(memoryStream, obj);
return memoryStream.ToArray();
}
}

/*
* Function: Deserialize(MyMessage message)
* Requires: MyMessage object with Data attribute.
* Returns: object which was originally serialized.
* Description: Private function used to reverse the process of "Serialize".
*/
private static object Deserialize(byte[] message)
{
using (var memoryStream = new MemoryStream(message))
{
return (new BinaryFormatter()).Deserialize(memoryStream);
}
}

这是服务器代码的相关部分:
public class ServerStateObject
{
// Client socket.
public Socket workSocket = null;
// Receive buffer.
public byte[] buffer = new byte[Constants.ServerBuffer];
// Received data string.
public StringBuilder sb = new StringBuilder();

public List<byte[]> Data = new List<byte[]>();
}

public class AsynchronousSocketListener
{
public static bool GetDataSuccess;
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);

public AsynchronousSocketListener()
{

}

public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[Constants.ServerBuffer];

// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Constants.Port);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

LingerOption lo = new LingerOption(false, 0);
listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lo);



// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);

while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();

// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);

// Wait until a connection is made before continuing.
allDone.WaitOne();

//Console.WriteLine("Done waiting for client.");
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("\nPress ENTER to continue...");
Console.Read();

}

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
ServerStateObject state = new ServerStateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, Constants.ServerBuffer, 0,
new AsyncCallback(ReadCallback), state);
}

public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;

// Retrieve the state object and the handler socket
// from the asynchronous state object.
ServerStateObject state = (ServerStateObject)ar.AsyncState;
Socket handler = state.workSocket;
Console.WriteLine("Read {0} bytes from {1} : {2}.",
content.Length, (handler.LocalEndPoint as IPEndPoint).Address, (handler.LocalEndPoint as IPEndPoint).Port);
int bytesRead = 0;
try
{
bytesRead = handler.EndReceive(ar);
}
catch (SocketException se)
{
Console.WriteLine("Socket Exception in Callback Read: {0}", se.ErrorCode); // I always get an error here on refresh
}

// Read data from the client socket.
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead)); // Check if there is an EOF in the byte[]
state.Data.Add(state.buffer); // Concat byte[] to received packet


// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
byte[] rArray = ConcatByteListToArray(state.Data);
byte[] sendArray = Serialize(HandleRead(rArray));
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from {1} : {2}.",
content.Length, (handler.LocalEndPoint as IPEndPoint).Address, (handler.LocalEndPoint as IPEndPoint).Port);

// Echo the data back to the client.
Send(handler, sendArray);
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, Constants.ServerBuffer, 0,
new AsyncCallback(ReadCallback), state);
}


}
}

private static void Send(Socket handler, object data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Serialize(data);

// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}

private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);

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

Console.WriteLine("Connection to client broken.");

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

我认为该问题可能与Windows处理套接字的方式有关,并且可能做得不够快。不幸的是,即使我在第一个连接和第二个连接之间等待了几分钟,代码也无法刷新。

最佳答案

好的,在我的代码中搜索了更多并阅读了错误消息之后,我发现客户端在进入“发送”之前抛出了错误代码10057。此错误表示套接字没有打开-即使套接字应该等待发送直到套接字打开。

因此,问题在于该事件在程序中每次运行的开始都没有重置。这在localhost上不是问题,因为异步套接字能够在到达send函数中需要打开的位置之前打开。但是,当端点不在本地时,它花费的时间太长,因此会出错。

我只是在“StartClient”的开头添加了三行代码:

            connectDone.Reset();
sendDone.Reset();
receiveDone.Reset();

这解决了整个问题。

所以...总是记住要设置您的初始变量条件,以免您看起来像个笨蛋!

关于c# - 客户端刷新连接上的C#套接字ErrorCode 10054,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145027/

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