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. Socket prblem

Socket prblem

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
4 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.
  • H Offline
    H Offline
    harsha_1234
    wrote on last edited by
    #1

    Hi all, i have one problem related to thread. i am having my main thread running which will on some event create a thread for validation purpose and then will start working. and now this validator thread will create a socket and it will bind to port 20111. and then will wait for client to connect. but before waiting for client to connect it will send a message to the main thread that validation is done. but in this way i need to stick with port 20111.which i really don't want. Is it possible for me to get a free port from operating system or from winsock library and then i should bind to this port?? and one more thing i want is i want to get the free port no that operating system has given in an variable because i will need that port number further in the main programs lifetime. the code is as follows UINT Thread_Func(LPVOID pParam) { wsaret = WSAStartup(0x101,&wsadata); if(wsaret != 0) return -1; sock.sin_family = AF_INET; sock.sin_addr.s_addr = INADDR_ANY; sock.sin_port = htons((unsigned short)20111);// iwant to obtain this port no on fly and // to get it in some variable so that i can use it further in my main thread server = socket(AF_INET,SOCK_STREAM,0); if(server == INVALID_SOCKET) return -1; if(bind(server,(SOCKADDR *)&sock,sizeof(sock))!=0) return -1; if(listen(server,1)!=0) return -1; cli_len = sizeof(cli); client = accept(server,(SOCKADDR *)&cli,&cli_len); if(client == 0) return -1; return 1; } i have not done socket programming before this is my first time i am doing it so if there are any mistakes please let me know Thanks and regards Harshal shete

    S 1 Reply Last reply
    0
    • H harsha_1234

      Hi all, i have one problem related to thread. i am having my main thread running which will on some event create a thread for validation purpose and then will start working. and now this validator thread will create a socket and it will bind to port 20111. and then will wait for client to connect. but before waiting for client to connect it will send a message to the main thread that validation is done. but in this way i need to stick with port 20111.which i really don't want. Is it possible for me to get a free port from operating system or from winsock library and then i should bind to this port?? and one more thing i want is i want to get the free port no that operating system has given in an variable because i will need that port number further in the main programs lifetime. the code is as follows UINT Thread_Func(LPVOID pParam) { wsaret = WSAStartup(0x101,&wsadata); if(wsaret != 0) return -1; sock.sin_family = AF_INET; sock.sin_addr.s_addr = INADDR_ANY; sock.sin_port = htons((unsigned short)20111);// iwant to obtain this port no on fly and // to get it in some variable so that i can use it further in my main thread server = socket(AF_INET,SOCK_STREAM,0); if(server == INVALID_SOCKET) return -1; if(bind(server,(SOCKADDR *)&sock,sizeof(sock))!=0) return -1; if(listen(server,1)!=0) return -1; cli_len = sizeof(cli); client = accept(server,(SOCKADDR *)&cli,&cli_len); if(client == 0) return -1; return 1; } i have not done socket programming before this is my first time i am doing it so if there are any mistakes please let me know Thanks and regards Harshal shete

      S Offline
      S Offline
      spielehelfer
      wrote on last edited by
      #2

      something in the back of my brain said: "sock.sin_port = 0" would let the OS select the port....

      H 1 Reply Last reply
      0
      • S spielehelfer

        something in the back of my brain said: "sock.sin_port = 0" would let the OS select the port....

        H Offline
        H Offline
        harsha_1234
        wrote on last edited by
        #3

        yes that is true but how i can access that port number??

        S 1 Reply Last reply
        0
        • H harsha_1234

          yes that is true but how i can access that port number??

          S Offline
          S Offline
          spielehelfer
          wrote on last edited by
          #4

          maybe you can reverse use the sock.sin_port after the connection has been occured with port = ntohs(sock.sin_port) from the socket.

          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