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. ATL / WTL / STL
  4. re:lookng for winsock tcp communications example

re:lookng for winsock tcp communications example

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++sysadmintutorial
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
    Alan Kurlansky
    wrote on last edited by
    #1

    Looking for c++ winsock example code that will enable the following: Allow for multiple clients each on a different machine ie different ip address to connect in. I would like to listen on the same port for all clients and also once connectivity is established be able to send and receive using this same port for each and all the connections. Currently, with the code I have written connections seem to get established and I initially receive data from the client but when I attempt to send to client I do not get any errors but the client isn't receiving my message either. Then when viewed in Wireshark I see that the messages are associated with "ezmessagesrv". Something to do with unknown caller. One question, the server socket I use to establish the bind and listen, once the accept returns successfully with the new client socket connection should I close the server socket (the one I use to do the listening) and reinitialize before attempting to listen for other client connections?

    M 1 Reply Last reply
    0
    • A Alan Kurlansky

      Looking for c++ winsock example code that will enable the following: Allow for multiple clients each on a different machine ie different ip address to connect in. I would like to listen on the same port for all clients and also once connectivity is established be able to send and receive using this same port for each and all the connections. Currently, with the code I have written connections seem to get established and I initially receive data from the client but when I attempt to send to client I do not get any errors but the client isn't receiving my message either. Then when viewed in Wireshark I see that the messages are associated with "ezmessagesrv". Something to do with unknown caller. One question, the server socket I use to establish the bind and listen, once the accept returns successfully with the new client socket connection should I close the server socket (the one I use to do the listening) and reinitialize before attempting to listen for other client connections?

      M Offline
      M Offline
      Moak
      wrote on last edited by
      #2

      Have a look at Beej's guide to networking programming[^], there are client/server examples in chapter 6. Another starting point is the Winsock Programmer's FAQ[^], it has examples in section 6. Regarding a server, the normal way would be opening a server socket, calling bind and listen and then accepting incoming client connections. You do not close the server socket, while more incoming connections are expected. Here is a code fragment (your code could look different, but the principle stays the same):

      void CSocketServer::OnAccept(int nErrorCode)
      {
      CAsyncNetwork::OnAccept(nErrorCode);
      CAsyncNetwork* pSocket = new CAsyncNetwork;
      if(Accept(*pSocket))
      {
      m_listSockets.push_back(pSocket); //list of sockets
      } else {
      delete pSocket; //handle error case
      }
      }

      CSocketServer server;
      server.Open();
      server.Bind(4242); //listening port of server
      server.Listen();

      Hope this helps. :) /Moak

      Chat in Europe :java: Now with 24% more Twitter

      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