BeginAccept不起作用 [英] BeginAccept doesn't work

查看:101
本文介绍了BeginAccept不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么BeginAccept不起作用?如果,在代码

下面,我做了正常的Accept函数我可以得到客户端套接字但是

似乎回调并没有被真正调用。 />
这里是代码:

-----------------------------

私有套接字套接字;

私有套接字clientSocket;


public TcpServer(string serverIP,int serverPort)

{

clientSocket = null;

socket = new

Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp) ;

IPEndPoint ep =新IPEndPoint(IPAddress.Parse(serverIP),serverPort);

socket.Bind(ep);

socket。听(10);

socket.BeginAccept(新的AsyncCallback(AcceptConnection),socket);

}


private void AcceptConnection (IAsyncResult ar)

{

套接字套接字=(套接字)ar.AsyncState;

clientSocket = socket.EndAccept(ar);

//抛出新的异常(> TCP接受

#" + cli entSocket.RemoteEndPoint.ToString());

抛出新的异常(" Yuhuu");

}

Does anyone know why the BeginAccept doesn''t work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn''t really called.
Here''s the code:
-----------------------------
private Socket socket;
private Socket clientSocket;

public TcpServer(string serverIP,int serverPort)
{
clientSocket=null;
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);
IPEndPoint ep=new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}

private void AcceptConnection(IAsyncResult ar)
{
Socket socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
//throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
throw new Exception("Yuhuu");
}

推荐答案

Nuno,


我在这段代码中没有看到错误。

但代码没有不显示serverIP和serverPort是什么。在调用socket.Listen检查socket后,我会给b $ b建议什么。连接以确保

所有套接字初始化都没问题。

第二是确保没有任何防火墙或防病毒程序

可能阻止访问侦听端口。


-
< br $>
Stoitcho Goutsev(100)

" Nuno Magalhaes" < NU ************ @ hotmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Nuno,

I don''t see the error in this code.
However the code doesn''t show what serverIP and serverPort are. What I''d
suggest after calling socket.Listen check socket.Connected to make sure that
all the socket initialization went OK.
Second is make sure that there are no any firewalls or antivirus programs
that may block the access to the listening port.

--

Stoitcho Goutsev (100)

"Nuno Magalhaes" <nu************@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
有谁知道为什么BeginAccept不起作用?如果,在下面的代码中,我执行正常的Accept函数,我可以得到客户端套接字,但似乎回调并没有被调用。
这里是代码:
-----------------------------
私有套接字套接字;
私有套接字clientSocket;

public TcpServer(string serverIP,int serverPort)
{/// clientSocket = null;
socket = new
Socket(AddressFamily.InterNetwork,SocketType.Strea) m,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(新的AsyncCallback(AcceptConnection),socket);

私有的void AcceptConnection(IAsyncResult ar)
{
套接字套接字=(套接字) )ar.AsyncState;
clientSocket = socket.EndAccept(ar);
//抛出新的异常(在#上接受TCP + + + clientSocket.RemoteEndPoint.ToString());
抛出新的异常(Yuhuu);
}
Does anyone know why the BeginAccept doesn''t work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn''t really called.
Here''s the code:
-----------------------------
private Socket socket;
private Socket clientSocket;

public TcpServer(string serverIP,int serverPort)
{
clientSocket=null;
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);
IPEndPoint ep=new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}

private void AcceptConnection(IAsyncResult ar)
{
Socket socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
//throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
throw new Exception("Yuhuu");
}



Nuno Magalhaes< nu ************ @ hotmail.com>写道:
Nuno Magalhaes <nu************@hotmail.com> wrote:
有谁知道为什么BeginAccept不起作用?如果,在下面的代码中,我执行正常的Accept函数,我可以得到客户端套接字,但似乎回调并没有真正被调用。
Does anyone know why the BeginAccept doesn''t work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn''t really called.




你能发一个简短但完整的程序来演示

的问题吗?


http://www.pobox.com/~skeet/csharp/complete.html 了解详细信息

我的意思是什么。


你还没有真正发布足够的代码供我们试用

我们自己。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复群组,请不要给我发邮件



Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You haven''t really posted enough of the code for us to try it for
ourselves.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


还有一条评论 - 抛出异常以发送自己的消息

可能是个好习惯!


Console.WriteLine或System.Diagnostics.Debug.WriteLine甚至

MessageBox.Show工作得很好。当它们被抛出时,例外是昂贵的,并且只有在出现问题时才应该使用。

Peter


-

联合创始人,Eggheadcafe.com开发者门户网站:
http: //www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Nuno Magalhaes"写道:
One more comment - throwing exceptions to send yourself "messages" is
probably a good habit to get out of!

Console.WriteLine or System.Diagnostics.Debug.WriteLine or even
MessageBox.Show work just fine. Exceptions are expensive when they are
thrown, and should be used only when something goes really wrong.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Nuno Magalhaes" wrote:
有谁知道为什么BeginAccept不起作用?如果,在下面的代码中,我执行正常的Accept函数,我可以得到客户端套接字,但似乎回调并没有被调用。
这里是代码:
-----------------------------
私有套接字套接字;
私有套接字clientSocket;

public TcpServer(string serverIP,int serverPort)
{/// clientSocket = null;
socket = new
Socket(AddressFamily.InterNetwork,SocketType.Strea) m,ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(新的AsyncCallback(AcceptConnection),socket);

私有的void AcceptConnection(IAsyncResult ar)
{
套接字套接字=(套接字) )ar.AsyncState;
clientSocket = socket.EndAccept(ar);
//抛出新的异常(在#上接受TCP + + + clientSocket.RemoteEndPoint.ToString());
抛出新的异常(Yuhuu);
}
Does anyone know why the BeginAccept doesn''t work? If, in the code
below, I do the normal Accept function I can get the client socket but
it seems that the callback isn''t really called.
Here''s the code:
-----------------------------
private Socket socket;
private Socket clientSocket;

public TcpServer(string serverIP,int serverPort)
{
clientSocket=null;
socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);
IPEndPoint ep=new IPEndPoint(IPAddress.Parse(serverIP),serverPort);
socket.Bind(ep);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptConnection),socket);
}

private void AcceptConnection(IAsyncResult ar)
{
Socket socket=(Socket)ar.AsyncState;
clientSocket=socket.EndAccept(ar);
//throw new Exception("TCP Accepted on
#"+clientSocket.RemoteEndPoint.ToString());
throw new Exception("Yuhuu");
}


这篇关于BeginAccept不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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