PDA

View Full Version : bind



chrismiceli
08-30-2003, 01:59 PM
why is the bind command always return -1 in this program


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>

int main(void) {
int sv; /* Socket. */
struct sockaddr_in info;
int sinfo;
const unsigned char ipnum[] = { 123, 2, 3, 4 };

if((sv = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "Couldn't make sockets.\n");
return -1;
}
memset(&info, 0, sizeof info);
info.sin_family = AF_INET;
info.sin_port = htons(9000);
memcpy(&info.sin_addr.s_addr, &ipnum, 4);
sinfo = sizeof info;
if((bind(sv, (struct sockaddr *)&info, sinfo)) == -1) {
fprintf(stderr, "Couldn't bind ipv4 to socket.\n");
close(sv);
return -1;
}
system("netstat --unix -p | grep \"test\"");
close(sv);
return 0;
}

chrismiceli
08-30-2003, 02:38 PM
yeah, i just did that and got ip out of range or something, so I changed to something that started with 127 and it worked. thanx.

Sebastiani
08-30-2003, 11:42 PM
Try:

info.sin_addr.s_addr = inet_addr("127.0.0.1");

Lynux-Penguin
08-30-2003, 11:44 PM
I'm sorry but I have to post this here =(

but Sebastiani, your quote is soooo awsome!

-LC