相关文章推荐
低调的鸵鸟  ·  jsoup 1.18.1 发布,Java ...·  4 天前    · 
爱搭讪的红金鱼  ·  WorksheetFunction.Week ...·  1 年前    · 
鬼畜的绿茶  ·  Android ...·  1 年前    · 

设置connect的超时很简单,CSDN上也有人提到过使用select,但却没有一个令人满意与完整的答案。偶所讲的也正是select函数,此函数集成在winsock1.1中,简单点讲,"作用使那些想避免在套接字调用过程中被锁定的应用程序,采取一种有序的方式,同时对多个套接字进行管理"(《Windows网络编程技术》原话)。使用方法与解释请见《Windows网络编程技术》。
在使用此函数前,需先将socket设置为非锁定模式,这样,在connect时,才会立马跳过,同时,通常也会产生一个WSAEWOULDBLOCK错误,这个错误没关系。再执行select则是真正的超时。

 1 WSADATA wsd;
 2 SOCKET cClient;
 3 int ret;
 4 struct sockaddr_in server;
 5 hostent *host=NULL;
 7 if(WSAStartup(MAKEWORD(2,0),&wsd)){
 8     return 0;
11 cClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
13 if(cClient==INVALID_SOCKET){
14     return 0;
17 //set Recv and Send time out
18 int TimeOut=6000; //设置发送超时6秒
19 if(::setsockopt(cClient,SOL_SOCKET,SO_SNDTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){
20     return 0;
22 TimeOut=6000;//设置接收超时6秒
23 if(::setsockopt(cClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&TimeOut,sizeof(TimeOut))==SOCKET_ERROR){
24     return 0;
26 //设置非阻塞方式连接
27 unsigned long ul = 1;
28 ret = ioctlsocket(cClient, FIONBIO, (unsigned long*)&ul);
29 if(ret==SOCKET_ERROR)
30     return 0;
32 //连接
33 server.sin_family = AF_INET;
34 server.sin_port = htons(25);
35 server.sin_addr .s_addr = inet_addr((LPCSTR)pSmtp);
36 if(server.sin_addr.s_addr == INADDR_NONE){
37     return 0;
40 connect(cClient,(const struct sockaddr *)&server,sizeof(server));
42 //select 模型,即设置超时
43 struct timeval timeout ;
44 fd_set r;
46 FD_ZERO(&r);
47 FD_SET(cClient, &r);
48 timeout.tv_sec = 15; //连接超时15秒
49 timeout.tv_usec =0;
50 ret = select(0, 0, &r, 0, &timeout);
51 if ( ret <= 0 )
53     ::closesocket(cClient);
54     return 0;
56 //一般非锁定模式套接比较难控制,可以根据实际情况考虑 再设回阻塞模式
57 unsigned long ul1= 0 ;
58 ret = ioctlsocket(cClient, FIONBIO, (unsigned long*)&ul1);
59 if(ret==SOCKET_ERROR){
60     ::closesocket (cClient);
61     return 0;

   关键代码段

1  struct timeval tv = {timeout, 0};
2 setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
3 //setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));//设置接收超时,不能对connect起作用

1.常用方法
  设置socket非阻塞,之后使用select等设置超时时间
2.使用alarm信号量
  需要注意:线程信号量掩码是线程私有的,当指定进程递交信号量时,操作系统会将信号量递交至该进程中未屏蔽该信号量的所有线程中的随机之一。见 man 7 signal:
A signal may be generated (and thus pending) for a process as a whole
(e.g., when sent using kill(2)) or for a specific thread (e.g., certain
signals, such as SIGSEGV and SIGFPE, generated as a consequence of exe-
cuting a specific machine-language instruction are thread directed, as
are signals targeted at a specific thread using pthread_kill(3)). A
process-directed signal may be delivered to any one of the threads that
does not currently have the signal blocked. If more than one of the
threads has the signal unblocked, then the kernel chooses an arbitrary
thread to which to deliver the signal.
《TCP高效编程》

没有坚守就没有事业,没有执着就没有未来!
python取消转义符号 python去除转义字符

字符串的切片操作字符串中的转义字符\n换行符号\反斜杠符号单引号双引号制表符防止转义的方法切片操作:print(变量名[开始:结束:步长]),注意:下标依然从0开始,且直至结束位置的下标减一 举例: 代码如下:nameList = 'lishk' print(nameList) print(nameList[::]) print(nameList[3::]) #从下标3开始,返回后面所有元素