-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
90 lines (69 loc) · 1.81 KB
/
client.c
File metadata and controls
90 lines (69 loc) · 1.81 KB
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
85
86
87
88
89
90
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/times.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <semaphore.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <sys/signal.h>
#include <fcntl.h>
#define SERVER_PROC 111
#define CLIENT_PROC 222
#define SERVER_PORT 4574
#define MIDDLEWARE_PORT 4575
#define CLIENT_PORT 5001
int sock;
void SIGINT_handler(int);
int main(int argc, char *argv[])
{
signal(SIGINT, SIGINT_handler);
struct sockaddr_in middleware;
char message[500];
//Create socket
sock = socket(AF_INET, SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
middleware.sin_addr.s_addr = htonl(INADDR_ANY);
middleware.sin_family = AF_INET;
middleware.sin_port = htons(MIDDLEWARE_PORT);
//Connect to remote middleware
if (connect(sock, (struct sockaddr *)&middleware, sizeof(middleware)) < 0)
{
perror("Connect failed. Error");
return 1;
}
puts("Connected\n");
sprintf(message, "CLIENT %ld", (long) getpid());
if(send(sock, message, sizeof(message), 0) < 0)
{
puts("Send failed");
return 1;
}
/*******************************************************************************************************/
while(1) {
printf("\n");
scanf("%s", message);
if(send(sock, message, sizeof(message), 0) < 0)
{
puts("Send failed");
return 1;
}
}
/*******************************************************************************************************/
close(sock);
return 0;
}
void SIGINT_handler(int sig) {
close(sock);
exit(1);
}