-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
105 lines (93 loc) · 2.43 KB
/
client.c
File metadata and controls
105 lines (93 loc) · 2.43 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<string.h>
#include<netinet/in.h>
#include <netdb.h>
//connect to server and send
int connect_server(const char *host, const char *port){
// Create a socket client
int client= socket( PF_INET6, SOCK_STREAM, 0);
if (client== 0) {
perror("socket()");
exit(EXIT_FAILURE);
}
//find address with host and the port
struct sockaddr_in6 in6;
memset(&in6,0,sizeof(in6));
in6.sin6_family = AF_INET6;
in6.sin6_port = htons(atoi(port));
//convert to in6
int s=inet_pton(AF_INET6,host,&in6.sin6_addr.s6_addr);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}
//connect
if(connect(client,(struct sockaddr *)&in6,sizeof(in6))!=0){
perror("connect()");
exit(EXIT_FAILURE);
}
return client;
}
//read from sock and store in buffer, stop when '\n' or nb >size
void receive_message(int sock, char *buffer, int size) {
int nb=0;
char* buff=buffer;
do{
int r=read(sock,buff,1);
if(r<0){
perror("read");
exit(EXIT_FAILURE);
}
if(r==0){
printf("quit!\n");
exit(EXIT_FAILURE);
}
nb++;
buff++;
}while(buffer[nb-1]!=10);
//printf("%d",nb);
buffer[nb]='\0';
}
//scan a message, send to server, print the result
void speak_to_server(int sock){
while(1){
printf(">>>");
char buf[100]={};
char buffer[100]={};
memset(buf,'\0',100);
memset(buffer,'\0',100);
if(fgets(buf,100,stdin)==NULL){
perror("fgets()");
exit(EXIT_FAILURE);
}
// printf("%sfrom client\n %lu",buf,strlen(buf));
if(write(sock,buf,strlen(buf))<0){
perror("write");
exit(EXIT_FAILURE);
}
receive_message(sock,buffer,100);
printf("%s",buffer);
}
}
int main(int argc, char *argv[]){
if(argc!=3){
printf("usage: client + host + port\n");
exit(EXIT_FAILURE);
}
int client=connect_server(argv[1],argv[2]);
speak_to_server(client);
if(close(client)!=0){
perror("close()");
exit(EXIT_FAILURE);
}
return 0;
}