确保套接字连接已完成,然后将其丢弃

时间:2014-05-12 10:35:24

标签: c# sockets

我有一个服务器,它通过套接字获取字符串并对其进行操作。我想将一个字符串发送到服务器,读取答案,发送另一个字符串,然后等待第二个字符串上的答案,最后关闭事务。下面的代码是我实现这一目标的进展。当我发送第一个字符串时,我收到了回复,但是当我发送第二个字符串时,程序"冻结"以无尽的思维方式(不是实际的冻结)。

我发送给的服务器不是我的,所以我无法发布任何代码。我让它在本地运行。我期待的回复大约是50-60个字符。我怀疑应用程序永远不会完成,我想要做的是检查事务是否完成然后关闭它。由于第一个字符串正在工作,它的第二个字符串我想检查它是否完成。

class test {

private Socket m_Socket;
private TcpClient m_Client = new TcpClient();

public Test()
{
    InitializeComponent();

    m_Socket = m_Client.Client;
}

private void DoIt()
{
    // Connect
    if (!m_Socket.IsBound)
        m_Socket.Connect(txtHost.Text, Convert.ToInt32(txtPort.Text));

    // Send first string
    try
    {
        string str = "asdfpojfdiogj589068d9fyugui";
        Send(m_Socket, Encoding.UTF8.GetBytes(str), 0, str.length, 10000);
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

    //// Receive answer
    byte[] buffer = new byte[48];
    Receive(m_Socket, buffer, 0, buffer.Length, 10000);
    Console.WriteLine("Answer 1: "+Encoding.UTF8.GetString(buffer, 0, buffer.Length));
    buffer = new byte[64];

    // Send string 2
    string str2 = "jasuidhj8348957y3478538hdjuhduihdfugv09im2390i490gfifjgiojdfiogj4893574y890dgydufighduifh";
    Send(m_Socket, Encoding.UTF8.GetBytes(str2), 0, str2.Length, 10000);

    // Receive answer2
    buffer = new byte[str2.Length];
    Receive(m_Socket, buffer, 0, buffer.Length, 10000);
    Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, buffer.Length));
}

public void Send(Socket p_Socket, byte[] p_Buffer, int p_Offset, int p_Size, int p_Timeout)
{
        int startTickCount = Environment.TickCount;
        int sent = 0;
        do
        {
            if (Environment.TickCount > startTickCount + p_Timeout)
                throw new Exception("Timeout..");
            try
            {
                sent += p_Socket.Send(p_Buffer, p_Offset + sent, p_Size - sent, SocketFlags.None);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.WouldBlock ||
                    ex.SocketErrorCode == SocketError.IOPending ||
                    ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                {
                    // socket buffer is probably full, wait and try again
                    System.Threading.Thread.Sleep(30);
                }
                else
                    throw ex;
            }
        } while (sent < p_Size);
}

public string Receive(Socket p_Socket, byte[] p_Buffer, int p_Offset, int p_Size, int p_Timeout)
{
    int startTickCount = Environment.TickCount;
    int received = 0;
    do
    {
        if (Environment.TickCount > startTickCount + p_Timeout)
            //throw new Exception("Timeout.");
            return p_Buffer.ToString();
        try
        {
            received += p_Socket.Receive(p_Buffer, p_Offset + received, p_Size - received, SocketFlags.None);
        }
        catch (SocketException ex)
        {
            if (ex.SocketErrorCode == SocketError.WouldBlock ||
                ex.SocketErrorCode == SocketError.IOPending ||
                ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
            {
                // socket buffer is probably empty, wait and try again
                System.Threading.Thread.Sleep(30);
            }
            else
                throw ex;
        }
    } while (received < p_Size);
    return p_Buffer.ToString();
}
}

流程从方法DoIt()开始,该方法是从UI中的按钮调用的。

0 个答案:

没有答案