Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sockets

Sockets

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adno
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • A Adno

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You won't get any messages if you close the socket before doing anything with it. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups