-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
84 lines (52 loc) · 1.72 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* client.c - Example of an Internet TPC client */
/* Compile with gcc -D_GNU_SOURCE client.c -o client */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include "utils.h"
#define PORT 5555 /* The well-nown port. */
#define BUFFER_SIZE 512
#define MESSAGE "Hello World of Sockets"
int
main (int argc, char **argv)
{
int socketfd; /* Socket file descriptor. */
struct sockaddr_in name; /* The socket address. */
struct hostent *hostinfo; /* The server information */
char * hostname;
int nbytes;
/* char buffer[BUFFER_SIZE]; */
/* argv[1] is the hostname where we want to connect to */
if (argc < 2)
{
fprintf (stderr, "Usage: client <server host name>\n");
return EXIT_FAILURE;
}
hostname = argv[1];
/* Create the socket.
socket (NAMESPACE, STYLE, PROTOCOL)
- namespace: local, other, internet: PF_INET || PF_INET6
- style: SOCK_STREAM, SOCK_DGRAM
- protocol: 0 means 'auto' */
socketfd = socket (PF_INET, SOCK_STREAM, 0);
if (socketfd < 0)
fatal ();
/* Give the socket a name. */
name.sin_family = AF_INET; /* Ineternet family */
name.sin_port = htons (PORT); /* Well-known port */
//name.sin_addr.s_addr = htonl (INADDR_ANY); /* Any internetface */
hostinfo = gethostbyname (hostname);
if (!hostinfo)
fatal ();
name.sin_addr = *(struct in_addr *) hostinfo->h_addr;
if ( connect (socketfd, & name, sizeof (name)))
fatal ();
nbytes = write (socketfd, MESSAGE, strlen (MESSAGE) + 1);
close (socketfd);
return EXIT_SUCCESS;
}