/[cvs]/wurmi/connect.c
ViewVC logotype

Contents of /wurmi/connect.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations)
Thu Aug 30 20:03:22 2007 UTC (11 years, 3 months ago) by riso
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
File MIME type: text/plain
Zdebugovany streamer

1 #include "connect.h"
2
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <arpa/inet.h>
6 #include <netdb.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 FILE *Connect(char *server, int port) {
11 int fd;
12 struct sockaddr_in saddr;
13 struct hostent *hent;
14 FILE *out;
15
16 if (!(hent = gethostbyname(server))) {
17 fprintf(stderr, "Cannot fint host '%s'!\n", server);
18 return NULL;
19 }
20 if (hent->h_addrtype != AF_INET) {
21 fprintf(stderr, "Wrong protocol\n");
22 return NULL;
23 }
24 if (hent->h_length != sizeof(saddr.sin_addr)) {
25 fprintf(stderr, "Wrong address size\n");
26 return NULL;
27 }
28
29 saddr.sin_family = hent->h_addrtype;
30 saddr.sin_port = htons(port);
31 memcpy(&saddr.sin_addr, hent->h_addr_list[0], hent->h_length);
32
33 if ( (fd = socket(PF_INET, SOCK_STREAM, 0)) == -1 ) {
34 fprintf(stderr, "Cannot open socket\n");
35 return NULL;
36 }
37 if ( connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1 ) {
38 fprintf(stderr, "Cannot connect\n");
39 close(fd);
40 return NULL;
41 }
42
43 if ( !(out = fdopen(fd, "r+")) ) {
44 fprintf(stderr, "Cannot open file\n");
45 close(fd);
46 return NULL;
47 }
48 return out;
49 }

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26