Sockets
-
im following this article here not quite getting it to work :( http://www.codeproject.com/internet/beginningtcp\_cpp.asp Listening for connection code: int ListenOnPort(int portno, HWND hwnd) { SOCKET s; WSADATA w; 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 int tempi = listen(s, SOMAXCONN); WSAAsyncSelect (s, hwnd, MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |FD_READ | FD_CLOSE)); //Don't forget to clean up with CloseConnection()! closesocket(s); ::MessageBox(NULL,"Done Listening ", "Got Request..", MB_OK); } //-------------- Look out for the events code: switch (message) { case MY_MESSAGE_NOTIFICATION: //Is a message being sent? { switch (lParam) //If so, which one is it? { case FD_ACCEPT: ::MessageBox(NULL,"Done Listening Event", "Got Request..", MB_OK); //Connection request was made break; case FD_CONNECT: ::MessageBox(NULL,"Done Listening Event", "Got Request..", MB_OK); //Connection was made succes
-
im following this article here not quite getting it to work :( http://www.codeproject.com/internet/beginningtcp\_cpp.asp Listening for connection code: int ListenOnPort(int portno, HWND hwnd) { SOCKET s; WSADATA w; 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 int tempi = listen(s, SOMAXCONN); WSAAsyncSelect (s, hwnd, MY_MESSAGE_NOTIFICATION, (FD_ACCEPT | FD_CONNECT |FD_READ | FD_CLOSE)); //Don't forget to clean up with CloseConnection()! closesocket(s); ::MessageBox(NULL,"Done Listening ", "Got Request..", MB_OK); } //-------------- Look out for the events code: switch (message) { case MY_MESSAGE_NOTIFICATION: //Is a message being sent? { switch (lParam) //If so, which one is it? { case FD_ACCEPT: ::MessageBox(NULL,"Done Listening Event", "Got Request..", MB_OK); //Connection request was made break; case FD_CONNECT: ::MessageBox(NULL,"Done Listening Event", "Got Request..", MB_OK); //Connection was made succes
You won't get any messages if you close the socket before doing anything with it. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: