basic winsock help

Started by
13 comments, last by jchmack 18 years, 5 months ago
Check the firewall settings on hes PC if you get an error connecting, as cybergolem said.
If it works using 127.0.0.1 that would be my guess

Windows XP has options to open a speciffic port in the firewall
Advertisement
windows firewall is off
how do i open specific ports

I have norwegian text in my windows menus, but I think i remeber the english counterparts more or less...

1 Open Control panel
2 Go to network connections (or something like that)
3 Right click the active network card on the list choose the bottom
option (Settings or something)
4 Go to the "Advanced" tab sheet

If you activate the firewall, you will get a Settings button at the bottom
where you can get more detailed control.
However if the firewall if not active this shouldnt be the problem.

Aslo if your client/server works when you run them on your own PC, and using 127.0.0.1, the program works. (I think I said that already ^^)

EDIT:
Im testing it and there is something fishy here for sure.
Ill post if i find the problem

[Edited by - pulpfist on November 4, 2005 10:11:21 PM]
Ok I got it!

Its the accept call in the server

change:
// Bind the socket.    sockaddr_in service;

to:
// Bind the socket.    sockaddr_in service, client;


and change:

printf( "Waiting for a client to connect...\n" );    while (1) {        AcceptSocket = SOCKET_ERROR;        while ( AcceptSocket == SOCKET_ERROR ) {            AcceptSocket = accept( m_socket, NULL, NULL );        }        printf( "Client Connected.\n");        m_socket = AcceptSocket;         break;    }

to:
printf( "Waiting for a client to connect...\n" );    AcceptSocket = SOCKET_ERROR;    int sin_size = sizeof(struct sockaddr_in);    while ( AcceptSocket == SOCKET_ERROR )      AcceptSocket = accept( m_socket, (struct sockaddr *)&client,  &sin_size);    printf( "Client Connected.\n");    m_socket = AcceptSocket;


accept is a blocking call so no need to put it in a loop (until you make it non-blocking ofcourse)
By doing it this way your server also have detailed info about the connected client in the "client" struct :p

The original code you posted works fine apart from that, even though you
should prolly use INADDR_ANY as stated by hplus
...service.sin_addr.s_addr = INADDR_ANY;...


And consider terminating the data recieved (not sure recv does that for you but better be sure)
...int num_bytes = recv( m_socket, recvbuf, 32, 0 );recvbuf[num_bytes] = '\0';...



[Edited by - pulpfist on November 5, 2005 12:26:39 AM]
ok i got it =)
i did what pulp said and opened a port on my router (in port forwarding).

service.sin_port = htons( 27015 );
requires port 27015 to be forwarded to my bros computer.

yay ty guys for all the help

This topic is closed to new replies.

Advertisement