Thread: Errors in my c socket program

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    79

    Errors in my c socket program

    Hey a while ago i started writing a c program to learn, i wanted to send a file using sockets. I have just come back from a 6 month holiday and am struggling to understand what i was doing. I kind of understand it, but in trying to get my latest code to compile im getting errors that i don't understand.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/wait.h>
    #include <errno.h>
    
    
    void bail (char *msg){
    	fputs(strerror(errno),stderr);
    	fputs(": ",stderr);
    	fputs(msg,stderr);
    	fputc('\n',stderr);
    	exit(1);
    
    }
    
    char *resolve (const char *hostname){
    
    	struct hostent *hp;
    	hp = gethostbyname(hostname);
    
    	if(!hp){
    
    		bail("Error Resolving host");
    
    	}
    	char rstr = sprintf("%s",hp->h_addr_list[0]); 
    	return hp->h_addr_list[0];
    
    
    
    }
    void recieveFile(int *s, struct sockaddr_in *adr){
    
    	char buff[1024];	
    	int bytes;
    	while(1){
    		
    		bytes = recv(*s,(struct sockaddr *) adr, buff,strlen(buff));
    		printf("Packet Recieved %d Bytes\n"bytes);
    		if(strcmp(buff,"--END--")==0)
    			break;
    
    		if(bytes < 0)
    			bail("Error recieving data\n");
    
    	}
    	printf("File Transfer Finished\n");
    
    
    }
    
    void sendFile(int *s, struct sockaddr_in *adr, char *filename){
    
    	FILE *p = fopen(filename,"rb");
    	if(p == NULL){
    		bail("Error opening file");
    	}
    	int bytes = 0;
    	int totbytes = 0;
    	char buff[1024];
    	int sbytes;
    
    	while(1){
    		
    		bytes = fread(buff,sizeof(char), 1024,p);
    		//send the data
    		sbytes = send(*s,(struct sockaddr *)adr, buff,strlen(buff));
    		printf("Packet Sent %d bytes: \n"bytes);
    		if(feof(p))
    			break;
    
    		if(sbytes < 0)
    			bail("Error sending file.")
    	
    	}
    	send(*s,(struct sockaddr *)adr, "--END--",7);
    	
    
    }
    
    int makeConnection(char *ip, int port){
    	int s;
    	struct sockaddr_in connectto;
    	if((s = socket(AF_INET,SOCK_STREAM,0)) == -1) 
    			bail("Socket Error"); 
    
    	memset(&connectto,0,sizeof(connectto));
    	connectto.sin_family = AF_INET;
    	connectto.sin_addr.s_addr = inet_addr(ip);
    	connectto.sin_port = htons(port);
    	
    	if (connect(s,(struct sockaddr *)&connectto,sizeof(connectto)) < 0) 
    		bail("Connection Error");
    
    	//we have connected.
    	printf("We have connected.\n\n");
    
    }
    int makeaddr(void * addr, int len, int *s, char *type, char *ip, int port){
    
    	//takes &addr,sizeof(addr), &sock,"TCP",ipaddr, port number
    	int Default_port = 3099;
    	int yes = 1;
    	struct sockaddr_in *ad = (struct sockaddr_in *)addr; 	
    
    	memset(ad,0,len);
    
    	ad->sin_family = AF_INET;
    
    	if(!ip){
    	
    		ad->sin_addr.s_addr = INADDR_ANY;
    
    	}
    
    	else{
    		ad->sin_addr.s_addr = inet_addr(ip);
    
    	}
    
    	if(port == 0){
    		printf("\n Port = 0\nDefault port = %d\n",Default_port);
    		ad->sin_port = htons(Default_port);
    	
    
    	}
    	else{
    		Default_port = port;
    		ad->sin_port = htons(port);
    
    	}
    
    	
    	
    	//we have created the address structure. Now create the socket
    
    	if(strcmp(type,"TCP")==0){
    
    		if((*s = socket(AF_INET,SOCK_STREAM,0))== -1)
    			bail("TCP Sock Error");
    
    		if(setsockopt(*s, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))==-1)
    			bail("Unable to set socket options");
    
    		if(bind(*s,(struct sockaddr*)ad,len)== -1){
    
    			bail("TCP Bind Error");
    
    		}
    
    		printf("TCP Socket Successfully created: On Port %d \n",Default_port);
    
    	}
    
    	else if(strcmp(type,"PASS")==0){
    		//nothing.
    	}
    
    	else{
    
    
    		if((*s = socket(AF_INET,SOCK_DGRAM,0))== -1)
    			bail("UDP Sock Error");
    
    		if(setsockopt(*s, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))== -1)
    			bail("Unable to set socket options");
    		if(bind(*s,(struct sockaddr*)ad,len)== -1){
    
    			bail("UDP Bind Error");
    
    		}
    		printf("UDP Socket Successfully created: On Port %d \n",Default_port);
    
    	}
    	
    	
    
    
    
    
    
    
    }
    
    int main(int argc, char *argv[]){
    
    	int sock;
    	int clisock;
    	struct sockaddr_in addr, cli_addr;
    
    	//takes &addr,sizeof(addr), &sock,"TCP",ipaddr, port number
    	//makeaddr(&addr,sizeof(addr),&sock,"UDP",NULL,0);
    
    	int flag_listen = 0;
    	int flag_listen_port = 0;
    	int flag_sendFile = 0;
    	int flag_filename = 0;
    	int i = 1;
    	int cli_len;
    	int flag_getfile = 0;
    	int flag_getfilePort = 0;
    
    	for(i = 0; i < argc; i++){
    
    		if(strcmp(argv[i],"-l")==0){
    	
    			flag_listen = i;
    			flag_listen_port = i + 1;
    			if(argc <= flag_listen_port)
    				bail("No Port Number Set\n");
    
    		}
    
    
    		if(strcmp(argv[i],"-sF")==0){
    			
    			if(flag_listen == 0)
    				bail("You need to specify a -l [portnumber] to use this function.\n");
    			flag_sendFile = i;
    			flag_filename = i + 1;
    			if(argc <= flag_filename)
    				bail("No Filename Set\n");
    
    			
    
    		}
    
    		
    		if(strcmp(argv[i],"-gF")==0){
    
    			flag_getfile = i;
    			flag_getfilePort = i + 1;
    			if(argc <= flag_getfilePort)
    				bail("Specify Port Number To connect too\n");
    
    		}
    
    
    	
    
    	}
    
    
    
    	//we got here we might have a logical command.
    
    	if(flag_getfile > 0 && flag_getfilePort > 1){
    
    		makeConnection("127.0.0.1", atoi(argv[flag_getfilePort]));
    		recieveFile(&sock,)
    		/*makeaddr(&addr,sizeof(addr),&sock,"PASS","127.0.0.1",atoi(argv[flag_getfilePort]));
    		if(sock = socket(AF_INET,SOCK_STREAM,0) == -1)
    				bail("Socket Error"); 
    
    		if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0)
    				bail("Error On Connection."); //errors here socket operation on non socket. 
    		int bytes;
    		char buff[256];
    		int pkt = 1;
    		
    		for(;;){
    
    			bytes = recv(sock,buff,sizeof(buff),0);
    			printf("Packet Recieved %d\n",pkt);
    			printf("Total Bytes: %d\n",bytes);
    			printf("Dump:\n%s",buff);
    
    			if(strcmp(buff,"END")==0)
    				break;
    	
    		}
    
    		printf("loop Exit.");*/
    		close(sock);
    	
    			
    	
    	
    
    	}
    	if(flag_listen > 0 && flag_sendFile > 0){
    		
    		char sndBuf[256];
    		FILE *p;
    		//program didnt bail out so must have logical port command
    		 p = fopen(argv[flag_filename],"r");
    			//bail(argv[flag_filename]);
    
    		//make socket address
    		makeaddr(&addr,sizeof(addr),&sock,"TCP",NULL,atoi(argv[flag_listen_port]));
    		//we should now have a bind socket.
    		printf("Listening on port %d - Waiting for connection...\nReady to send file: %s\n",atoi(argv[flag_listen_port]),argv[flag_filename]);
    		listen(sock,5);
    		
    		cli_len = sizeof(cli_addr);
    		if(clisock = accept(sock,(struct sockaddr *) &cli_addr, &cli_len)== -1)
    			bail("Error Accepting Remote connection\n");
    		
    		printf("Connection Established. Sending File: %s\n\n",argv[flag_filename]);
    		
    		//read and write.
    		int bytes_sent;
    		int jj = 1;
    		//sndBuf
    		
    		while(fgets(sndBuf,256,p)!= NULL){
    			
    			//write the file to the client.
    			bytes_sent = send(clisock,sndBuf,sizeof(sndBuf),0);
    			printf("Sent packet %d\n",jj);
    			jj++;
    			printf("Total Bytes Sent: %d\n",bytes_sent); //this always remains at -1 error??
    			printf("Dump:\n%s\n\n",sndBuf);
    			//sleep(2); //wont sleep keeps sending packets of data very fast
    
    		}
    		int k = strlen("END");
    		send(clisock,"END",k,0);
    	}
    
    
    	else{printf("Usage: ./ma [-sF send file] [-gF get File]\n");}
    
    	close(sock);
    	return 0;
    }

    Here are the compiler problems

    makeaddress.c: In function ‘recieveFile’:
    makeaddress.c:45: warning: passing argument 3 of ‘recv’ makes integer from pointer without a cast
    makeaddress.c:46: error: expected ‘)’ before ‘bytes’
    makeaddress.c: In function ‘sendFile’:
    makeaddress.c:74: warning: passing argument 3 of ‘send’ makes integer from pointer without a cast
    makeaddress.c:75: error: expected ‘)’ before ‘bytes’
    makeaddress.c:82: error: expected ‘;’ before ‘}’ token
    makeaddress.c:83: warning: passing argument 3 of ‘send’ makes integer from pointer without a cast
    makeaddress.c: In function ‘main’:
    makeaddress.c:257: error: expected expression before ‘)’ token

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    bytes = recv(*s,(struct sockaddr *) adr, buff,strlen(buff));
    recv
    You could try RTFM before passing any random garbage to a function.

    Actually, NOT doing that before posting on a forum is quite frankly annoying.
    The error messages seem obvious enough. The parameters are wrong, so RTM and find out what you should be passing.

    Also, figure out what strlen(buff) is likely to return when your buff is UNINITIALISED.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    wow thanks, what a kind reply. Im sorry i annoyed you with my silly questions. I must learn not to waste such important peoples time. Out of us two its really clear im the dick here.


    Quote Originally Posted by Salem View Post
    bytes = recv(*s,(struct sockaddr *) adr, buff,strlen(buff));
    recv
    You could try RTFM before passing any random garbage to a function.

    Actually, NOT doing that before posting on a forum is quite frankly annoying.
    The error messages seem obvious enough. The parameters are wrong, so RTM and find out what you should be passing.

    Also, figure out what strlen(buff) is likely to return when your buff is UNINITIALISED.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hey, until you read to learn error messages, I´ll just ignore you from now on.
    Bye bye, enjoy your error message confusion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unlocked again, just in case you feel like whining about the unfair universe.
    Not that much will happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mushy View Post
    wow thanks, what a kind reply. Im sorry i annoyed you with my silly questions. I must learn not to waste such important peoples time. Out of us two its really clear im the dick here.
    Mushy... take a nice deep breath. I don't get the sense that Salem was being mean.

    He was doing what I might do in his place... trying to direct you to the tools that will help you learn while solving your problems. We could probably just give you the answers and you'd be no further ahead next time this happens. But if we get you to the information you need and show you how to use it, next time you know exactly how to fix it...

    Old saying: "Give a man a fish and he's fed for one day. Teach a man to catch a fish and you've fed him for life".

    The best help does not always come from easy answers, my friend.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    thanks. I started a new thead on it, i got it compiled. I just get frustrated because ive tried to transfer a file in c sockets for ages and can never do it. I even bought a book linux socket programming in c and it explains everything well until it gets to select and sending files then it just goes off track and starts talking about a rpn calculator.

    Its just so hard to get info on how to do it, when it seems so easy but i cant get it to work.
    ideally i just want to make a basic filserver using select and send / recv but i cant get it to work

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mushy View Post
    thanks. I started a new thead on it, i got it compiled. I just get frustrated because ive tried to transfer a file in c sockets for ages and can never do it. I even bought a book linux socket programming in c and it explains everything well until it gets to select and sending files then it just goes off track and starts talking about a rpn calculator.

    Its just so hard to get info on how to do it, when it seems so easy but i cant get it to work.
    ideally i just want to make a basic filserver using select and send / recv but i cant get it to work
    I understand your frustration. I've shared it many times. Sockets and winsock are probably the most frustrating code there is. In the time I can write a simple winsock program, I could probably knock out 5 or 6 other programs of similar size.

    I found this to be very helpful...

    Winsock Programmer&rsquo;s FAQ: Winsock Programmer's FAQ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. multi-thread socket program with packet fragmentation
    By thinkerchjf in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-25-2009, 10:46 AM
  3. Errors in Dev-C++ program
    By sinking2008 in forum C Programming
    Replies: 5
    Last Post: 05-15-2008, 09:00 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM