-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
55 lines (45 loc) · 1.33 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
#define _BSD_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char message[] = "Kawabounga";
void exitOnError(const char * message)
{
fprintf(stderr,"%s \n", message);
exit(EXIT_FAILURE);
}
int main()
{
int sock;
struct sockaddr_in sockstr;
struct in_addr adresse;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
exitOnError("Socket() client : Nok \n");
else
fprintf(stdout, "Socket() client : ok \n");
sockstr.sin_family = AF_INET;
sockstr.sin_port = htons(5990);
// if(inet_aton("127.0.0.1", &adresse) == 0)
// exitOnError("inet_aton() client : Nok \n");
// else
// fprintf(stdout, "inet_aton() client : ok \n");
//sockstr.sin_addr = adresse;
sockstr.sin_addr.s_addr = inet_addr("127.0.0.1");
/* inet_addr is deprecated, please use inet_aton */
if(connect(sock, (struct sockaddr *) &sockstr, sizeof(sockstr)) == -1)
exitOnError("connect() client : Nok \n");
else
fprintf(stdout, "connect() client : ok \n");
if (write(sock , message, strlen(message)) < 0)
exitOnError("write() client : Nok \n");
else
fprintf(stdout, "write() client : ok \n");
close(sock);
exit(EXIT_SUCCESS);
}