-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientUDP2.c
67 lines (50 loc) · 1.4 KB
/
ClientUDP2.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
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
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
int main()
{
int cd,cd2,clilen,port,len,sport=5001,dport=5000;
cd = socket(AF_INET,SOCK_DGRAM,0);
if(cd<0)
printf("Can't Create\n");
else
printf("Socket is created\n");
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(sport);
cd2 = bind(cd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(cd2<0)
printf("cant bind\n");
else
printf("Binded\n");
struct sockaddr_in cliaddr;
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliaddr.sin_port = htons(dport);
char sendmsg[100],recvmsg[100];
len = sizeof(cliaddr);
while(1)
{
bzero(recvmsg,80);
recvfrom(cd,recvmsg,sizeof(recvmsg),0,(struct sockaddr*)&cliaddr,&len);
printf("\nFrom Client1: %s\n",recvmsg);
if(!strncmp(recvmsg,"exit",4))
{
break;
}
printf("Enter the message: ");
fgets(sendmsg,20,stdin);
sendto(cd,sendmsg,sizeof(sendmsg),0,(struct sockaddr*)&cliaddr,len);
if(!strncmp(sendmsg,"bye",4))
{
break;
}
}
printf("\nChat is terminated\n");
close(cd);
return 0;
}