Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket, listen

Status
Not open for further replies.

mattias1975

Programmer
Jul 26, 2004
36
SE
I have made a socket program that is supposed to
receive some text from a client.
But there is something wrong when i call the
listen function. It returns -1.
Does anyone know whats wrong?

Mattias Westerberg



#include <winsock2.h>
#include <iostream.h>

#define BACKLOG 10

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 )
cout << "could not find a usable WinSock DLL" << "\n";

if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
cout << "could not find a usable WinSock DLL" << "\n";
}

int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;

if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
cout << "socket";
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = inet_addr("xxx.xxx.x.x");
my_addr.sin_port = htons(321);

memset(&(my_addr.sin_zero), '\0', 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
cout << "bind";
exit(1);
}

if (listen(sockfd, BACKLOG) == -1) {
cout << "listen";
exit(1);
}

char buf[100];
int numbytes;

while(1) {
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
cout << "accept";
continue;
}

if ((numbytes=recv(sockfd, buf, 100, 0)) != -1) {
cout << buf;
break;
}
}
}
 
I think that ports 1-1000 or something are reserved, so you should use a bigger number. Other than that, I got it working, but I had to change some stuff so that you may not be able to do what you are trying to do. I don't have a much experience with sockets, so this may not help, but with these changes I can at least get past listen().

I changed:

socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))

to

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

and changed:

my_addr.sin_addr.s_addr = inet_addr("xxx.xxx.x.x"); my_addr.sin_port = htons(321);

to

my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
my_addr.sin_port = htons(3210);

Hope this helps.
 
I'm newby to sockets too, but few comments anyway:

1.
> I think that ports 1-1000 or something are reserved

I don't think so - you can easy listen on port 80, simulating HTTP server, for example.

2. inet_addr("xxx.xxx.x.x") looks suspicious. Probably I'm wrong, but idea of "listenning" is to accept messages comming from all hosts of the net - ADDR_ANY is commonly used in this place.

3. I'm not sure, what socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) means, but in your example listen() causes an error code 10045 meaning that requested operation (listenning) is not supported on such kind of socket or protocol.
 
Actually, I just checked my sockets book, and it does indeed say that you shouldn't use sockets 1-1000. I do know that I picked an arbitrary number under 1000 once and my program wouldn't work because of it. It might work, but it's generally safer to choose a number over 1000. I have heard this from many different sources, and I know it's true. Here's a link to another article that mentions this:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top