Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created August 26, 2015 04:16
Show Gist options
  • Save tanayseven/41b1e3462107fc33f722 to your computer and use it in GitHub Desktop.
Save tanayseven/41b1e3462107fc33f722 to your computer and use it in GitHub Desktop.
UDP string reverse server
/*
The MIT License (MIT)
Copyright (c) 2015 Tanay PrabhuDesai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define S_PORT 43454
#define C_PORT 43455
#define ERROR -1
#define IP_STR "127.0.0.1"
int main(int argc, char const *argv[]) {
int sfd, len;
char str_buf[2048];
struct sockaddr_in servaddr, clientaddr;
socklen_t addrlen;
sfd = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (sfd == ERROR) {
perror("Could not open a socket");
return 1;
}
memset((char *) &servaddr, 0, sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(IP_STR);
servaddr.sin_port=htons(S_PORT);
memset((char *) &clientaddr, 0, sizeof(clientaddr));
clientaddr.sin_family=AF_INET;
clientaddr.sin_addr.s_addr=inet_addr(IP_STR);
clientaddr.sin_port=htons(C_PORT);
if((bind(sfd,(struct sockaddr *)&clientaddr,sizeof(clientaddr)))!=0) {
perror("Could not bind socket");
return 2;
}
printf("Client is running on %s:%d\n", IP_STR, C_PORT);
printf("Enter a string: ");
scanf("%[^\n]%*c",str_buf);
len = strlen(str_buf);
sendto(sfd, &len, sizeof(len), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
sendto(sfd, str_buf, len, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
addrlen = sizeof(clientaddr);
recvfrom(sfd, &len, sizeof(len), 0, (struct sockaddr *)&clientaddr, &addrlen);
recvfrom(sfd, str_buf, len, 0, (struct sockaddr *)&clientaddr, &addrlen);
printf("Server Replied: %s\n", str_buf);
return 0;
}
/*
The MIT License (MIT)
Copyright (c) 2015 Tanay PrabhuDesai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define S_PORT 43454
#define C_PORT 43455
#define ERROR -1
#define IP_STR "127.0.0.1"
void strrev(char *str, int len) {
int i, j;
char temp;
for (i = 0, j = len -1; i < j; ++i, --j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main(int argc, char const *argv[]) {
int sfd, len;
char *str_buf;
struct sockaddr_in servaddr, clientaddr;
sfd = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (sfd == ERROR) {
perror("Could not open a socket");
return 1;
}
memset((char *) &servaddr, 0, sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(S_PORT);
memset((char *) &clientaddr, 0, sizeof(clientaddr));
clientaddr.sin_family=AF_INET;
clientaddr.sin_addr.s_addr=inet_addr(IP_STR);
clientaddr.sin_port=htons(C_PORT);
if((bind(sfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))!=0) {
perror("Could not bind socket");
return 2;
}
printf("Server is running on %s:%d\n", IP_STR, S_PORT);
while(1) {
recvfrom(sfd, &len, sizeof(len), 0, (struct sockaddr *)&clientaddr, (socklen_t *)&clientaddr);
str_buf = (char *) malloc(len*sizeof(char));
recvfrom(sfd, str_buf, len, 0, (struct sockaddr *)&clientaddr, (socklen_t *)&clientaddr);
printf("Client at %s:%d said: %s\t", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), str_buf);
strrev(str_buf,len);
sendto(sfd, &len, sizeof(len), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
sendto(sfd, str_buf, len, 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
printf("The reverse is: %s\n", str_buf);
free(str_buf);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment