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. Query Regarding UDP socket programming

Query Regarding UDP socket programming

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasequestion
6 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.
  • F Offline
    F Offline
    fearless stallion
    wrote on last edited by
    #1

    Hi, I wrote a program that sends data to few systems through a differnt thread(SendThread) using UDP on a LAN. and before this SendThread i am launching another Thread(RecvThread), that listens to a port no 54000. this RecvThread listens and whenever it gets any message Say "AAA" then it replies back with a sendto(sd,"yes",3,&client,size); but this message never reaches the other system that sent the first message "AAA". When i dubugged i found that the IP was alright. Once the SendThread finishes sending to selected systems, it ends. my code for RecvThread goes like this. is it because the recvfrom() is done in another thread? so that when another system sends a message, this thread completed its time slice or is not run by the CPU. hence it is not received? can this happen ? Please help me on this. while (1) { buffer[0]='\0'; client_length = (int)sizeof(struct sockaddr_in); memset((void *)&client, '\0', sizeof(struct sockaddr_in)); bytes_received=0; bytes_received = recvfrom(sd, buffer, 4, 0, (struct sockaddr *)&client, &client_length); if (bytes_received < 0){ printf("No Data is Found.\n"); closesocket(sd); _endthread(); } else { if (strcmp(buffer, "XXX") == 0){ if (sendto(sd, current_time, 4, 0, (struct sockaddr *)&client, client_length) != 4){ printf("Error sending datagram.\n"); closesocket(sd); } else { printf("\nReplySent"); } } else if(strcmp(buffer,"Yes") == 0){ printf("Got a reply for yes"); } }//end of outer else }//end of while Thanks in Advance:) _K_SS

    E 1 Reply Last reply
    0
    • F fearless stallion

      Hi, I wrote a program that sends data to few systems through a differnt thread(SendThread) using UDP on a LAN. and before this SendThread i am launching another Thread(RecvThread), that listens to a port no 54000. this RecvThread listens and whenever it gets any message Say "AAA" then it replies back with a sendto(sd,"yes",3,&client,size); but this message never reaches the other system that sent the first message "AAA". When i dubugged i found that the IP was alright. Once the SendThread finishes sending to selected systems, it ends. my code for RecvThread goes like this. is it because the recvfrom() is done in another thread? so that when another system sends a message, this thread completed its time slice or is not run by the CPU. hence it is not received? can this happen ? Please help me on this. while (1) { buffer[0]='\0'; client_length = (int)sizeof(struct sockaddr_in); memset((void *)&client, '\0', sizeof(struct sockaddr_in)); bytes_received=0; bytes_received = recvfrom(sd, buffer, 4, 0, (struct sockaddr *)&client, &client_length); if (bytes_received < 0){ printf("No Data is Found.\n"); closesocket(sd); _endthread(); } else { if (strcmp(buffer, "XXX") == 0){ if (sendto(sd, current_time, 4, 0, (struct sockaddr *)&client, client_length) != 4){ printf("Error sending datagram.\n"); closesocket(sd); } else { printf("\nReplySent"); } } else if(strcmp(buffer,"Yes") == 0){ printf("Got a reply for yes"); } }//end of outer else }//end of while Thanks in Advance:) _K_SS

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      you have a separate thread RecvThreadfor receiving, then why you receive it again here ?:confused:

      fearless stallion wrote:

      bytes_received = recvfrom(sd, buffer, 4, 0, (struct sockaddr *)&client, &client_length);


      VuNic

      F 1 Reply Last reply
      0
      • E Eytukan

        you have a separate thread RecvThreadfor receiving, then why you receive it again here ?:confused:

        fearless stallion wrote:

        bytes_received = recvfrom(sd, buffer, 4, 0, (struct sockaddr *)&client, &client_length);


        VuNic

        F Offline
        F Offline
        fearless stallion
        wrote on last edited by
        #3

        Hi, Let me make it a bit more clear, My Application has two threads SendThread and RecvThread. I place the same copy of the Application on the Selected Systems. First The RecvThread is set to Bind at 54000 port no. And then the SendThread sends a Query "XXX" to all the Selected Systems and then ends itself. all those systems must also be running the RecvThread at their own end so the Code that I listed above is my RecvThread, which when gets the Message "XXX" replies back to the sender with "Yes". I am able to successfully send the reply message "Yes" but on the Receivers end(Initial senders) i am not able to get the Message. The code handles the both "XXX" and "Yes" messages but still.. Why is it so. is there any problem in the Code?.. _K_SS

        E 1 Reply Last reply
        0
        • F fearless stallion

          Hi, Let me make it a bit more clear, My Application has two threads SendThread and RecvThread. I place the same copy of the Application on the Selected Systems. First The RecvThread is set to Bind at 54000 port no. And then the SendThread sends a Query "XXX" to all the Selected Systems and then ends itself. all those systems must also be running the RecvThread at their own end so the Code that I listed above is my RecvThread, which when gets the Message "XXX" replies back to the sender with "Yes". I am able to successfully send the reply message "Yes" but on the Receivers end(Initial senders) i am not able to get the Message. The code handles the both "XXX" and "Yes" messages but still.. Why is it so. is there any problem in the Code?.. _K_SS

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          Did you try the same with single UDP C/S application?, and one more question, your requirement seems that "Multicasting" would be the best option when you want to send to multiple users(clients), where you'll need to add an interface IP address and use ip_addmembership. but anyway, we'll first try your way.


          VuNic

          F 1 Reply Last reply
          0
          • E Eytukan

            Did you try the same with single UDP C/S application?, and one more question, your requirement seems that "Multicasting" would be the best option when you want to send to multiple users(clients), where you'll need to add an interface IP address and use ip_addmembership. but anyway, we'll first try your way.


            VuNic

            F Offline
            F Offline
            fearless stallion
            wrote on last edited by
            #5

            Hi, Yes I did, in fact it was a simple Client/Server Application and it worked fine, then i changed it to use Threads. you are right my application detects the number of machines on the network and sends a message to all of them and if it finds any answer then it adds that machine name to a file. I want to make it more like SQLDMO which detects the number SQL servers running on the network. can Multicasting Help me ? will it not increase the network traffic? BTW what is problem with this code?...:doh: One more thing when i start to debug it skips the Break points sometimes and just runs as if it is in general run mode. Thanks a lot for the Reply. _K_SS

            F 1 Reply Last reply
            0
            • F fearless stallion

              Hi, Yes I did, in fact it was a simple Client/Server Application and it worked fine, then i changed it to use Threads. you are right my application detects the number of machines on the network and sends a message to all of them and if it finds any answer then it adds that machine name to a file. I want to make it more like SQLDMO which detects the number SQL servers running on the network. can Multicasting Help me ? will it not increase the network traffic? BTW what is problem with this code?...:doh: One more thing when i start to debug it skips the Break points sometimes and just runs as if it is in general run mode. Thanks a lot for the Reply. _K_SS

              F Offline
              F Offline
              fearless stallion
              wrote on last edited by
              #6

              Thanks a lot, i got it resolvesd, actually the RecvThread was sending to a sockaddr_in that was with in the sender thread and that SendThread was killed by the time it got the mrssage. any ways thanks KSS

              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