-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathechoserverudp.cpp
64 lines (53 loc) · 1.47 KB
/
echoserverudp.cpp
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
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int sockfd,newsockfd,portno,chilen,nBytes;
char buffer[256];
struct sockaddr_in serv_addr ,cli_addr;
int n;
socklen_t addr_size;
if(argc<3)
{
printf("No port provided");
exit(1);
}
//printf("argument mil gyi sari\n");
sockfd=socket(PF_INET, SOCK_DGRAM,0);
if(sockfd<0)
{
printf("Error opening sockt\n");
exit(1);
}
//printf("Socket bhi open ho gya\n");
bzero((char*)& serv_addr,sizeof(serv_addr));
portno= atoi(argv[2]);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);
if(bind(sockfd,(struct sockaddr*)& serv_addr,sizeof(serv_addr))<0)
{
printf("Error on binding\n");
exit(1);
}
//printf("Bind bhi ho gya\n");
/*Initialize size variable to be used later on*/
addr_size = sizeof cli_addr;
while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(sockfd,buffer,1024,0,(struct sockaddr *)& cli_addr, &addr_size);
printf("recieved\n");
/*Convert message received to uppercase*/
/*Send uppercase message back to client, using serverStorage as the address*/
sendto(sockfd,buffer,nBytes,0,(struct sockaddr *)& cli_addr,addr_size);
printf("sent\n");
}
return 0;
}