Problem with network code

This is a discussion on Problem with network code within the Linux Programming forums, part of the Platform Specific Boards category; Hello, i have the following network code. If i call the function sendDataToServer, sometimes i get random core dumps. I ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    Problem with network code

    Hello,
    i have the following network code. If i call the function sendDataToServer, sometimes i get random core dumps. I know its bad code, but its also not mine ... i would be very happy if you guys could help me out. Thank you.

    Code:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include "NetworkHandler.h"
    
    #define RECEIVE_BUFFER 8000
    
    /* "Private" Functions */
    char *send_message (int socket, char *message);
    int createConnection (char *serv_host, int port);
    void close_connection (int socket);
    
    char *send_message (int socket, char *message)
    {
        int len = strlen (message);
        char messageFromSocket [RECEIVE_BUFFER];
        char *ret = "";
        char *tmpret;
    	int anzBytesRead, totalBytesRead, status;
    	
    	status = write(socket, message, len);
    	if (status <= 0) {
    		printf ("Error while trying to write to the server.\n");
    		return ret;
    	}
    	read (socket, messageFromSocket, RECEIVE_BUFFER);
    
        return messageFromSocket;
    }
    
    int createConnection (char *serv_host, int port)
    {
        int socket;
        struct hostent *host_ptr;
        struct sockaddr_in serv_addr;
    	
    	host_ptr = malloc(sizeof(struct hostent));
    	/* get the address of the host */
        if((host_ptr = gethostbyname(serv_host)) == NULL) {
            perror("ERROR: Hostname");
            exit(1);
        }
        if(host_ptr->h_addrtype !=  AF_INET) {
            perror("unknown address type");
            return -1;
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr =
                ((struct in_addr *)host_ptr->h_addr_list[0])->s_addr;
        serv_addr.sin_port = htons(port);
    	/* open a TCP socket */
        if((socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
           perror("Can't open stream socket");
           exit(1);
        }
    	/* connect to the server */
        if(connect(socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
           perror("Can't connect to server");
           exit(1);
        }
    	free(host_ptr);
        return socket;
    }
    
    void close_connection (int socket) {
        close (socket);
    }
    
    int sendDataToServer(char *dataMessage){
    	int socket = createConnection("someserver", 2000);
    	send_message(socket, dataMessage);
    	close_connection(socket);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > I know its bad code, but its also not mine
    This site isn't for tech support for other people's buggy code.
    Given the number and type of bugs, I would not accept any more code from this person.

    I'm surprised it runs at all, nevermind most of the time.

    1. char messageFromSocket [RECEIVE_BUFFER];
    VERY BAD - You return a pointer to this local array.

    2. int anzBytesRead, totalBytesRead, status;
    Fairly harmless, prune unused variables.

    3. status = write(socket, message, len);
    Poor - Assuming that writing to a socket always succeeds in sending the entire message

    4. read (socket, messageFromSocket, RECEIVE_BUFFER);
    Very poor - unchecked return result.
    Also, given your prior assumption that your data is strings, there is no implicit '\0' in this data.

    5. host_ptr = malloc(sizeof(struct hostent));
    VERY BAD - gethostbyname() returns it's own pointer, you don't malloc one for it. All you have now is a memory leak which is caused when you overwrite the pointer with host_ptr = gethostbyname(serv_host)

    6. free(host_ptr);
    CATASTROPHIC - freeing memory which isn't yours.

    7. int socket = createConnection("someserver", 2000);
    Poor - at least one path in the called function can return -1, which goes unchecked into other functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with compiling code with option -O2
    By koushikyou in forum C Programming
    Replies: 16
    Last Post: 01-07-2009, 05:03 AM
  2. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

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