Can't get BSD bind() to work.
Can't get BSD bind() to work.
- Subject: Can't get BSD bind() to work.
- From: Ken Baer <email@hidden>
- Date: Tue, 4 Oct 2005 16:11:49 -0700
I'm trying to create a simple listening socket in my app with BSD sockets. I am unable to get bind() to work when passing it the port I want to use, 780. It does work if I set the port to 0, but that's not what I want. I made a simple C app that I built in XCode that exhibits this problem. All the sample code examples I've found (which this code comes from) are virtually identical. Is there something I should do using setsockopt() that I'm missing? The following code results in bind() failing, and errno being set to 13.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#define MAX_BUF 100
int main(int argc, char* argv[])
{
int sockd;
int count;
struct sockaddr_in serv_name;
char buf[MAX_BUF];
int status;
/* create a socket */
sockd = socket(AF_INET, SOCK_STREAM, 0);
if (sockd == -1)
{
perror("Socket creation");
exit(1);
}
struct sockaddr_in my_name;
/* server address */
my_name.sin_family = AF_INET;
my_name.sin_addr.s_addr = INADDR_ANY;
u_short port = 780;
my_name.sin_port = htons(port);
status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));
int error = errno;
if (status == -1)
{
perror("Binding error");
exit(1);
}
return 0;
}
-Ken Baer.
Hash, Inc.
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden