-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
59 lines (49 loc) · 1.16 KB
/
client.c
File metadata and controls
59 lines (49 loc) · 1.16 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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define MAX 512
int main(int argc , char *argv[]){
int newSocket, ret;
struct sockaddr_in server_addr;
char input[MAX];
newSocket = socket(AF_INET, SOCK_STREAM, 0);
if(newSocket < 0){
printf("Connection Failed\n");
exit(1);
}
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[1]));
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = connect(newSocket, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(ret < 0){
printf("Connection Failed!\n");
exit(1);
}
printf("Welcome to command server.\n");
while(1){
printf("Command: ");
gets(input);
send(newSocket, input, strlen(input), 0);
if(strcmp(input, "QUIT") == 0){
close(newSocket);
printf("Goodbye!\n");
exit(1);
}
if(recv(newSocket, input, MAX, 0) < 0){
printf("something went wrong.\n");
}else{
printf("%s\n", input);
}
}
return 0;
}