OSDN Git Service

I was library can be compiled 20140223
[zither/ds-zither.git] / libms / select_connect.c
1
2 #include<stdio.h>
3 #include<unistd.h>
4 #include<fcntl.h>
5 #include<sys/socket.h>
6 #include<netinet/in_systm.h>
7 #include<netinet/in.h>
8 #include<netinet/ip.h>
9 #include<arpa/inet.h>
10 #include<stdlib.h>
11 #include<string.h>
12 #include<pthread.h>
13 #include <errno.h>
14
15
16
17 /*----------------------- CONNECT --------------------*/
18 //int select_connect(int * sk, struct sockaddr_in * my_addr, int len, int timeout)
19 int select_connect(int * sk, struct sockaddr_in * my_addr, int timeout)
20 {
21 int ret, retval;
22 fd_set rfds;
23 struct timeval tv;
24
25 ret = 0;
26
27 fcntl(* sk, F_SETFL, O_NONBLOCK);
28
29 if((connect(*sk, (struct sockaddr *) my_addr, sizeof(* my_addr))) < 0){
30
31 //if((ret = connect(* sk, (struct sockaddr *) my_addr, len)) < 0){
32 // fprintf(stderr,"socket_connect(): Not connection\n");
33    fcntl(* sk, F_SETFL, 0);
34    return 0;
35    }
36
37 if(ret >= 0){
38    fcntl(* sk, F_SETFL, 0);
39    return(ret);
40    }
41
42 if(errno == EINPROGRESS){
43    tv.tv_sec = timeout;
44    tv.tv_usec = 0;
45    while(1){
46       FD_ZERO(&rfds);
47       FD_SET(* sk, &rfds);
48       retval = select(* sk + 1, NULL, &rfds, NULL, &tv);
49
50       if(retval < 0){
51          fcntl(* sk, F_SETFL, 0);
52          return(-1);
53          }
54       else if(retval == 0){
55          fcntl(* sk, F_SETFL, 0);
56          return(-1);
57          }
58       else{
59          fcntl(* sk, F_SETFL, 0);
60          return(ret);
61          }
62
63       }
64    }
65 else{
66    fcntl(* sk, F_SETFL, 0);
67    return(-1);
68    }
69
70 return(ret);
71 }
72
73
74
75
76
77
78