-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_socket.c
More file actions
40 lines (39 loc) · 1001 Bytes
/
client_socket.c
File metadata and controls
40 lines (39 loc) · 1001 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#define PORT 8000
#define MAXSZ 100
#define SERVER_IP "127.0.0.1"
int main()
{
int sockfd;
struct sockaddr_in servaddr;
int n;
char msg1[MAXSZ];
char msg2[MAXSZ];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP);
servaddr.sin_port = htons(PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (1)
{
printf("\nEnter message to send to server:");
fgets(msg1, MAXSZ, stdin);
if (msg1[0] ='#')
{
break;
}
n = strlen(msg1) + 1;
send(sockfd, msg1, n, 0);
n = recv(sockfd, msg2, MAXSZ, 0);
printf("Receive message from server:%s\n", msg2);
}
return 0;
}