diff options
author | Simon Josefsson <simon@josefsson.org> | 2005-08-12 10:13:40 +0000 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2005-08-12 10:13:40 +0000 |
commit | 083cb2a59feefea841403d178a92f6716fb48143 (patch) | |
tree | ea8bf34e5ea8be0f6b15ecf62201bc3151e2c1e4 /doc/examples/tcp.c | |
parent | 0b705df7124444e354b96e2c44740dfc5ead7ca4 (diff) | |
download | gnutls-083cb2a59feefea841403d178a92f6716fb48143.tar.gz |
Add, from ex-client2.c.
Diffstat (limited to 'doc/examples/tcp.c')
-rw-r--r-- | doc/examples/tcp.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/doc/examples/tcp.c b/doc/examples/tcp.c new file mode 100644 index 0000000000..e98581e1c9 --- /dev/null +++ b/doc/examples/tcp.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <unistd.h> + +#define SA struct sockaddr + +/* Connects to the peer and returns a socket + * descriptor. + */ +extern int +tcp_connect (void) +{ + const char *PORT = "5556"; + const char *SERVER = "127.0.0.1"; + int err, sd; + struct sockaddr_in sa; + + /* connects to server + */ + sd = socket (AF_INET, SOCK_STREAM, 0); + + memset (&sa, '\0', sizeof (sa)); + sa.sin_family = AF_INET; + sa.sin_port = htons (atoi (PORT)); + inet_pton (AF_INET, SERVER, &sa.sin_addr); + + err = connect (sd, (SA *) & sa, sizeof (sa)); + if (err < 0) + { + fprintf (stderr, "Connect error\n"); + exit (1); + } + + return sd; +} + +/* closes the given socket descriptor. + */ +extern void +tcp_close (int sd) +{ + shutdown (sd, SHUT_RDWR); /* no more receptions */ + close (sd); +} |