求各位指点一下啊,关于socket通信的:
Socket mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any , port);
mysocket.Bind(endpoint);
mysocket.Listen(25);
while (true)
{
try
{
Socket thesocket = mysocket.Accept();
MemoryStream mem = new MemoryStream();
mem.Position = 0;
byte[] r_data = new byte[4];
thesocket.Receive(r_data);
int size = BitConverter.ToInt32(r_data, 0);
int offset = 0;
if (size > 0)
{
while (size > 0)
{
byte[] data = new byte[1024];
thesocket.Receive(data, offset, 1024,SocketFlags.None);
int revc = data.GetLength(0);
mem.Write(data, offset, size);
offset += revc;
size -= revc;
}
}
BinaryFormatter formatter = new BinaryFormatter();
mem.Position = 0;
cell = (cell_perf.Cell)formatter.Deserialize(mem); //这句出错,提示为“二进制0不包含有效的binnaryheader,或版本更新。。。。”
thesocket.Shutdown(SocketShutdown.Both);
thesocket.Close();
}
catch (Exception e)
{
Exception ex = e;
}
Socket mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any , port);
mysocket.Bind(endpoint);
mysocket.Listen(25);
while (true)
{
try
{
Socket thesocket = mysocket.Accept();
MemoryStream mem = new MemoryStream();
mem.Position = 0;
byte[] r_data = new byte[4];
thesocket.Receive(r_data);
int size = BitConverter.ToInt32(r_data, 0);
int offset = 0;
if (size > 0)
{
while (size > 0)
{
byte[] data = new byte[1024];
thesocket.Receive(data, offset, 1024,SocketFlags.None);
int revc = data.GetLength(0);
mem.Write(data, offset, size);
offset += revc;
size -= revc;
}
}
BinaryFormatter formatter = new BinaryFormatter();
mem.Position = 0;
cell = (cell_perf.Cell)formatter.Deserialize(mem); //这句出错,提示为“二进制0不包含有效的binnaryheader,或版本更新。。。。”
thesocket.Shutdown(SocketShutdown.Both);
thesocket.Close();
}
catch (Exception e)
{
Exception ex = e;
}