source: trunk/FACT++/dim/src/examples/test_tcp.c@ 13492

Last change on this file since 13492 was 11071, checked in by tbretz, 13 years ago
Replaced v19r21 by a version extracted with 'unzip -a' to get proper unix text format.
File size: 3.8 KB
Line 
1#ifdef WIN32
2#define ioctl ioctlsocket
3
4#define closesock myclosesocket
5#define readsock recv
6#define writesock send
7
8#define EINTR WSAEINTR
9#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
10#define EWOULDBLOCK WSAEWOULDBLOCK
11#define ECONNREFUSED WSAECONNREFUSED
12#define HOST_NOT_FOUND WSAHOST_NOT_FOUND
13#define NO_DATA WSANO_DATA
14
15#include <windows.h>
16#include <process.h>
17#include <io.h>
18#include <fcntl.h>
19#include <Winsock.h>
20#include <stddef.h>
21#include <stdlib.h>
22#include <stdio.h>
23#else
24#define closesock close
25#define readsock(a,b,c,d) read(a,b,c)
26
27#if defined(__linux__) && !defined (darwin)
28#define writesock(a,b,c,d) send(a,b,c,MSG_NOSIGNAL)
29#else
30#define writesock(a,b,c,d) write(a,b,c)
31#endif
32#include <ctype.h>
33#include <sys/socket.h>
34#include <fcntl.h>
35#include <netinet/in.h>
36#include <netinet/tcp.h>
37#include <signal.h>
38#include <sys/ioctl.h>
39#include <errno.h>
40#include <netdb.h>
41#include <unistd.h>
42#include <sys/time.h>
43#include <sys/types.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <signal.h>
48#endif
49
50#define ushort unsigned short
51#define TCP_RCV_BUF_SIZE 16384/*32768*//*65536*/
52#define TCP_SND_BUF_SIZE 16384/*32768*//*65536*/
53
54
55#ifdef WIN32
56int init_sock()
57{
58 WORD wVersionRequested;
59 WSADATA wsaData;
60 int err;
61 static int sock_init_done = 0;
62
63 if(sock_init_done) return(1);
64 wVersionRequested = MAKEWORD( 2, 0 );
65 err = WSAStartup( wVersionRequested, &wsaData );
66
67 if ( err != 0 )
68 {
69 return(0);
70 }
71
72 /* Confirm that the WinSock DLL supports 2.0.*/
73 /* Note that if the DLL supports versions greater */
74 /* than 2.0 in addition to 2.0, it will still return */
75 /* 2.0 in wVersion since that is the version we */
76 /* requested. */
77
78 if ( LOBYTE( wsaData.wVersion ) != 2 ||
79 HIBYTE( wsaData.wVersion ) != 0 )
80 {
81 WSACleanup( );
82 return(0);
83 }
84 sock_init_done = 1;
85 return(1);
86}
87
88int myclosesocket(int path)
89{
90 int code, ret;
91 code = WSAGetLastError();
92 ret = closesocket(path);
93 WSASetLastError(code);
94 return ret;
95}
96#endif
97
98int tcp_open_client( char *node, int port )
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}
179
180int tcp_write( int path, char *buffer, int size )
181{
182 /* Do a (synchronous) write to conn_id.
183 */
184 int wrote;
185
186 wrote = writesock( path, buffer, size, 0 );
187 if( wrote == -1 ) {
188 return(0);
189 }
190 return(wrote);
191}
Note: See TracBrowser for help on using the repository browser.