ServerSocket接受方法出现问题. [英] Problem with ServerSocket Accept Method.

查看:81
本文介绍了ServerSocket接受方法出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议我如何停止接受连接的接受方法.我需要停止接受,因为我想在两者之间执行回调函数.

can anyone suggest me how can i stop accept method for accepting connection .i need to stop accept because i want to execute my callback function in between.

推荐答案

很奇怪的问题.
当然,您可以关闭套接字,否则服务器也不再监听.
您可以在侦听中指定仅要服务1个这样的连接
听(袜子,1).
除此之外,我认为您的应用程序编写完全错误,因为在返回要接受的处理之前执行回调不应该成为问题.因为通常您会执行以下操作:

while(1){
newsockfd = accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
执行您的回调
}

执行回调时,您无法接受新连接.
您应将回调代码保持尽可能小.
实际上,TCP应用程序旨在一次处理多个连接,这使它们变得相当复杂.最终要么在多个套接字上使用select要么使用多个线程.
Very strange question.
You can close the socket of course then you don''t have your server listening anymore either.
You can specify in listen that you want to service only 1 connection like this
listen(sock,1).
Other than that I think your application is written entirely wrong because it should not be a problem to execute your callback before returning your processing to accept. Because normally you do something like this :

while(1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
execute your callback
}

while your callback is executing you cannot accept a new connection.
You should keep the code of your callback as small as possible.
TCP applications are actually meant to handle multiple connections at a time, which makes them quite complicated. Either you end up using select on multiple sockets or using several threads.


这篇关于ServerSocket接受方法出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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