c - Prob with binary files using sockets -
this not total code.
this working fine normal files text files, not working tar.gz , binary files transfer please me.
and how send chunks of memory using sockets.
server.c
void main() { int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_in my_addr; // address information struct sockaddr_in their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; char buf[16384]; char remotefile[maxdatasize]; if ((sockfd = socket(af_inet, sock_stream, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sockfd, sol_socket, so_reuseaddr, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } my_addr.sin_family = af_inet; // host byte order my_addr.sin_port = htons(myport); // short, network byte order my_addr.sin_addr.s_addr = inaddr_any; // automatically fill ip memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); printf("call binding\n"); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) { perror("bind"); exit(1); } if (listen(sockfd, backlog) == -1) { perror("listen"); exit(1); } sa.sa_handler = sigchld_handler; // reap dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = sa_restart; if (sigaction(sigchld, &sa, null) == -1) { perror("sigaction"); exit(1); } while(1) { // main accept() loop sin_size = sizeof their_addr; if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("accept"); exit(1); continue; } printf("server: got connection %s\n",inet_ntoa(their_addr.sin_addr)); if (!fork()) { // child process if ((byt=recv(new_fd, remotefile, maxdatasize-1, 0)) == -1) { perror("server recv"); exit(1); } int serverfile_fd; size_t result; printf("\nremotefile in val1 %s\n",remotefile); if((serverfile_fd = open(remotefile,o_rdonly)) < 0) { printf("error @ remotefile\n"); exit(1); } else { read(serverfile_fd, &buf[0], sizeof(buf)); } //printf("file is\n%s", buf); /* 3. sending buf in val 0*/ if (send(new_fd, buf, 16384, 0) == -1) perror("send"); close(new_fd); exit(0); }
client.c
int remote_to_local(const char *remotehost,const char *remotefile,const char *localfile) { int sockfd, numbytes,i = 0,j = 0; char buf[16384]; struct hostent *he; struct sockaddr_in s_addr; // connector's address information printf("\n"); printf("remotehost %s\n", remotehost); if ((he=gethostbyname(remotehost)) == null) { // host info perror("gethostbyname"); exit(1); } if ((sockfd = socket(af_inet, sock_stream, 0)) == -1) { perror("socket"); exit(1); } s_addr.sin_family = af_inet; // host byte order s_addr.sin_port = htons(port); // short, network byte order s_addr.sin_addr = *((struct in_addr *)he->h_addr); //inet_aton(he->h_addr, &s_addr.sin_addr); memset(s_addr.sin_zero, '\0', sizeof s_addr.sin_zero); if (connect(sockfd, (struct sockaddr *)&s_addr, sizeof s_addr) == -1) { perror("connect"); exit(1); } //send(sockfd, remotefile, maxdatasize-1, 0); val[0] = 1; printf("val 0 %d\n", val[0]); printf("val 1 %d\n", val[1]); /*1 sending val in r l*/ if (send(sockfd, val, maxdatasize-1, 0) == -1) perror("send"); printf("remotefile %s\n",remotefile); /* 2 sending remotefile in r l*/ if (send(sockfd, remotefile, maxdatasize-1, 0) == -1) perror("send"); /* 3. recieve buf in r l */ if ((numbytes=recv(sockfd, buf, 16384, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; //printf("received: \n%s",buf); int clientfile_fd; printf("local file %s\n",localfile); if((clientfile_fd = open(localfile,o_creat|o_wronly,0777)) < 0) { printf("error @ remotefile\n"); exit(1); } else { //read(clientfile_fd, &buf[0], sizeof(buf)); int result = strlen(buf); //printf("result size %d\n",result); open(localfile,o_trunc); write(clientfile_fd, &buf[0], result); } close(sockfd); return 0; }
go through code , fix/change places you:
- don't correctly handle results returned system calls recv(). if positive value returned, value safe way of finding out how data has been read buffer.
- get rid of strlen(), printf("%s...) etc. either useless, (the binary data may contain nulls , action complete early), or dangerous, (binary data contains no nulls @ , calls ub).
Comments
Post a Comment