TCP异步套接字抛出10057 [英] TCP async sockets throwing 10057

查看:129
本文介绍了TCP异步套接字抛出10057的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了下面的code:

I've written the following code:

    public static void BeginListen( int port )
    {
        IPAddress address = IPAddress.Any;
        IPEndPoint endPoint = new IPEndPoint( address, port );

        m_Socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );

        m_Socket.Bind( endPoint );
        m_Socket.Listen( 8 );

        m_Socket.BeginAccept( new AsyncCallback( Accept ), null );
    }

    private static void Accept( IAsyncResult result )
    {
        Socket client = m_Socket.EndAccept( result );

        SocketData state = new SocketData( client, 32767 );

        m_Socket.BeginReceive( state.Buffer, 0, state.Buffer.Length, SocketFlags.None,
            new AsyncCallback( Receive ), state );

        m_Socket.BeginAccept( new AsyncCallback( Accept ), null );
    }

当一个客户端试图连接,则会引发错误的BeginReceive调用accept()方法:

When a client attempts to connect, an error is thrown at the BeginReceive call in Accept():

一个请求发送或接收数据是不允许因为套接字未连接,并且没有提供地址(使用一个sendto调用发送数据报套接字时)

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

是什么造成的?我只是似乎无法把它。

What's causing this? I just can't seem to place it.

感谢

推荐答案

您需要调用 BeginReceive 插槽 EndAccept (在你的情况客户端),而不是服务器插槽(在你的情况 m_Socket ):

You need to call BeginReceive on the Socket returned by EndAccept (in your case client), not the server Socket (in your case m_Socket):

Socket client = m_Socket.EndAccept( result );
// ...
client.BeginAccept( new AsyncCallback( Accept ), null );

这里有一个例子,它说明了 MSDN 的正确用法。

这篇关于TCP异步套接字抛出10057的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆