收发是不用互斥的,因为SOCKET是全双工的,只要注意不要多个线程同时进到收或者发,这个得自己保证,比如通过收发队列来进行。再一个就是使用这个SOCKET的某一个线程发现SOCKET挂了,在重建之前要通知所有使用的该SOCKET的线程统一退出或其它的错误处理。
解决方案 »
- winform 从数据库 保存图片到本地
- 能不能实现用正则表达式替换for为foreach
- 求C#实现文本TXT文件导入SQL时加一个WHILE循环
- 怎样从控件反过来得到ITemplate
- sqlconnection对象
- 如何判断某个文件是否已经打开
- ListView 问题,如何在ListView中实现中文排序?
- 如何用c#读出win2000的账号?
- 请教几个问题(来者有分)
- 对自定义自定义类该如何使用DataContractSerializer进行序列化和反序列化?
- 怎么在TreeNode.Tag里设置字符串?在代码里面的,不是设计器里
- 自定义Web控件属性(FileName),如何实现选取一个文件的路径?(结贴王在线)
using System.Data;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;namespace VIPComm
{
public class clsVIPComm
{
public const int TCP_Msg_Length = 65535;
public const int UDP_Msg_Length = 9500;
public static int Msg_Length;
//Thread thread_send = new Thread(new ThreadStart(TCPAsyncComm.send));
Thread thread_receive = new Thread(new ThreadStart(TCPAsyncComm.receive)); public clsVIPComm()
{
Msg_Length = TCP_Msg_Length;
Initial.setds();
VIPComm_Start();
} ~clsVIPComm()
{
VIPComm_Stop();
thread_send = null;
thread_receive = null;
} public void VIPComm_Start()
{
thread_send.Start();
thread_receive.Start();
} public void VIPComm_Pause()
{
thread_send.Start();
thread_receive.Start();
} public void VIPComm_Resume()
{
thread_send.Resume();
thread_receive.Resume();
} public void VIPComm_Stop()
{
thread_send.Abort();
thread_receive.Abort();
} public class TCPAsyncComm
{
public static string theResponse=null;
public static byte[] buffer = new byte[Msg_Length]; public static ManualResetEvent socketEvent = new ManualResetEvent(false);
public static Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static IPEndPoint EPServer; public static void send(string data)
{
try
{
byte[] byteData=null; if (data!=((char)16).ToString())
{
byteData = Encoding.ASCII.GetBytes(data+(char)3);
}
else
{
byteData = Encoding.ASCII.GetBytes(data);
} if (!sClient.Connected)
{
//IPAddress ipAddr = IPAddress.Parse(Initial.ds.Tables["L3ServerInfo"].Rows[0]["IPAddress"].ToString());
//int Port = Convert.ToInt32(Initial.ds.Tables["L3ServerInfo"].Rows[0]["Port"].ToString());
//EPServer = new IPEndPoint(ipAddr, Port);
//Debug.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")+" "+"Connect begining......");
sClient.BeginConnect(new IPEndPoint(IPAddress.Parse("192.16.10.1"), 5001), new AsyncCallback(ConnectCallback),sClient);
//Debug.WriteLine(AppDomain.GetCurrentThreadId()+"||1--Level 3 Server connection is broken, waiting for Level 3 Server connection......");
socketEvent.WaitOne();
}
sClient.BeginSend(byteData,0,byteData.Length,0,new AsyncCallback(SendCallback),sClient);
socketEvent.WaitOne();
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
socketEvent.Set();
return;
}
} public static void send(byte[] data)
{
try
{
//if connect is not ok, need connect to level 1 controller first
if (!sClient.Connected)
{
sClient.BeginConnect(new IPEndPoint(IPAddress.Parse("192.16.10.1"), 5001),
new AsyncCallback(ConnectCallback),sClient);
socketEvent.WaitOne();
}
sClient.BeginSend(data,0,data.Length,0,new AsyncCallback(SendCallback),sClient);
socketEvent.WaitOne();
}
catch (Exception e)
{
//Connect is failed, so return, wait next send again.
Debug.WriteLine(e.ToString());
socketEvent.Set();
return;
}
}
public static void receive()
{
try
{
if (!sClient.Connected)
{
sClient.BeginConnect(new IPEndPoint(IPAddress.Parse("192.16.10.1"), 5001),
new AsyncCallback(ConnectCallback),sClient);
socketEvent.WaitOne();
}
sClient.BeginReceive(buffer,0,buffer.Length,0,new AsyncCallback(ReceiveCallback),sClient);
socketEvent.WaitOne();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
socketEvent.Set();
return;
}
} public static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket sClient = (Socket)ar.AsyncState;
sClient.EndConnect(ar);
socketEvent.Set();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
sClient= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketEvent.Set();
}
} public static void SendCallback(IAsyncResult ar)
{
try
{
Socket sClient = (Socket)ar.AsyncState;
int bytesSent = sClient.EndSend(ar);
socketEvent.Set();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
socketEvent.Set();
}
} public static void ReceiveCallback(IAsyncResult ar)
{
try
{
//Debug.WriteLine("ConnectCallback Thread State:" + AppDomain.GetCurrentThreadId());
Socket sClient = (Socket)ar.AsyncState;
int bytesRead = sClient.EndReceive(ar);//,i=0;
if (bytesRead>0)
{
DelegateReceive delegatereceive = new DelegateReceive();
delegatereceive.receiveBytes = buffer;
Thread delegatereceivethread = new Thread(new ThreadStart(delegatereceive.DecodeMethod));
//Debug.WriteLine("--"+AppDomain.GetCurrentThreadId()+"--Thread Start===============");
delegatereceivethread.Start();
//Debug.WriteLine("--"+AppDomain.GetCurrentThreadId()+"--Thread Stop===============");
sClient.BeginReceive(buffer,0,buffer.Length,0,new AsyncCallback(ReceiveCallback),sClient);
}
else
{
Debug.WriteLine("=======Finished==========");
socketEvent.Set();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
socketEvent.Set();
}
}
}