Thread: help with UNIX sockets.... please

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    help with UNIX sockets.... please

    I'm writing some code for a project. I'm kind of new to this type of programming. Basically I'm trying to send packets from one process to another using UDP sockets. I'm running the sender and reciever processes in two different shells on the same host. My code works for the first packet then when I try to get the second packet it prints the error "bad file name" in the receivers shell... I've been messing with it for about an hour and I tried googling and I couldn't find anything that helped me. I would really appreciate any help you could offer. Here is my code...

    TCP.h defines the port numbers for TO_DELTA and CLIENT_HOST.

    This is the test process...
    Code:
    #include "TCP.h"
    #include "delta.h"
    
    int main(void){
    	start_timer(1,5);
    	start_timer(1,6); 
    	return 0;
    }
    It's testing code from delta.c. The code for start_timer is...
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include "TCP.h"
    #include "delta.h"
    
    /* delta packet data type */
    typedef struct DeltaPacket {
    	unsigned int time;
    	unsigned int seq;
    } DeltaPacket;
    
    /* starts a timer for a packet */
    int start_timer(unsigned int tm, unsigned int sq){
    	int sock;
            struct sockaddr_in name;
            struct hostent *hp, *gethostbyname();
    
    	/* make a socket to send to delta */
    	sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if(sock < 0) {
    		perror("opening TCPD to delta socket");
    		return -1;
    	} 
    	
    	/* set up name */
    	name.sin_family = AF_INET;
            name.sin_port = htons(TO_DELTA);
    	
    	/* convert client hostname to IP address and enter into name */
           hp = gethostbyname(CLIENT_HOST);
           bcopy((char *)hp->h_addr, (char *)&name.sin_addr, hp->h_length);
    	
    	/* send packet to delta */
    	DeltaPacket packet;
    	packet.time = tm;
    	packet.seq = sq;
    	int bytes_sent = sendto(sock, (char*)&packet, 64, 0, (struct sockaddr *)&name, sizeof(name));
        
    	if(bytes_sent!=64) {
    	      perror("error sending delta packet.\n");
    	      close(sock);
    	      return -1;
            }
    	
    	/* close socket */
    	close(sock);
    	return 1;
    }
    and then the process that is receiving the packet has this code...
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include "TCP.h"
    #include "delta.h"
    
    int main(void) {
    	int recv_sock;
    	struct sockaddr_in name;
    	
    	/* makes a socket to listen for packets */
    	recv_sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if(recv_sock < 0) {
    		perror("opening TCPD to delta socket");
    		exit(1);
    	}
    	
    	/* create name with parameters and bind name to socket */
           name.sin_family = AF_INET;
           name.sin_port = htons(TO_DELTA);
           name.sin_addr.s_addr = INADDR_ANY;
           if(bind(recv_sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
    		perror("getting to delta socket name");
    		exit(2);
           }
    	
    	printf("Timer ready.\n");
    
    	/* block for first packet */
    	int namelen = sizeof(name);
    	DeltaPacket packet;
    	int recv = recvfrom(recv_sock, (char*)&packet, 64, 0,(struct sockaddr *)&name, &name);
    	if (recv != 64){
    		perror("error receiving delta packet."); 
    		close(recv_sock);
    		exit(3);
           }
    	printf("Seq #%i recieved!\n", (int) packet.seq);
    	
    	recv = recvfrom(recv_sock, (char*)&packet, 64, 0,(struct sockaddr *)&name, &name);
    	if (recv != 64){
    		perror("error receiving delta packet."); 
    		close(recv_sock);
    		exit(3);
            }
    	printf("Seq #%i recieved!\n", (int) packet.seq);
    	
    	close(recv_sock);
    	return 0;
    }
    as far as I can tell the problem is somewhere on the receiving end because the test process runs the way I would expect. It sends the packets without printing any errors. It would print errors if the packets weren't sent correctly... I really dont know what "bad file name" means...

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by osubuckeye84 View Post
    as far as I can tell the problem is somewhere on the receiving end because the test process runs the way I would expect. It sends the packets without printing any errors. It would print errors if the packets weren't sent correctly... I really dont know what "bad file name" means...
    Ummm... don't count on that.
    I recently spent most of a week trying to figure out why packets weren't being sent... on Windows at least, sockets is quite happy to silently not send a packet, with the only indication of failure being that it's not received on the other end.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Can you be more specific about how/where you're seeing the error? Maybe a copy & paste of a sample run?

    One thing I see is a memory access violation:
    Code:
    typedef struct DeltaPacket {
    	unsigned int time;
    	unsigned int seq;
    } DeltaPacket;
    You're reading 64 bytes of memory starting at the packet address (in the sender), but that struct is, I'm betting, 8 bytes. That means you're reading (and in the receiver, writing) past the end of the memory allocated for the packet. Maybe use sizeof(DeltaPacket) instead of a "magic number" like 64.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Oh wow I feel dumb now. I was thinking 64 bits (32 bit int + 32 bit int)... but it should be 8 bytes... thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 12:20 AM
  2. Difference between Unix sockets and winsock.
    By antex in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-21-2005, 04:53 PM
  3. Winsock vs Unix Sockets
    By khoxxxy in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-05-2003, 05:13 AM
  4. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 09:16 AM
  5. new to unix sockets
    By lithium in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 05:53 AM