-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
47 lines (33 loc) · 1.11 KB
/
client.c
File metadata and controls
47 lines (33 loc) · 1.11 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define PORT 4444
int main(){
int client_socketfd;
struct sockaddr_in server_address;
if((client_socketfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("failed to create the client's socket");
exit(1);
}
server_address.sin_family=AF_INET;
server_address.sin_port=htons(PORT);
server_address.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(client_socketfd,(struct sockaddr *)&server_address,sizeof(server_address))<0){
perror("failed to connect to the server's socket");
exit(1);
}
char *buffer = (char *)calloc(1024,sizeof(char));
char *hello = "Hello from Client";
write(client_socketfd,hello,strlen(hello));
//send(client_socketfd,hello,strlen(hello),0);
printf("message '%s' is sent to the server\n",hello);
read(client_socketfd,buffer,1024);
printf("message received from the server : %s\n",buffer);
close(client_socketfd);
free(buffer);
return 0;
}