Last active
October 16, 2017 10:47
-
-
Save AjayKrP/05d1bd511200bc9b89b755a3c61300b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
// Time function, sockets, htons... file stat | |
#include <sys/time.h> | |
#include <time.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <sys/types.h> | |
#include <sys/uio.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <strings.h> | |
#define BUFFERT 512 | |
/* Queue size of clients */ | |
#define BACKLOG 1 | |
int duration (struct timeval *start,struct timeval *stop, struct timeval *delta); | |
int create_server_socket (int port); | |
struct sockaddr_in sock_serv,sock_clt; | |
int main(int argc,char** argv){ | |
int sfd,fd; | |
unsigned int length=sizeof(struct sockaddr_in); | |
long int n, m,count=0; | |
unsigned int nsid; | |
ushort clt_port; | |
char buffer[BUFFERT],filename[256]; | |
char dst[INET_ADDRSTRLEN]; | |
// Variable for date | |
time_t intps; | |
struct tm* tmi; | |
if(argc!=2) { | |
perror("use ./a.out <num_port> <file2send> \n"); | |
exit(3); | |
} | |
sfd = create_server_socket(atoi(argv[1])); | |
bzero(buffer,BUFFERT); | |
listen(sfd,BACKLOG); | |
// Function that waits for the client's connected function | |
nsid = accept(sfd,(struct sockaddr*)&sock_clt,&length); | |
if(nsid==-1){ | |
perror("accept fail"); | |
return EXIT_FAILURE; | |
} | |
else { | |
if(inet_ntop(AF_INET,&sock_clt.sin_addr,dst,INET_ADDRSTRLEN)==NULL){ | |
perror("socket error"); | |
exit (4); | |
} | |
clt_port=ntohs(sock_clt.sin_port); | |
printf("Start of connection for : %s:%d\n",dst,clt_port); | |
// Processing the filename with date | |
bzero(filename,256); | |
intps = time(NULL); | |
tmi = localtime(&intps); | |
bzero(filename,256); | |
sprintf(filename,"clt.%d.%d.%d.%d.%d.%d",tmi->tm_mday,tmi->tm_mon+1,1900+tmi->tm_year,tmi->tm_hour,tmi->tm_min,tmi->tm_sec); | |
printf("Creating the copied output file : %s\n",filename); | |
if ((fd=open(filename,O_CREAT|O_WRONLY,0600))==-1) | |
{ | |
perror("open fail"); | |
exit (3); | |
} | |
bzero(buffer,BUFFERT); | |
n=recv(nsid,buffer,BUFFERT,0); | |
while(n) { | |
if(n==-1){ | |
perror("recv fail"); | |
exit(5); | |
} | |
if((m=write(fd,buffer,n))==-1){ | |
perror("write fail"); | |
exit (6); | |
} | |
count=count+m; | |
bzero(buffer,BUFFERT); | |
n=recv(nsid,buffer,BUFFERT,0); | |
} | |
close(sfd); | |
close(fd); | |
} | |
close(nsid); | |
printf ("End of transmission with%s.%d \n", dst, clt_port); | |
printf ("Number of bytes received:%ld \n", count); | |
return EXIT_SUCCESS; | |
} | |
/* Function to create a socket and its attachment to the system | |
* Returns a file descriptor in the process descriptor table | |
* bind allows its definition with the system | |
*/ | |
int create_server_socket (int port){ | |
int l; | |
int sfd; | |
int yes=1; | |
sfd = socket(PF_INET,SOCK_STREAM,0); | |
if (sfd == -1){ | |
perror("socket fail"); | |
return EXIT_SUCCESS; | |
} | |
/*SOL_SOCKET: To manipulate options at the sockets API level | |
* SO_REUSEADDR: When you need to restart a server after a crash, this can be useful | |
* not having an error when creating the socket (the system's IP stack did not | |
* always had time to do housework). | |
* Case where several servers that listen to the same port ... (?) | |
*/ | |
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(int)) == -1 ) { | |
perror("setsockopt error"); | |
exit(5); | |
} | |
// prepare destination socket address | |
l=sizeof(struct sockaddr_in); | |
bzero(&sock_serv,l); | |
sock_serv.sin_family=AF_INET; | |
sock_serv.sin_port=htons(port); | |
sock_serv.sin_addr.s_addr=htonl(INADDR_ANY); | |
// Assign an identity to the socket | |
if(bind(sfd,(struct sockaddr*)&sock_serv,l)==-1){ | |
perror("bind fail"); | |
return EXIT_FAILURE; | |
} | |
return sfd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment