954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

bind() returning -1 (for a tftp server)

I am trying to write a TFTP server. I have successfully completed the code, it is fully RFC 1350 compliant.
However, I wrote the whole thing in Haiku OS, and in Haiku, it works great. However, I also need a DHCP server, and I don't have one for Haiku. So I need to port my TFTP server to Linux.
Now the code compiles fine in linux, but does not receive any packets sent to it. I can do wireshark captures and see that the packets are arriving, but my server does not receive them. I added some debugging printfs and found that bind is returning a -1 (instead of 0 like in Haiku). I am running the executable as root, so its not a permissions issue. Am I doing something wrong? Here is some of my code:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
int sock, length, n;
struct sockaddr_in server;
struct hostent *hp;
unsigned char buffer[1024];

//Set up and open datagram socket
sock=socket(AF_INET, SOCK_DGRAM, 0);

//Setup socket properties
server.sin_family = AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port = htons(69);

length = sizeof(struct sockaddr_in);

n=bind(sock,(struct sockaddr *)&server,length);
//Finish setting up datagram socket. It should be open.

printf("%d\n",n);


So where did I go wrong?

prushik
Junior Poster
101 posts since Oct 2007
Reputation Points: 61
Solved Threads: 5
 

http://beej.us/guide/bgnet/output/html/multipage/syscalls.html#bind

Since your port is <1024, are you running as root?

Some more error checking wouldn't go amiss either. You might have found the error to be "EPERM" or something.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

http://beej.us/guide/bgnet/output/html/multipage/syscalls.html#bind

Since your port is <1024, are you running as root?

Some more error checking wouldn't go amiss either. You might have found the error to be "EPERM" or something.

I am running the executable as root, so its not a permissions issue.


Yes, I am running as root. sudo ./tftps
I'm thinking maybe something else is using that port, but I uninstalled all other tftp severs, and I see no other traffic on wireshark.
Meanwhile, i'll add some more error checking. I have a tendency to use printf for all debugging.

prushik
Junior Poster
101 posts since Oct 2007
Reputation Points: 61
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: