Hanging code:

This is a discussion on Hanging code: within the C Programming forums, part of the General Programming Boards category; this code hangs at line: Code: if (connect(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) complete code: Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Hanging code:

    this code hangs at line:

    Code:
    if (connect(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    complete code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <errno.h>
    
    char *ip;
    char filename[32];
    char buffer[256];
    unsigned short port;
    int serverSocket;
    unsigned long filesize;
    FILE *readFile, *writeFile;
    struct sockaddr_in serverAddr;
    char done;
    
    int main (int argc, const char * argv[]) {
    	printf("rev: 2\n");
    	if (argc > 1)
    	{
    		ip = malloc(15*sizeof(char));
    		strncpy(ip, argv[1], sizeof(ip));
    		if (argc > 2)
    			port = atoi(argv[2]);
    		else
    		{
    			printf("Usage: fEcho [ip] [port]\n--using: %s:65530\n", ip);
    			port = 65530;
    		}
    	}
    	else
    	{
    		
    		ip = malloc(15*sizeof(char));
    		ip = "127.0.0.1";
    		port = 65530;
    		printf("Usage: fEcho [ip] [port]\n--using: %s:%d\n", ip, port);
    	}
    	printf("Filename: ");
    	scanf("%s", filename);
    	fflush(stdouot);
    	readFile = fopen(filename, "r");
    	strcat(filename, ".copy");
    	writeFile = fopen(filename, "w");
    	memset(&serverAddr, 0, sizeof(serverAddr));
    	serverAddr.sin_family = AF_INET;
    	serverAddr.sin_port = htons(port);
    	serverAddr.sin_addr.s_addr = htonl(inet_addr(ip));
    	if (ip != NULL)
    	{
    		//	free(ip);
    		ip = NULL;
    	}
    	
    	serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    	if (serverSocket < 0)
    	{
    		printf("Error creating socket\n");
    		exit(1);
    	}
    	if (connect(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
    	{
    		printf("Error: connect()\n");
    		exit(1);
    	}
    	for(;;)
    	{
    		printf("Tranfering file:", stdout);
    		while(fgets(buffer, sizeof(buffer), readFile) != NULL)
    		{
    			send(serverSocket, &buffer, sizeof(buffer), 0);
    			//printf(".");
    			memset(&buffer, 0, sizeof(buffer));
    			listen(serverSocket, 1);
    			recv(serverSocket, &buffer, sizeof(buffer), 0);
    			//printf(".");
    			fprintf(writeFile, "%s", buffer);
    		}
    		fclose(writeFile);
    		fclose(readFile);
    		printf("Next file['q' to quit]:");
    		scanf("%s", filename);
    		if (strcmp(filename, "q") == 0)
    		{
    			close(serverSocket);
    			return(0);
    		}
    		readFile = fopen(filename, "r");
    		strcat(filename, ".copy");
    		writeFile = fopen(filename, "w");
    	}
    	return(0);
    }

    I'm just absolutely baffled...


    TIA

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know that it's really hanging there? I can see a number of things could take a bit of tweaking:
    Code:
    	for(;;) you are in an infinite loop...
    	{
    		printf("Tranfering file:", stdout); you don't flush your output stream
    		while(fgets(buffer, sizeof(buffer), readFile) != NULL) assume it reads...
    		{
    			send(serverSocket, &buffer, sizeof(buffer), 0); you don't check any of these return values
    			//printf(".");
    			memset(&buffer, 0, sizeof(buffer));
    			listen(serverSocket, 1);
    			recv(serverSocket, &buffer, sizeof(buffer), 0);
    			//printf(".");
    			fprintf(writeFile, "%s", buffer);
    		}
    		fclose(writeFile);
    		fclose(readFile); 
    		printf("Next file['q' to quit]:"); you still haven't flushed your output..
    		scanf("%s", filename); you might just be sitting here...
    		if (strcmp(filename, "q") == 0)
    		{
    			close(serverSocket);
    			return(0);
    		}
    		readFile = fopen(filename, "r");
    		strcat(filename, ".copy");
    		writeFile = fopen(filename, "w");
    	}
    I would advise checking the return values of all of your functions, and flush the output of all of your attempts. You may just be sitting there waiting for you to type something. Add a \n on the end of all of your prints, or fflush( stream ). That way you'll know for sure. If it is really hanging at connect, then you'll probably need to see that what you're really trying to connect to can actually be connected to (though again, I doubt it's actually connect that is hanging).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    The exact problem is in this line ,

    Code:
    	serverAddr.sin_addr.s_addr = htonl(inet_addr(ip));
    It should be,
    Code:
           
     serverAddr.sin_addr.s_addr = inet_addr(ip);
    Because inet_addr is used to convert the host byte order ip to network byte order ip . But htonl is used to convert the hostlong integer to network long integer .So no need to use this for the ip.inet_addr is enough .
    Last edited by karthigayan; 03-18-2010 at 05:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code is hanging - Very simple C (Beginner)
    By pc_doctor in forum C Programming
    Replies: 10
    Last Post: 10-29-2007, 06:05 PM
  2. Can't figure out why code is hanging up
    By Panserbjorn in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 06:09 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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