-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#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 ; | ||
struct hostent *server; | ||
socklen_t addr_size; | ||
if(argc<3) | ||
{ | ||
printf("No port provided"); | ||
exit(1); | ||
} | ||
portno = atoi(argv[2]); | ||
//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"); | ||
server = gethostbyname(argv[1]); | ||
bzero((char*)& serv_addr,sizeof(serv_addr)); | ||
serv_addr.sin_family=AF_INET; | ||
serv_addr.sin_addr.s_addr=INADDR_ANY; | ||
serv_addr.sin_port=htons(portno); | ||
bcopy((char*) server->h_addr, (char*)& serv_addr.sin_addr.s_addr,server->h_length); | ||
serv_addr.sin_port=htons(portno); | ||
|
||
/*Initialize size variable to be used later on*/ | ||
addr_size = sizeof serv_addr; | ||
|
||
while(1){ | ||
printf("Type a sentence to send to server:\n"); | ||
fgets(buffer,1024,stdin); | ||
printf("You typed: %s",buffer); | ||
|
||
nBytes = strlen(buffer) + 1; | ||
|
||
/*Send message to server*/ | ||
sendto(sockfd,buffer,nBytes,0,(struct sockaddr *)& serv_addr,addr_size); | ||
|
||
/*Receive message from server*/ | ||
nBytes = recvfrom(sockfd,buffer,1024,0,NULL, NULL); | ||
|
||
printf("Received from server: %s\n",buffer); | ||
|
||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#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; | ||
} | ||
|
||
|