C# 客户端/服务器套接字数据丢失
我试图用MSDN()中的示例编写自己的应用程序 任务是向服务器发送一个对象,并获取它收到的反馈。我有一个表单,我点击按钮将信息发送到服务器。我的问题是,如果我启动服务器和客户端,我单击按钮-我发送一个对象并从服务器获得反馈,如果我再次单击按钮,有时它会工作,有时我会得到:C# 客户端/服务器套接字数据丢失,c#,sockets,C#,Sockets,我试图用MSDN()中的示例编写自己的应用程序 任务是向服务器发送一个对象,并获取它收到的反馈。我有一个表单,我点击按钮将信息发送到服务器。我的问题是,如果我启动服务器和客户端,我单击按钮-我发送一个对象并从服务器获得反馈,如果我再次单击按钮,有时它会工作,有时我会得到: Sent 595 bytes to server. Response received : Mustermann Exception thrown: 'System.ObjectDisposedException' in Sy
Sent 595 bytes to server.
Response received : Mustermann
Exception thrown: 'System.ObjectDisposedException' in System.dll
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult, SocketError& errorCode)
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at Zakinator.AsynchronousClient.ReceiveCallback(IAsyncResult ar) in C:\Data\1_Progs\Me\Zakinator\Zakinator\Client.cs:line 144
在带有按钮的窗体上,单击“我仅从客户端StartClient调用函数”(Communicator Communicator,WriteLog WriteLog)
我的客户代码:
namespace Zakinator
{
public delegate void WriteLog(string msg);
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
private static WriteLog wl;
// The port number for the remote device.
private const int port = 11000;
// 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 String response = String.Empty;
public static void StartClient(Communicator communicator,WriteLog writeLog)
{
wl = writeLog;
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, communicator);
//sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Debug.Print(String.Format("Response received : {0}", response));
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
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);
Debug.Print(String.Format("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.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Debug.Print(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)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));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Debug.Print(e.ToString());
}
}
private static void Send(Socket client, byte[] byteData)
{
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private static void Send(Socket client, Communicator data)
{
byte[] byteData;
MemoryStream ms = new MemoryStream();
BinaryFormatter bf1 = new BinaryFormatter();
bf1.Serialize(ms, data);
byteData = ms.ToArray();
Send(client, BitConverter.GetBytes(byteData.Length));
sendDone.WaitOne();
Send(client, byteData);
sendDone.WaitOne();
}
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);
Debug.Print(String.Format("Sent {0} bytes to server.", bytesSent));
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Debug.Print(e.ToString());
}
}
}
}
我的服务器代码:
namespace ServerZakinator
{
// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
// incoming image size
public int ObjectSize;
}
public class AsynchronousSocketListener
{
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
private static System.Timers.Timer SentCommunicatorToInfo = new System.Timers.Timer();
private static byte[] _objectBuff;
private static int _totBytesRead = 0;
public static int Port=11000;
public static string Host = "127.0.0.1";
private static List<Communicator> CommunicatorsList = new List<Communicator>();
public delegate void DataReceived(Communicator communicator);
public static event DataReceived NewData;
protected static void SendToWinwow(Communicator communicator)
{
CommunicatorsList.Add(communicator);
}
public AsynchronousSocketListener()
{
}
public static void StartListening()
{
//SetUp Timer
SentCommunicatorToInfo.Interval = 1000;
SentCommunicatorToInfo.Enabled = true;
SentCommunicatorToInfo.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.GetHostEntry(Host);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(Host), Port);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
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();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if (NewData != null)
{
var item = CommunicatorsList.FirstOrDefault();
if (item == null)
return;
NewData(item);
CommunicatorsList.Remove(item);
}
}
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.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadHeaderCallback), state);
}
public static void ReadHeaderCallback(IAsyncResult ar)
{
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
//we need to add error handler here...but later
state.ObjectSize = BitConverter.ToInt32(state.buffer, 0);
_objectBuff = new byte[state.ObjectSize];
_totBytesRead = 0;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 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.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
Buffer.BlockCopy(state.buffer, 0, _objectBuff, _totBytesRead, bytesRead);
_totBytesRead += bytesRead;
if (_totBytesRead < state.ObjectSize)
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{
MemoryStream ms = new MemoryStream(_objectBuff);
BinaryFormatter bf1 = new BinaryFormatter();
ms.Position = 0;
object rawObj = bf1.Deserialize(ms);
Communicator msgObj = (Communicator)rawObj;
SendToWinwow(msgObj);
Send(handler, msgObj);
}
}
}
private static void Send(Socket handler, Communicator communicator)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(communicator.Patient.Name);
// 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);
Debug.Print("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
名称空间服务器Zakinator
{
//用于异步读取客户端数据的状态对象
公共类状态对象
{
//客户端套接字。
公共套接字工作组=null;
//接收缓冲区的大小。
public const int BufferSize=1024;
//接收缓冲区。
公共字节[]缓冲区=新字节[BufferSize];
//接收到的数据字符串。
公共StringBuilder sb=新StringBuilder();
//传入图像大小
公共int对象大小;
}
公共类异步SocketListener
{
//线程信号。
public static ManualResetEvent allDone=新的ManualResetEvent(false);
private static System.Timers.Timer SentCommunicatorToInfo=新系统.Timers.Timer();
私有静态字节[]\u objectBuff;
私有静态int_totBytesRead=0;
公共静态int端口=11000;
公共静态字符串Host=“127.0.0.1”;
私有静态列表通讯器列表=新列表();
收到公共代表无效数据(通讯器);
公共静态事件数据接收到新数据;
受保护的静态无效SendToWinwow(通信器)
{
通讯器列表。添加(通讯器);
}
公共异步SocketListener()
{
}
公共静态侦听()
{
//设置计时器
SentCommunicator或ToInfo.间隔=1000;
SentCommunicationorToInfo.Enabled=真;
SentCommunicatorToInfo.Appead+=新的ElapsedEventHandler(OnTimedEvent);
//输入数据的数据缓冲区。
字节[]字节=新字节[1024];
//为套接字建立本地端点。
//计算机的DNS名称
//运行侦听器的是“host.contoso.com”。
IPHostEntry ipHostInfo=Dns.GetHostEntry(主机);
IPAddress IPAddress=ipHostInfo.AddressList[0];
IPEndPoint localEndPoint=新的IPEndPoint(IPAddress.Parse(主机),端口);
//创建TCP/IP套接字。
套接字侦听器=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
//将套接字绑定到本地端点并侦听传入连接。
尝试
{
Bind(localEndPoint);
听,听(100);
while(true)
{
//将事件设置为非信号状态。
全部完成。重置();
//启动异步套接字以侦听连接。
Console.WriteLine(“等待连接…”);
beginacept(新的异步回调(AcceptCallback),listener);
//等待连接完成后再继续。
全部完成。WaitOne();
}
}
捕获(例外e)
{
Console.WriteLine(如ToString());
}
Console.WriteLine(“\n按ENTER继续…”);
Console.Read();
}
私有静态void OnTimedEvent(对象源,ElapsedEventArgs e)
{
if(NewData!=null)
{
var item=CommunicatorsList.FirstOrDefault();
如果(项==null)
返回;
新数据(项目);
通讯器列表。删除(项目);
}
}
公共静态无效接受回调(IAsyncResult ar)
{
//向主线程发出继续的信号。
allDone.Set();
//获取处理客户端请求的套接字。
套接字侦听器=(套接字)ar.AsyncState;
套接字处理程序=listener.EndAccept(ar);
//创建状态对象。
StateObject状态=新的StateObject();
state.workSocket=处理程序;
BeginReceive(state.buffer,0,StateObject.BufferSize,0,新异步回调(ReadHeaderCallback),state);
}
公共静态无效ReadHeaderCallback(IAsyncResult ar)
{
//检索状态对象和处理程序套接字
//从异步状态对象。
StateObject状态=(StateObject)ar.AsyncState;
套接字处理程序=state.workSocket;
//从客户端套接字读取数据。
int bytesRead=handler.EndReceive(ar);
//我们需要在这里添加错误处理程序…但稍后
state.ObjectSize=BitConverter.ToInt32(state.buffer,0);
_objectBuff=新字节[state.ObjectSize];
_totBytesRead=0;
handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
新的异步回调(ReadCallback),状态);
}
公共静态void ReadCallback(IAsyncResult ar)
{
String content=String.Empty;
//检索状态对象和处理程序套接字
//从异步状态对象。
StateObject状态=(StateObject)ar.AsyncState;
套接字处理程序=state.workSocket;
//从客户端套接字读取数据。
int bytesRead=handler.EndReceive(ar);
如果(字节读取>0)
{
//可能会有更多数据,因此请存储这些数据
Receive(client);