Socket.Receive()卡住了

时间:2014-04-04 13:50:05

标签: c# sockets

我在大约两年内错过了它并为服务器编写客户端后,正在练习我的C#。无论如何,问题是Socket.Receive()似乎被卡住(我的循环实际上没有通过,测试它)..这是我在Program.cs中的调用:

                byte[] buffer = new byte[7];
                hSocket.Receive(buffer, 0, buffer.Length, 500);
这里是我的APISocket.cs的片段
        public bool Connect(string ServerIP, int ServerPort, int Timeout)
        {
            TimeoutObject.Reset();
            SocketException = null;
            try
            {
                Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint IPEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort);
                object state = new object();
                Socket.BeginConnect(IPEndPoint, new AsyncCallback(CallBackMethod), state);

            if (TimeoutObject.WaitOne(Timeout, false))
            {
                if (IsConnected)
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }

        }
        catch (Exception Ex)
        {
            SocketException = Ex;
        }

        return false;
    }

    private void CallBackMethod(IAsyncResult AsyncResult)
    {
        try
        {
            IsConnected = false;

            if (Socket.Connected)
            {
                IsConnected = true;
            }
        }
        catch (Exception Ex)
        {
            IsConnected = false;
            SocketException = Ex;
        }
        finally
        {
            TimeoutObject.Set();
        }
    }



    public void Receive(byte[] buffer, int offset, int size, int timeout)
    {
        SocketException = null;
        int startTick = Environment.TickCount;
        int received = 0;

        do
        {
            if (Environment.TickCount > startTick + timeout)
            {
                SocketException = new Exception("Timeout.");
                return;
            }

            try
            {
                received += Socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
            }
            catch (SocketException Ex)
            {
                if (Ex.SocketErrorCode == SocketError.WouldBlock ||
                    Ex.SocketErrorCode == SocketError.IOPending ||
                    Ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                {
                    Thread.Sleep(30);
                }
                else
                {
                    SocketException = Ex;
                    return;
                }
            }
        } while (received < size);
    }

if (TimeoutObject.WaitOne(Timeout, false)) { if (IsConnected) return true; else return false; } else { return false; } } catch (Exception Ex) { SocketException = Ex; } return false; } private void CallBackMethod(IAsyncResult AsyncResult) { try { IsConnected = false; if (Socket.Connected) { IsConnected = true; } } catch (Exception Ex) { IsConnected = false; SocketException = Ex; } finally { TimeoutObject.Set(); } } public void Receive(byte[] buffer, int offset, int size, int timeout) { SocketException = null; int startTick = Environment.TickCount; int received = 0; do { if (Environment.TickCount > startTick + timeout) { SocketException = new Exception("Timeout."); return; } try { received += Socket.Receive(buffer, offset + received, size - received, SocketFlags.None); } catch (SocketException Ex) { if (Ex.SocketErrorCode == SocketError.WouldBlock || Ex.SocketErrorCode == SocketError.IOPending || Ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { Thread.Sleep(30); } else { SocketException = Ex; return; } } } while (received < size); }

我不知道我哪里出错了,任何帮助都会受到赞赏..

1 个答案:

答案 0 :(得分:0)

Socket.Receive()是一个阻止通话。如果没有可用数据,它将阻塞,直到有可读数据。请参阅MSDN

If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException.