Socket client 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,cd,i=0,n;
char fn[256],buf[5000];
struct sockaddr_in server;
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((cd=connect(sd,(struct sockaddr *)&server,sizeof(server)))<0)
printf("\nConnect Error");
printf("\nEnter filename= ");
scanf("%s",fn);
write(sd,&fn,sizeof(fn));
read(sd,&n,sizeof(n));
// next two lines r newly added with else
if(n==0)
printf("file doesnt exist\n");
else{
printf("%d",n);
for(i=0;i {
read(sd,&buf[i],sizeof(char));
}
for(i=0;i {
write(1,&buf[i],sizeof(char));
// printf("%c",buf[i]);
}
}
close(sd);
close(cd);
return 0;
}

This entry was posted at 4:52 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.