Sockets 环传输中的异步套接字

Sockets 环传输中的异步套接字,sockets,asynchronous,Sockets,Asynchronous,我试图写一个程序,它把几台机器组合在一起。在那之后,我会在这枚戒指周围发送令牌。我有一个问题:当标记器绕着戒指转了一圈,我想第二次发送时,机器不想接受它。有时会出现异常,有点像“…主机强行关闭了连接”。在此之前,使用异步套接字时不会出现此问题。。。 我猜,监听插座的问题,解决了一次,就解决了。但是我怎样才能修复它呢? 提前感谢你的帮助。 函数,用于初始化计算机: private void connectButton_Click(object sender, EventArgs e) {

我试图写一个程序,它把几台机器组合在一起。在那之后,我会在这枚戒指周围发送令牌。我有一个问题:当标记器绕着戒指转了一圈,我想第二次发送时,机器不想接受它。有时会出现异常,有点像“…主机强行关闭了连接”。在此之前,使用异步套接字时不会出现此问题。。。 我猜,监听插座的问题,解决了一次,就解决了。但是我怎样才能修复它呢? 提前感谢你的帮助。 函数,用于初始化计算机:

private void connectButton_Click(object sender, EventArgs e)
    {
        if (!connected)
        {
            //some operation
        }
        else 
        {
            //some operation
        }
        try
        {
            sendS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            recieveS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            portFrom = int.Parse(port1TextBox.Text);
            portTo = int.Parse(port2TextBox.Text);
        }
        catch (Exception)
        {
            port1TextBox.Text = "Incorrect!";
        }

        IPAddress ipAddressFrom = IPAddress.Parse("192.168.1.122");
        IPEndPoint ipEnd = new IPEndPoint(ipAddressFrom, portFrom); //IPAddress.Any

        sendS.Bind(ipEnd);
        sendS.Listen(1);
        sendS.BeginAccept(new AsyncCallback(accept), null);
    }
    public void accept(IAsyncResult asyn)
    {
        Socket sock = sendS.EndAccept(asyn);
        // Let the worker Socket do the further processing for the just connected client
        begin(sock);

        // Since the main Socket is now free, it can go back and wait for
        // other clients who are attempting to connect
        sendS.BeginAccept(new AsyncCallback(accept), null);
    }

    public class SocketPacket
    {
        public Socket m_currentSocket;
        public byte[] dataBuffer;

        // Constructor that takes one argument. 
        public SocketPacket(int size)
        {
            dataBuffer = new byte[size];
        }
    }

    public void begin(Socket s)
    {
        AsyncCallback pfnWorkerCallBack = new AsyncCallback(serialPort1_DataReceived);
        SocketPacket theSocPkt = new SocketPacket(15);
        theSocPkt.m_currentSocket = s;
        s.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
    }

    private void serialPort1_DataReceived(IAsyncResult asyn)
    {
        SocketPacket socketData = (SocketPacket)asyn.AsyncState;
        int iRx = socketData.m_currentSocket.EndReceive(asyn);
        byte[] dataBytes = socketData.dataBuffer.ToArray();
        for (int i = dataBytes.Length - 1; i > 0; i--)
        {
            if (dataBytes[i] != 0)
            {
                Array.Resize(ref dataBytes, i+1);
                break;
            }
        }

        Encoding enc = Encoding.GetEncoding(1251);
        String text = enc.GetString(dataBytes);
        analyze(text);            
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (connected)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipTo.Text), System.Convert.ToInt32(portTo));
            recieveS.Connect(ip);
        }
    }
功能,连接两台机器:

private void connectButton_Click(object sender, EventArgs e)
    {
        if (!connected)
        {
            //some operation
        }
        else 
        {
            //some operation
        }
        try
        {
            sendS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            recieveS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            portFrom = int.Parse(port1TextBox.Text);
            portTo = int.Parse(port2TextBox.Text);
        }
        catch (Exception)
        {
            port1TextBox.Text = "Incorrect!";
        }

        IPAddress ipAddressFrom = IPAddress.Parse("192.168.1.122");
        IPEndPoint ipEnd = new IPEndPoint(ipAddressFrom, portFrom); //IPAddress.Any

        sendS.Bind(ipEnd);
        sendS.Listen(1);
        sendS.BeginAccept(new AsyncCallback(accept), null);
    }
    public void accept(IAsyncResult asyn)
    {
        Socket sock = sendS.EndAccept(asyn);
        // Let the worker Socket do the further processing for the just connected client
        begin(sock);

        // Since the main Socket is now free, it can go back and wait for
        // other clients who are attempting to connect
        sendS.BeginAccept(new AsyncCallback(accept), null);
    }

    public class SocketPacket
    {
        public Socket m_currentSocket;
        public byte[] dataBuffer;

        // Constructor that takes one argument. 
        public SocketPacket(int size)
        {
            dataBuffer = new byte[size];
        }
    }

    public void begin(Socket s)
    {
        AsyncCallback pfnWorkerCallBack = new AsyncCallback(serialPort1_DataReceived);
        SocketPacket theSocPkt = new SocketPacket(15);
        theSocPkt.m_currentSocket = s;
        s.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
    }

    private void serialPort1_DataReceived(IAsyncResult asyn)
    {
        SocketPacket socketData = (SocketPacket)asyn.AsyncState;
        int iRx = socketData.m_currentSocket.EndReceive(asyn);
        byte[] dataBytes = socketData.dataBuffer.ToArray();
        for (int i = dataBytes.Length - 1; i > 0; i--)
        {
            if (dataBytes[i] != 0)
            {
                Array.Resize(ref dataBytes, i+1);
                break;
            }
        }

        Encoding enc = Encoding.GetEncoding(1251);
        String text = enc.GetString(dataBytes);
        analyze(text);            
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (connected)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipTo.Text), System.Convert.ToInt32(portTo));
            recieveS.Connect(ip);
        }
    }

嗯。解决了的。我忘了绳子

begin(socketData.m_currentSocket);
在serialPort1_DataReceived函数的末尾