Network programming (C++)
Posted: Mon Nov 26, 2007 9:29 pm
I have started reading up on network programming and i have found a really good tutorial and it is going fine i'd say.
However i have one problem and one question.
The problem is with some sample code:
It should hang until a connection request is made, however i exits emidiately, no matter which port i specify.
NB. I have linked the "libws2_32.a" and it compiles without warnings of any kind.
The idea was then to test it with telnet 127.0.0.1:1234 but ofcourse i havent gotten that far.
I have another question though.
I was planning on allso testing this with an outside source, so i started up pokerstars and did a "netstat" to find out which port to listen on.
i got this result:
localadress:rand
remoteadress:const
by rand i mean that the port changes from time to time and by const i mean that its the same port every time.
As far a i know i should listen on the port specified at the local adress, but that confuses me a bit as it changes, which basicly mean that i'd have to determine which port to listen on every time i wanted to do so. To me it seems that i should listen on the other port instead, but that conflicts with what im told here.
Which is the correct one?
Allso what do you do when one or more key keep falling of your laptop keyboard? its getting really annoying!
Anyway, thanks in advance.
Edit:
1 ...\main.cpp [Warning] `nul.gcda' is not a gcov data file
I have been googling around and have faound lots about this warning, only problem being that nobody seems to know what it means.
However i have one problem and one question.
The problem is with some sample code:
Code: Select all
#include <windows.h>
#include <winsock.h>
SOCKET s;
WSADATA w;
//LISTENONPORT – Listens on a specified port for incoming connections
//or data
int ListenOnPort(int portno)
{
int error = WSAStartup (0x0202, &w); // Fill in WSA info
if (error)
{
return false; //For some reason we couldn't start Winsock
}
if (w.wVersion != 0x0202) //Wrong Winsock version?
{
WSACleanup ();
return false;
}
SOCKADDR_IN addr; // The address structure for a TCP socket
addr.sin_family = AF_INET; // Address family
addr.sin_port = htons (portno); // Assign port to this socket
//Accept a connection from any IP using INADDR_ANY
//You could pass inet_addr("0.0.0.0") instead to accomplish the
//same thing. If you want only to watch for a connection from a
//specific IP, specify that //instead.
addr.sin_addr.s_addr = htonl (INADDR_ANY);
s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
if (s == INVALID_SOCKET)
{
return false; //Don't continue if we couldn't create a //socket!!
}
if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
{
//We couldn't bind (this will happen if you try to bind to the same
//socket more than once)
return false;
}
//Now we can start listening (allowing as many connections as possible to
//be made at the same time using SOMAXCONN). You could specify any
//integer value equal to or lesser than SOMAXCONN instead for custom
//purposes). The function will not //return until a connection request is
//made
listen(s, SOMAXCONN);
//Don't forget to clean up with CloseConnection()!
}
NB. I have linked the "libws2_32.a" and it compiles without warnings of any kind.
The idea was then to test it with telnet 127.0.0.1:1234 but ofcourse i havent gotten that far.
I have another question though.
I was planning on allso testing this with an outside source, so i started up pokerstars and did a "netstat" to find out which port to listen on.
i got this result:
localadress:rand
remoteadress:const
by rand i mean that the port changes from time to time and by const i mean that its the same port every time.
As far a i know i should listen on the port specified at the local adress, but that confuses me a bit as it changes, which basicly mean that i'd have to determine which port to listen on every time i wanted to do so. To me it seems that i should listen on the other port instead, but that conflicts with what im told here.
Which is the correct one?
Allso what do you do when one or more key keep falling of your laptop keyboard? its getting really annoying!
Anyway, thanks in advance.
Edit:
1 ...\main.cpp [Warning] `nul.gcda' is not a gcov data file
I have been googling around and have faound lots about this warning, only problem being that nobody seems to know what it means.