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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <string.h>
int main(int argc, char* argv[]) {
const char *server_name = "127.0.0.1";
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, server_name, &servaddr.sin_addr);
int port = atoi(argv[1]);
servaddr.sin_port = htons(port);
if((connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == 0) {
printf("Connected to server!\n");
fflush(stdout);
} else {
printf("Invalid or busy server address\n");
fflush(stdout);
exit(0);
};
int cont = 0;
char *buf = malloc(1024);
printf("Client input: ");
fgets(buf, 100, stdin);
while(strcmp(buf, "quit\n") != 0) {
send(sockfd, buf, 100, 0);
if ((cont = recv(sockfd, buf, 100, 0)) > 0) {
printf("Server response: %s\n", buf);;
fflush(stdout);
}
printf("Client input: ");
fgets(buf, 100, stdin);
};
close(sockfd);
}
|