Socket server program  

Posted in

#include "netinet/in.h"
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "stdlib.h"
#include "arpa/inet.h"
#define IP "127.0.0.1"
#define PORT 6010

int main()
{
int sd,ad,bd,i=0,j,ss;
char fn[256],buf[5000],c;
FILE *fp;
struct sockaddr_in server,client;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\nSocket Error");
server.sin_addr.s_addr=inet_addr(IP);
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
if((bd=bind(sd,(struct sockaddr *)&server,sizeof(server)))<0)
printf("\nBind Error");
listen(sd,5);
ss=sizeof(client);
if((ad=accept(sd,(struct sockaddr *)&client,&ss))<0)
printf("\nAccept Error");
read(ad,&fn,sizeof(fn));
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("\nFile doesnot exist");
write(ad,&i,sizeof(i));
exit(0);
}
while((c=fgetc(fp))!=EOF)
{
buf[i]=c;
i++;
}
write(ad,&i,sizeof(i));
for(j=0;j write(ad,&buf[j],sizeof(buf[j]));
fclose(fp);
close(sd);
close(bd);
close(ad);
return 0;
}

This entry was posted at 4:54 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment

Reverse Engineering :Subscribe Now

Zts - ZTS

DISCLAIMER

This Blog is ONLY for educational purposes,
and any such codes/snippets provided are to be executed on
your sole discretion. The author is not responsible for the codes.