FACT++  1.0
int tcp_open_client ( char *  node,
int  port 
)

Definition at line 98 of file test_tcp.c.

References closesock, TCP_RCV_BUF_SIZE, TCP_SND_BUF_SIZE, and ushort.

99 {
100  /* Create connection: create and initialize socket stuff. Try
101  * and make a connection with the server.
102  */
103  struct sockaddr_in sockname;
104  struct hostent *host;
105  int path, val, ret_code, ret;
106 
107 #ifdef WIN32
108  init_sock();
109 #endif
110  if( (host = gethostbyname(node)) == (struct hostent *)0 )
111  {
112  return(0);
113  }
114 
115  if( (path = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
116  {
117  perror("socket");
118  return(0);
119  }
120 
121  val = 1;
122 
123  if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_NODELAY,
124  (char*)&val, sizeof(val))) == -1 )
125  {
126 #ifdef DEBUG
127  printf("Couln't set TCP_NODELAY\n");
128 #endif
129  closesock(path);
130  return(0);
131  }
132 
133  val = TCP_SND_BUF_SIZE;
134  if ((ret_code = setsockopt(path, SOL_SOCKET, SO_SNDBUF,
135  (char*)&val, sizeof(val))) == -1 )
136  {
137 #ifdef DEBUG
138  printf("Couln't set SO_SNDBUF\n");
139 #endif
140  closesock(path);
141  return(0);
142  }
143 
144  val = TCP_RCV_BUF_SIZE;
145  if ((ret_code = setsockopt(path, SOL_SOCKET, SO_RCVBUF,
146  (char*)&val, sizeof(val))) == -1 )
147  {
148 #ifdef DEBUG
149  printf("Couln't set SO_RCVBUF\n");
150 #endif
151  closesock(path);
152  return(0);
153  }
154 
155 #if defined(__linux__) && !defined (darwin)
156  val = 2;
157  if ((ret_code = setsockopt(path, IPPROTO_TCP, TCP_SYNCNT,
158  (char*)&val, sizeof(val))) == -1 )
159  {
160 #ifdef DEBUG
161  printf("Couln't set TCP_SYNCNT\n");
162 #endif
163  }
164 #endif
165 
166  sockname.sin_family = PF_INET;
167  sockname.sin_addr = *((struct in_addr *) host->h_addr);
168  sockname.sin_port = htons((ushort) port); /* port number to send to */
169  while((ret = connect(path, (struct sockaddr*)&sockname, sizeof(sockname))) == -1 )
170  {
171  if(errno != EINTR)
172  {
173  closesock(path);
174  return(0);
175  }
176  }
177  return(path);
178 }
Definition: dns.c:26
#define ushort
Definition: test_tcp.c:50
#define TCP_SND_BUF_SIZE
Definition: test_tcp.c:52
#define TCP_RCV_BUF_SIZE
Definition: test_tcp.c:51
#define closesock
Definition: test_tcp.c:24