Last thread locked, but still need help. c sockets

This is a discussion on Last thread locked, but still need help. c sockets within the C Programming forums, part of the General Programming Boards category; This now compiles but gives errors when i try to transfer the files, i don't know why. I think it ...

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

    Last thread locked, but still need help. c sockets

    This now compiles but gives errors when i try to transfer the files, i don't know why. I think it might be because im passing the socket to the function and its causing errors. If this is the case i don't know a way around it. As i say im new and have semi tried many times and never been able to get file transfer to work over sockets.

    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){
    
    	char buff[1024];	
    	int bytes;
    	while(1){
    		
    		bytes = recv(*s, buff,124,0);
    		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,buff,1024,0);
    		printf("Packet Sent %d bytes: \n",bytes);
    		if(feof(p))
    			break;
    
    		if(sbytes < 0)
    			bail("Error sending file.");
    	
    	}
    	send(*s,"--END--",7,0);
    	
    
    }
    
    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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by mushy View Post
    This now compiles but gives errors when i try to transfer the files
    What are the errors that it gives?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    Packet Recieved -1 Bytes
    Bad file descriptor: Error recieving data

    then program exits, thats the client side anyway.

    Sendfile part sends 107 packets which seems to work to a degree though every packet size is shown as -1

    an example of sendfile output is the following
    Code:
    Sent packet 29
    Total Bytes Sent: -1
    Dump:
        $val2 = "SELECT * FROM tbl_article WHERE pID = " .$r;
    
    
    Sent packet 30
    Total Bytes Sent: -1
    Dump:
        $res2 = mysql_query($val2) or die (mysql_error());
    
    
    Sent packet 31
    Total Bytes Sent: -1
    Dump:
        if(mysql_num_rows($res2) < 1){
    
    
    Sent packet 32
    Total Bytes Sent: -1
    Dump:
        
    
    
    Sent packet 33
    Total Bytes Sent: -1
    Dump:
        echo "<div class = 'adb'><div class = 'adbtName'>This user has no Articles</div></div>";
    
    
    Sent packet 34
    Total Bytes Sent: -1
    Dump:
        
    
    
    Sent packet 35
    Total Bytes Sent: -1
    Dump:
        }
    
    
    Sent packet 36
    Total Bytes Sent: -1
    Dump:
        
    
    
    Sent packet 37
    Total Bytes Sent: -1
    Dump:
        else{
    
    
    Sent packet 38
    Total Bytes Sent: -1
    Dump:
          
    
    
    Sent packet 39
    Total Bytes Sent: -1
    Dump:
           $nq = "SELECT * FROM tbl_users WHERE USERID = " .$r;

    it seems to be reading from the file, one line at a time as opposed to a whole chunk that i wanted.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    The only call I see to recieveFile[sic] is a call using a sock that has not been initialized. Which would certainly lead to the error bad file descriptor.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    79
    umm i see now. Makeconnection creates a socket. Can i get it to return the socket or will that not work. I.e int s =makeconnection. Then just have make connection return int sock?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  3. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  4. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM

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