1 /* This example code is placed in the public domain. */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
16 #define SA struct sockaddr
19 int tcp_connect (void);
20 void tcp_close (int sd
);
22 /* Connects to the peer and returns a socket
28 const char *PORT
= "5556";
29 const char *SERVER
= "127.0.0.1";
31 struct sockaddr_in sa
;
35 sd
= socket (AF_INET
, SOCK_STREAM
, 0);
37 memset (&sa
, '\0', sizeof (sa
));
38 sa
.sin_family
= AF_INET
;
39 sa
.sin_port
= htons (atoi (PORT
));
40 inet_pton (AF_INET
, SERVER
, &sa
.sin_addr
);
42 err
= connect (sd
, (SA
*) & sa
, sizeof (sa
));
45 fprintf (stderr
, "Connect error\n");
52 /* closes the given socket descriptor.
57 shutdown (sd
, SHUT_RDWR
); /* no more receptions */