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...