TCP同步连续传输大量数据出现数据不一致现象,求解决方法。
有两个类TCPService与TCPClient,这是我自己封装的两个Socket类,实现的原理是通过多线程实现同步数据通信
第一个类TCPService:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using PDSCommon;
using System.Threading;
namespace BottomCommunicate
{
public class TCPService
{
Socket soc;
const string State = "State";
const string Next = "Next";
const string End = "End";
const string Again = "Again";
IPEndPoint ips = null;
List<Socket> listSocket = new List<Socket>();
private TCPService() { }
public TCPService(string ip, int port)
{
//IPEndPoint ips = null;
try
{
ips = new IPEndPoint(IPAddress.Parse(ip), port);
}
catch
{
MessageBox.Show("IP或端口错误!");
}
soc = new Socket(ips.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
public TCPService(IPEndPoint ip)
{
ips = ip;
soc = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
#region ITcpService 成员
void Listen()
{
soc.Bind(ips);
soc.Listen(100);
ThreadPool.QueueUserWorkItem(ListenThread,soc);
}
void ListenThread(object obj)
{
Socket s = (Socket)obj;
Socket target = s.Accept();//当等待客户端连接时会阻塞
AcceptEvent(target);//与客户端建立连接,通知用户
listSocket.Add(target);
}
public bool Accept()
{
try
{
Listen();
return true;
}
catch (Exception ex)
{
MessageBox.Show("<TCPService,Accept>:" + ex.Message);
return false;
}
}
public event acceptSocket AcceptSocket;
void AcceptEvent(Socket target)
{
if (AcceptSocket != null)
AcceptSocket(target);
}
public bool Send(Socket target, byte[] data)
{
return Send(target, data, SocketFlags.None);
}
public bool Send(Socket target, byte[] data, SocketFlags socketflags)
{
lock (new object())
{
try
{
StateObject stateobject = new StateObject();
stateobject.target = target;
stateobject.data = data;
stateobject.socketflags = socketflags;
ThreadPool.QueueUserWorkItem(SendData, stateobject);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
void SendData(object obj)
{
StateObject state = (StateObject)obj;
state.All = SendDegree(state.data);
state.target.Send(SendInfo(State));
bool ifend = false;
while (true)
{
byte[] info = new byte[CommonData.Size];//CommonData.Size = 1024;
state.target.Receive(info);
string infoData = ConversionControl.ReceiveConversion(info);
string[] array = infoData.Split(new string[] { "\0" }, StringSplitOptions.None);
switch (array[0])
{
//case State: break;
case Next: SendNextMethod(state); break;
case End: ifend = EndMethod(state); break;
}
if (ifend)
break;
}
}
#region 发送数据处理函数
int SendDegree(byte[] data)
{
//CommonData.Size = 1024
int count = data.Length / CommonData.Size;
if ((data.Length % CommonData.Size) > 0)
{
count++;
}
return count;
}
void SendNextMethod(StateObject state)//同步分段发送数据
{
if (state.currentCount < state.All)
{
if (state.currentCount + 1 != state.All)
state.target.Send(state.data, state.currentCount * CommonData.Size, CommonData.Size, state.socketflags);
else
if (state.data.Length > CommonData.Size)
state.target.Send(state.data, state.currentCount * CommonData.Size, state.data.Length - state.currentCount * CommonData.Size, state.socketflags);
else
state.target.Send(state.data,0,state.data.Length, state.socketflags);
state.currentCount++;
}
else
{
state.target.Send(SendInfo(End));
}
}
bool EndMethod(StateObject state)
{
state = null;
return true;
}
byte[] SendInfo(string info)//发送信号
{
byte[] by = new byte[CommonData.Size];
ConversionControl.SendConversion(info).CopyTo(by, 0);
return by;
}
#endregion
class StateObject
{
public Socket target;
public byte[] data;
public SocketFlags socketflags;
public int All;
public int currentCount;
}
#endregion
#region ITcpService 成员
public void Close()
{
for (int i = 0; i < listSocket.Count; i++)
{
listSocket[i].Shutdown(SocketShutdown.Both);
listSocket[i].Close();
}
soc.Close();
}
#endregion
#region IDisposable 成员
public void Dispose()
{
Close();
}
#endregion
}
}