Socket Programming in C - Sending a file from Server to Client -


i have socket code , client code below. far establish connection server, want send file client server; if file has no data in it. have augment code send files? suggestions in code form?

client:

#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h>   int main(int argc, char *argv[]) {     int sockfd = 0, n = 0;     char recvbuff[1024];     struct sockaddr_in serv_addr;       if(argc != 2)     {         printf("\n usage: %s <ip of server> \n",argv[0]);         return 1;     }       memset(recvbuff, '0',sizeof(recvbuff));     if((sockfd = socket(af_inet, sock_stream, 0)) < 0)     {         printf("\n error : not create socket \n");         return 1;     }       memset(&serv_addr, '0', sizeof(serv_addr));       serv_addr.sin_family = af_inet;     serv_addr.sin_port = htons(5000);       if(inet_pton(af_inet, argv[1], &serv_addr.sin_addr)<=0)     {         printf("\n inet_pton error occured\n");         return 1;     }       if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)     {        printf("\n error : connect failed \n");        return 1;     }       while ( (n = read(sockfd, recvbuff, sizeof(recvbuff)-1)) > 0)     {         recvbuff[n] = 0;         if(fputs(recvbuff, stdout) == eof)         {             printf("\n error : fputs error\n");         }     }       if(n < 0)     {         printf("\n read error \n");     }       return 0; }  

server:

#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <time.h>   int main(int argc, char *argv[]) {     int listenfd = 0, connfd = 0;     struct sockaddr_in serv_addr;       char sendbuff[1025];     time_t ticks;       listenfd = socket(af_inet, sock_stream, 0);     memset(&serv_addr, '0', sizeof(serv_addr));     memset(sendbuff, '0', sizeof(sendbuff));       serv_addr.sin_family = af_inet;     serv_addr.sin_addr.s_addr = htonl(inaddr_any);     serv_addr.sin_port = htons(5000);       bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));       listen(listenfd, 10);       while(1)     {         connfd = accept(listenfd, (struct sockaddr*)null, null);           ticks = time(null);         snprintf(sendbuff, sizeof(sendbuff), "%.24s\r\n", ctime(&ticks));         write(connfd, sendbuff, strlen(sendbuff));           close(connfd);         sleep(1);      } } 

i havn't tried compiling this, , code not polished similar below trick :)

modify client end this:

if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {   printf("\n error : connect failed \n");   return 1; }   file *inputfile = fopen("inputfile.txt", "rb");  if(inputfile == null) {   fprintf(stderr, "oh no!");   return 1; }  char sendbuffer[10];  // todo: check errors here int bytesread = fread(sendbuffer, sizeof(sendbuffer), 1, inputfile);  while(!feof(inputfile)) {   //todo: check errors here   send(sockfd, sendbuffer, bytesread, 0);   bytesread = fread(sendbuffer, sizeof(sendbuffer), 1, inputfile); }  close(sockfd); 

modify server end this:

listen(listenfd, 10);   file *outputfile = fopen("output.txt", "wb");  if(outputfile == null) {   fprintf(stderr, "something went south!");   return 1; }  while(1) {     connfd = accept(listenfd, (struct sockaddr*)null, null);       char recvbuff[10];      ticks = time(null);     int bytesreceived = recv(confd, recvbuff, 10, 0);     while(bytesreceived != 0)     {        // should add error checking here       fwrite(recvbuff, bytesreceived, 1, outputfile);        bytesreceived = recv(confd, recvbuff, 10, 0);     }      close(connfd); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -