Socket using. Data is not sending on the second time

95 Views Asked by At

Issue description. I sending and receiving data. When i launch program, data who i send and receive, displayed is right. If relaunch program in 1 minutes, no more, data send and receive displayed is currect.

BUT! IF i relaunching program more then after 2 minutes, FIRST receiving and sending displayed is right, BUT SECOND sending and receiving data, is not displayed (((

What's happening, and how i fix this??? Thanks for any help!

public override bool Connect()
    {
        try
        {
            if (NetClient != null)
            {
                NetClient.Close();
            }

            NetClient = new Socket(ConnectionPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            IAsyncResult result = NetClient.BeginConnect(ConnectionPoint, null, null);
            bool success = result.AsyncWaitHandle.WaitOne(connectionDelay, false/*true*/);

            if (NetClient.Connected)
            {
                Log("report.log", string.Format("Connection OK"));
                return true;
            }

            Log("report.log", string.Format("NetClient is not connected"));
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            Log("report.log", string.Format("NetClient error while connecting: {0}", e.Message));
        }

        return false;
    }

    public override bool Send(byte[] data)
    {
        try
        {
            if (NetClient.Connected)
            {
                NetClient.SendTo(data, ConnectionPoint);
                Thread.Sleep(transferDelay);

                return true;
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }

        return false;
    }

    public override string Receive(int length)
    {
        byte[] data = new byte[bufferSize]; // bufferSize = i tryed any buffer from 5 bytes to 16384 bytes
        string strbuff = "";
        try
        {
            if (NetClient.Connected)
            {
                do
                {
                    IAsyncResult result = NetClient.BeginReceive(data, 0, bufferSize, SocketFlags.None, null, null);
                    bool success = result.AsyncWaitHandle.WaitOne(connectionDelay, true);

                    int receivedBytesCount = NetClient.EndReceive(result);

                    if (receivedBytesCount == 0)
                    {
                        receivedBytesCount = 0;
                    }

                    strbuff += Encoding.ASCII.GetString(data, 0, receivedBytesCount);
                } while (NetClient.Available > 0);

                if (NetClient != null && NetClient.Connected)
                {
                    NetClient.Shutdown(SocketShutdown.Both);
                    NetClient.Close();
                }
                return strbuff;
            }
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return null;
    }

    public override void Disconnect()
    {
        try
        {
            if (NetClient.Connected)
            {
                NetClient.Shutdown(SocketShutdown.Both);
                NetClient.Close();
            }
            else
            {
                if (NetClient != null)
                {
                    NetClient.Close();
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
0

There are 0 best solutions below