transfer binary over socket

This is a discussion on transfer binary over socket within the Networking/Device Communication forums, part of the General Programming Boards category; Looking at this in the fresh morning air, I realized what a mess I had made (no thanks to master5001 ...

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looking at this in the fresh morning air, I realized what a mess I had made (no thanks to master5001, but much to RobSeace...)

    So for posterity, here's my working code for binary transfer on a local unix socket (for which there may be a few uses, who knows?):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <sys/stat.h>
    
    	
    int loclconn (char *socket, int fd) { 
    	size_t size;
    	struct sockaddr_un peer;
    	peer.sun_family=AF_LOCAL;
    	strcpy(peer.sun_path,socket);
    	size=SUN_LEN(&peer);
    	return connect(fd,(struct sockaddr*)&peer,size);
    } 
    
    
    int loclsckt (char *file) {
    	struct sockaddr_un name;
    	int sock;
    	size_t size;
    
    	if ((strlen(file)) > 107) return -2;
    	if ((sock=socket(PF_LOCAL,SOCK_STREAM,0)) < 0) return -1;
    	
    	name.sun_family = AF_LOCAL;	
    	strcpy(name.sun_path, file);
    	size=SUN_LEN(&name);
    	if ((bind(sock,(struct sockaddr*)&name,size) < 0)) return -3;
    	return sock;
    }
    
    
    int main (int argc, char *argv[]) {
    	int pid, CHLD, PRNT, IN;
    	size_t len;
    	int bytes;
    	char *buffer, *buffer2;
    	struct stat finfo;
    	FILE *fstRO = fopen(argv[1], "rb");
    	FILE *fstW = fopen("/root/test/image.jpg", "wb");
    	struct sockaddr addr;
    
    	if (fstRO == NULL) {
    		puts("valid filename required");
    		return -1;}
    	
    	stat(argv[1],&finfo);
    	bytes=finfo.st_size;
    	buffer=malloc(bytes);
    	buffer2=malloc(bytes);
    	
    	fread(buffer,bytes,1,fstRO);
    	fclose(fstRO);
    	
    	PRNT=loclsckt("/root/test/testsock");
    	listen(PRNT,5);
    	if ((CHLD=socket(PF_LOCAL,SOCK_STREAM,0)) < 0) perror("socket");
    
    	pid=fork();
    	if (pid == 0) {
    		if (loclconn("/root/test/testsock",CHLD) < 0) perror("localconn");
    		if(write(CHLD,buffer,bytes) == -1) perror("write");
    		close(CHLD);
    		return;
    	} 
    	IN=accept(PRNT,&addr,&len);
    	read(IN,buffer2,bytes);
    	close(IN);
    	close(PRNT);
    	unlink("/root/test/testsock");
    
    	fwrite(buffer2,bytes,1,fstW);
    	fclose(fstW);
    }
    Last edited by MK27; 10-09-2008 at 10:52 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  2. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21