int socket_init(int port)
{
WSADATA wsaData;
int my_sockfd;
struct sockaddr_in my_addr;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
MessageBox(NULL, "Could not initialize socket library.", "WSAStartup", MB_OK);
exit(1);
}
if (my_sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), ''\0'', 8);
if (bind(my_sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(my_sockfd, 1) == -1) {
perror("listen");
exit(1);
}
return my_sockfd;
}
[\code]
Whenever I do, perror prints "Bind: No error." If I comment the if structure with the bind function in it, perror prints "Listen: No error." Why won''t it just bind and listen?
I''m using VC++ 7 and I am running Windows XP.
Thanks!
Lysandius
Hi there!
I''m trying to execute the following code:
April 03, 2003 10:17 AM
In the bind() statement, the size should be of ''struct sockaddr_in ''. Also, you should use WSAGetLastError() for getting the network error.
I get the same "error" when I try that.
I typecast a sockaddr_in structure to a sockaddr structure. So it should be the size of a sockaddr structure. But I still tried :-)
I typecast a sockaddr_in structure to a sockaddr structure. So it should be the size of a sockaddr structure. But I still tried :-)
Your are mixing winsock with BSD sockets :-).. you have probably read both windows and linux/unix tutorials. Under windows a socket is of the type SOCKET and not an int. As far as I know perror() isn''t supported on windows either.
You would want an init_socket function which calls listen on the socket anyway.. what if you want to create sockets which are not listening?.. Looks like you have taken code from Beej''s guide and mixed it with code from the winsock reference :-).. heres some winsock code:
here is a couple of functions you mind find handy.. they are from a little winsock wrapper I wrote along time ago. I''ve have omitted to functions wrapping select and some packet packing functions because they suck :-)
of and the NET_OK and NET_ERROR_something are defines (old school error checking).
the reason the wrapper sucks is that my packet handling are relying on the same endianess on both machines and that they rely on you using blocking sockets..
You would want an init_socket function which calls listen on the socket anyway.. what if you want to create sockets which are not listening?.. Looks like you have taken code from Beej''s guide and mixed it with code from the winsock reference :-).. heres some winsock code:
here is a couple of functions you mind find handy.. they are from a little winsock wrapper I wrote along time ago. I''ve have omitted to functions wrapping select and some packet packing functions because they suck :-)
of and the NET_OK and NET_ERROR_something are defines (old school error checking).
// errors#define NET_OK 0#define NET_ERROR 1#define NET_ERROR_NOWINSOCK 2#define NET_ERROR_BIND 3#define NET_ERROR_CONNECT 4#define NET_ERROR_LISTEN 5#define NET_ERROR_CLOSE 6#define NET_ERROR_RESOLVE 7#define NET_ERROR_PACKETSEND 8#define NET_ERROR_PACKETRECV 9#define NET_ERROR_CONNECTIONCLOSED 10int NET_Init(){ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 2, 0 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return NET_ERROR_NOWINSOCK; } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) { WSACleanup(); return NET_ERROR_NOWINSOCK; } return NET_OK;}int NET_Shutdown(){ err = WSACleanup(); if( err != 0 ) return NET_ERROR; return NET_OK;}SOCKET NET_CreateSocket(){ SOCKET temp = socket(AF_INET, SOCK_STREAM, 0); return temp;}int NET_BindSocket(SOCKET sockfd, WORD port){ struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(port); // short, network byte order my_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(&(my_addr.sin_zero), ''\0'', 8); // zero the rest of the struct err = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); if( err != 0 ) return NET_ERROR_BIND; return NET_OK;}int NET_ConnectSocket(SOCKET sockfd, WORD port, char *dest_ip){ struct sockaddr_in dest_addr; // will hold the destination addr dest_addr.sin_family = AF_INET; // host byte order dest_addr.sin_port = htons(port); // short, network byte order dest_addr.sin_addr.s_addr = inet_addr(dest_ip); memset(&(dest_addr.sin_zero), ''\0'', 8); // zero the rest of the struct err = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)); if( err != 0 ) return NET_ERROR_CONNECT; return NET_OK;}int NET_Listen(SOCKET sockfd, int backlog){ err = listen(sockfd,backlog); if( err != 0 ) return NET_ERROR_LISTEN; return NET_OK;}SOCKET NET_Accept(SOCKET sockfd, struct sockaddr_in *their_addr){ SOCKET new_fd; // listen on sock_fd, new connection on new_fd int sin_size; sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)their_addr, &sin_size); return new_fd;}int NET_Send(SOCKET sockfd, char *buffer, int len){ int bytes_sent = send(sockfd, buffer, len, 0); return bytes_sent;}int NET_Recv(SOCKET sockfd, char *buffer, int len){ int bytes_received = recv(sockfd, buffer, len, 0); return bytes_received;}int NET_CloseSocket(SOCKET sockfd){ err = closesocket(sockfd); if( err != 0 ) return NET_ERROR_CLOSE; return NET_OK;}int NET_Resolve(char *hostname, char *ip_address){ struct hostent *h; if( (h=gethostbyname(hostname)) == NULL ) { return NET_ERROR_RESOLVE; } strcpy(ip_address, inet_ntoa(*((struct in_addr *)h->h_addr))); return NET_OK;}int NET_SetBlocking(SOCKET sockfd, bool state){ unsigned long arg; if( state ) arg = 1; else arg = 0; err = ioctlsocket(sockfd, FIONBIO, &arg); if( err != 0 ) return NET_ERROR; return NET_OK;}
the reason the wrapper sucks is that my packet handling are relying on the same endianess on both machines and that they rely on you using blocking sockets..
I tried replacing all socket types with the SOCKET type. But I get the same errors. SOCKET is just a macro for unsigned int btw.
And you''re right, I read Beej''s guide and thought I could use (almost) the same code on Windows :-)
I hope I''m doing something else wrong and I hope even more you can tell me what :-)
And you''re right, I read Beej''s guide and thought I could use (almost) the same code on Windows :-)
I hope I''m doing something else wrong and I hope even more you can tell me what :-)
> you should use WSAGetLastError()
Get the error code from this function instead.
> my_addr.sin_port = htons(port);
I''m curious as to which port you are trying to bind. The port may already be in use, or it can be restricted (i.e. administrator-only), or you may have ''Norton Personal Firewall'' and the like running in the background.
-cb
Get the error code from this function instead.
> my_addr.sin_port = htons(port);
I''m curious as to which port you are trying to bind. The port may already be in use, or it can be restricted (i.e. administrator-only), or you may have ''Norton Personal Firewall'' and the like running in the background.
-cb
Lysandius:
I am sorry I don't know why your stuff isn't working. Im currently only running linux so I don't have most of my winsock code.. But why don't you just paste the code I gave you into your program and do this:
unsigned short Myport = 9000;
int backlog = 5;
NET_Init();
SOCKET Mysock = NET_CreateSocket();
NET_BindSocket(Mysock, Myport);
NET_Listen(Mysock, backlog);
// you might use NET_Accept() here and then NET_Send
// something to the client..
NET_CloseSocket(Mysock);
NET_Shutdown();
it have done the trick for me several times.. or just look at the code in my functions.. If my functions doesn't work either your compiler are setup wrong or some of the winsock calls are failing. As mentioned by others, use WSAGetLastError()
[edited by - smokes on April 3, 2003 1:22:17 PM]
I am sorry I don't know why your stuff isn't working. Im currently only running linux so I don't have most of my winsock code.. But why don't you just paste the code I gave you into your program and do this:
unsigned short Myport = 9000;
int backlog = 5;
NET_Init();
SOCKET Mysock = NET_CreateSocket();
NET_BindSocket(Mysock, Myport);
NET_Listen(Mysock, backlog);
// you might use NET_Accept() here and then NET_Send
// something to the client..
NET_CloseSocket(Mysock);
NET_Shutdown();
it have done the trick for me several times.. or just look at the code in my functions.. If my functions doesn't work either your compiler are setup wrong or some of the winsock calls are failing. As mentioned by others, use WSAGetLastError()
[edited by - smokes on April 3, 2003 1:22:17 PM]
You are missing parenthesis in this line:
if (my_sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
The compiler is treating this as:
if (my_sockfd = ( socket(AF_INET, SOCK_STREAM, 0) == -1) )
and my_sockfd always gets ths value of 0. This then gets passed to bind and, if you had checked the error code the way the documentation plainly tells you to, you would have seen that you got error WSANOTSOCK which would probably have tipped you off right away.
Either fix the parens or move the assignment out of the if statement since doing that is evil anyway.
-Mike
if (my_sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
The compiler is treating this as:
if (my_sockfd = ( socket(AF_INET, SOCK_STREAM, 0) == -1) )
and my_sockfd always gets ths value of 0. This then gets passed to bind and, if you had checked the error code the way the documentation plainly tells you to, you would have seen that you got error WSANOTSOCK which would probably have tipped you off right away.
Either fix the parens or move the assignment out of the if statement since doing that is evil anyway.
-Mike
-Mike
Try changing this:
To:
Hope that helps you out.
[edited by - Digitalfiend on April 3, 2003 7:14:09 PM]
if (my_sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1) { perror("socket"); exit(1);} my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port);my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '\0', 8); if (bind(my_sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1){ perror("bind"); exit(1);}
To:
my_sockfd = socket(AF_INET, SOCK_STREAM, 0);if(my_sockfd == INVALID_SOCKET){ DWORD err = WSAGetLastError(); exit(1); // ugh} sockaddr_in my_addr;my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port);my_addr.sin_addr.s_addr = INADDR_ANY; int result = bind(my_sockfd, (sockaddr*)&my_addr, sizeof(my_addr));if(result == SOCKET_ERROR){ DWORD err = WSAGetLastError(); exit(1); // ugh} // ... rest of code
Hope that helps you out.
[edited by - Digitalfiend on April 3, 2003 7:14:09 PM]
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement