多个线程到同一个TCP套接字

本文关键字:TCP 套接字 同一个 线程 | 更新日期: 2023-09-27 18:08:50

我是。net开发人员,是套接字编程新手。

我用。net套接字库编写了一个程序,使用TCP套接字向客户端发送一些数据。

客户端需要每40秒自定义一条Keep Alive消息来保持连接的活动状态。

因此,我编写了与客户端建立连接的主程序。在这个主程序中,我创建了一个线程,并传递了之前创建的Socket Class的实例。这个线程负责向客户端发送keep alive消息,而主线程负责发送数据。

一切都很好。但是如果由于某种原因套接字连接超时,程序将永远无法恢复?我设置了线程退出和新连接建立的逻辑,但它总是会给出一个错误- '与主机的连接被终止'或类似的东西。

我做错了什么吗?

我需要两个线程连接到同一个套接字。一个线程负责发送数据,另一个线程负责发送keep alive消息。最好的方法是什么?

不,我没有尝试使用相同的套接字。我脱离了for循环和clntSock.close()…

代码如下:

我有主线程调用handleClient。handleClient创建另一个线程。

class DispatchLoop
{
    ILogger logger;
    TcpListener listener;
    IProtocolFactory protoFactory;
    public DispatchLoop(TcpListener listener, ILogger logger, IProtocolFactory protoFactory)
    {
        this.logger = logger;
        this.listener = listener;
        this.protoFactory = protoFactory;
    }
    public void mainThread()
    {
        // Run forever, accepting and handling each connection
        for (; ; )
        {
            try
            {
                Socket clntSock = listener.AcceptSocket(); // Block waiting for connection
                PoolDispatcher._stopper.Reset();
                clntSock.ReceiveTimeout = 10000;
                IProtocol protocol = protoFactory.createProtocol(clntSock, logger);
                protocol.handleClient();
            }
            catch (SocketException se)
            {
                logger.writeEntry("(Run Dispatcher) Exception = " + se.Message);
            }
        }
    }
}
    public void handleClient()
    {
        entry.Add("Main Thread Entered : Client address and Port = " + clntSock.RemoteEndPoint + ", Thread Number = " + Thread.CurrentThread.GetHashCode());
        //Kick Starting Keep Alive Thread
        KeepAlive ka = new KeepAlive(clntSock, logger);
        Thread thread = new Thread(new ThreadStart(ka.SendKeepAlive));
        thread.Start();
        try
        {
            int recvMsgSize; // Size of received message
            byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer
            byte[] messageBuffer = new byte[1024];
            XDocument _messageXDoc;
            FlightInfoExtended _flightInfoExtended;
            try
            {
                LogEntry(entry);
                for (; ; )
                {
                    try
                    {
                        //Read from the Queue 
                        var _queue = new IBMQueue();
                        var message = _queue.ReceiveMessage();
                        if (message.Length > 0)
                        {
                            entry.Add("Sending the GOS Message to the client : " + message);
                            messageBuffer = Encoding.ASCII.GetBytes(message);
                            if (clntSock.Connected)
                            {
                                clntSock.Send(messageBuffer, 0, messageBuffer.Length, SocketFlags.None);
                                recvMsgSize = clntSock.Receive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None);
                                SaveGOSMessage(_auditMessage);
                            }
                            else
                            {
                                PoolDispatcher._stopper.Set();
                                LogFailureStatus("No Socket Connection");
                                Thread.Sleep(30000);
                                break;
                            }
                        }
                    }
                    catch (SocketException se)
                    {
                        PoolDispatcher._stopper.Set();
                        LogFailureStatus(se.Message);
                        Thread.Sleep(30000);
                        break;
                    }
                    catch (Exception e)
                    {
                    }
                    LogEntry(entry);
                }
            }
            catch (Exception se)
            {
                entry.Add(String.Format("{0}: {1}", se.Source, se.Message));
            }
        }
        catch (Exception se)
        {
            entry.Add(String.Format("{0}: {1}", se.Source, se.Message));
        }
        clntSock.Close();
        logger.writeEntry(entry);
    }

public class KeepAlive
{
    ArrayList entry;
    private ILogger logger;
    private Socket clntSock;
    public const int BUFSIZE = 1024;
    public KeepAlive(Socket clntSock, ILogger logger)
    {
        this.logger = logger;
        this.clntSock = clntSock;
        entry = new ArrayList();
    }
    void LogEntry(ArrayList _entry)
    {
        logger.writeEntry(_entry);
        entry.Clear();
    }
    public void SendKeepAlive()
    {
        entry.Add("Keep Alive Thread Entered : Client address and Port = " + clntSock.RemoteEndPoint + ", Thread Number = " + Thread.CurrentThread.GetHashCode());
        var message= "Some Keep Alive Message";
        try
        {
            byte[] messageBuffer = new byte[1024];
            LogEntry(entry);
            for (; ; )
            {
                //Check if main thread died
                if ( PoolDispatcher._stopper.WaitOne(100, false))  
                {                                 
                    break;
                }
                if (clntSock.Connected)
                {
                    entry.Add("Sending the Keep Alive Message... " + message);
                    messageBuffer = Encoding.ASCII.GetBytes(message);
                    clntSock.Send(messageBuffer, 0, messageBuffer.Length, SocketFlags.None);
                }
                else
                {
                    entry.Add("Socket Connection is not active. Keep Alive not sent");
                    break;
                }
                LogEntry(entry);
                Thread.Sleep(30000);
            }
        }
        catch (SocketException se)
        {
            entry.Add(String.Format("{0}: {1}", se.ErrorCode, se.Message));
        }
        catch (ObjectDisposedException ode)
        {
            entry.Add("Connection to the socket lost. Child Thread Aborted");
        }
        LogEntry(entry);
    }
}

多个线程到同一个TCP套接字

方法很简单,只需将两个模块集成。让服务器和客户端在一个线程中,并为每个实例生成读写线程,每个套接字具有相同的IP地址(不是相同的端口)

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=VS.90%29.aspx,http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=VS.90%29.aspx

单独使用它,它工作完美。我完成了这个东西,希望它对你有帮助。