10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netinet/tcp.h>
23 send(sock, data, data_length, 0);
34 va_start( ap , format );
35 length = vsnprintf( NULL , 0 , format , ap );
44 vsnprintf( output , length+1 , format , ap );
47 while ((stop = strstr(output,
"\r\n"))) strcpy(stop, stop + 1);
49 send( sock , output , length , 0 );
50 send( sock ,
"\r\n" , 2 , 0 );
56 const int growsize = 80;
60 int len = growsize, pos = 0;
63 if (recv(sock, &c, 1, 0) <= 0)
break;
64 if (c ==
'\r')
continue;
69 newline = (
char*) realloc(line, len);
84 SOCKET sock = INVALID_SOCKET;
90 struct sockaddr_in serv_addr;
93 return INVALID_SOCKET;
96 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
97 perror(
"create socket");
98 return INVALID_SOCKET;
101 if (connect(sock, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) == SOCKET_ERROR) {
104 return INVALID_SOCKET;
108 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on,
sizeof(on)) == SOCKET_ERROR) {
109 perror(
"setsockopt");
111 return INVALID_SOCKET;
120 if (sock != INVALID_SOCKET) closesocket(sock);
122 if (sock > 0) close(sock);
129 char *port = strchr( hostname,
':' );
130 struct hostent* host_ent;
135 default_port = atoi(port);
138 host_ent = gethostbyname(hostname);
139 if( host_ent == NULL )
141 perror(
"gethostbyname");
147 memset( sockaddr , 0 ,
sizeof(
struct sockaddr_in) );
148 sockaddr->sin_family = AF_INET;
149 memcpy( &sockaddr->sin_addr , host_ent->h_addr , host_ent->h_length );
150 sockaddr->sin_port = htons(default_port);
159 struct sockaddr_in serv_addr;
167 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
168 if( sock == INVALID_SOCKET )
173 if( sendto(sock, (
char*)data , data_length , 0 , (
struct sockaddr*)&serv_addr ,
sizeof(serv_addr) ) > 0 )