收发是不用互斥的,因为SOCKET是全双工的,只要注意不要多个线程同时进到收或者发,这个得自己保证,比如通过收发队列来进行。再一个就是使用这个SOCKET的某一个线程发现SOCKET挂了,在重建之前要通知所有使用的该SOCKET的线程统一退出或其它的错误处理。

解决方案 »

  1.   

    收发最好在一个进程完成,分成两个有什么意思呢你的发多是在收到信息的情况下变化的啊,建议还是把收发在一起SOCKET的建立缓冲为了避免冲突,是最好在消息发放和接受一个过程完成后就FFLSH一下的
      

  2.   

    using System;
    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();
    }
    }
    }
      

  3.   

    后面的代理涉及商业目的,所以不能公开,Sorry