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. Getting ip of client using winsock

Getting ip of client using winsock

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelptutorial
4 Posts 3 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.
  • D Offline
    D Offline
    darkcloud 42o
    wrote on last edited by
    #1

    Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...

    _ V 2 Replies Last reply
    0
    • D darkcloud 42o

      Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      See if this helps inet_ntoa(client.sin_addr) Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      D 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        See if this helps inet_ntoa(client.sin_addr) Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

        D Offline
        D Offline
        darkcloud 42o
        wrote on last edited by
        #3

        hmm.. getting 204.204.204.204 from the inet_ntoa.. which probably means.. that part of the accept is not going to help me get the ip... i assume.. ive seen this before.... char hostname[256]; struct hostent *host; in_addr ip; gethostname(hostname, 256); host = gethostbyname(hostname); memcpy(&ip, host->h_addr, sizeof(ip)); printf("First host IP is: %s\n", inet_ntoa(ip)); which seems to get the local ip.. which is not what im trying to do...

        1 Reply Last reply
        0
        • D darkcloud 42o

          Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          I think you can get the IP in this manner:

          sockaddr_in clientAddress;
          accept( ..., (SOCKADDR*)&clientAddress, ...);
          in_addr inAddr = clientAddress.sin_addr;
          

          Now the four components of IP, from left to right, are in inAddr.S_un.S_un_b.s_b# members:

          printf("The IP is: %i.%i.%i.%i", 
              inAddr.S_un.S_un_b.s_b1,
              inAddr.S_un.S_un_b.s_b2,
              inAddr.S_un.S_un_b.s_b3,
              inAddr.S_un.S_un_b.s_b4);
          

          Hope it helps.

          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