-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
33 lines (32 loc) · 958 Bytes
/
client.c
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
#define MAXLINE 1024
#define PORT 8200
int main(int argc, char *argv[])
{
int sockfd;
int n;
socklen_t len;
char sendline[1024], recvline[1024];
struct sockaddr_in servaddr;
strcpy(sendline, "");
printf("\n Enter the message : ");
scanf("%[^\n]s", sendline);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
len = sizeof(servaddr);
sendto(sockfd, sendline, MAXLINE, 0, (struct sockaddr *)&servaddr, len);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0;
printf("\n Server's Echo : %s\n\n", recvline);
return 0;
}