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. Basic UDP Connection

Basic UDP Connection

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Has anyone got a very basic sample of creating a UDP socket on a specified port and sending a line of text to another port on the local machine? May sound simple but I have just started looking atthis stuff in C++ and I cant find any good simple references to start me off. - Rob.

    J 2 Replies Last reply
    0
    • L Lost User

      Has anyone got a very basic sample of creating a UDP socket on a specified port and sending a line of text to another port on the local machine? May sound simple but I have just started looking atthis stuff in C++ and I cant find any good simple references to start me off. - Rob.

      J Offline
      J Offline
      Jon Sagara
      wrote on last edited by
      #2

      This function doesn't do exactly what you're looking for, but it's pretty close. You just have to change it so that the socket binds to a specific port instead of the next available port.

      // ----------------------------------------------------------------------------
      // udp_recv_setup()
      //
      // This function sets the server socket. It lets the system determine the port
      // number. The function returns the server socket number.
      // ----------------------------------------------------------------------------
      //
      int udp_recv_setup()
      {
      struct sockaddr_in local; // socket address for local side
      int nLocalAddrLen = sizeof(local); // length of local address
      int nServerSocket = 0;

      //
      // Create the socket.
      //
      nServerSocket = socket(AF\_INET, SOCK\_DGRAM, 0);
      
      if ( nServerSocket < 0)
      {
      	cout << "%error opening datagram socket \\n" ;
      	exit(1);
      }
      
      //
      // Bind the socket to a port, let the system decide the number.
      //
      local.sin\_family		= AF\_INET;		// Internet family
      local.sin\_addr.s\_addr	= INADDR\_ANY;	// Wild card machine address  (defaults to local)
      local.sin\_port			= 0;			// Let system choose the port 
      
      bind(nServerSocket, (struct sockaddr \*) &local,sizeof(local));	//bind the name (address) to a port
      
      //
      // Get the port name and print it out.
      //
      getsockname( nServerSocket, (struct sockaddr \*) &local, &nLocalAddrLen);		
      cout << "socket has port " << local.sin\_port << endl;
      
      return  nServerSocket;
      

      }

      Jon Sagara "Ninety percent of baseball is mental, the other half is physical." -- Yogi Bera

      M 1 Reply Last reply
      0
      • L Lost User

        Has anyone got a very basic sample of creating a UDP socket on a specified port and sending a line of text to another port on the local machine? May sound simple but I have just started looking atthis stuff in C++ and I cant find any good simple references to start me off. - Rob.

        J Offline
        J Offline
        Jon Sagara
        wrote on last edited by
        #3

        ... and to send/receive data, use the sendto/recvfrom functions. *** Caveat - I wrote this code to run on Solaris, but it should work on Windows without major modifications. Jon Sagara "Ninety percent of baseball is mental, the other half is physical." -- Yogi Bera

        1 Reply Last reply
        0
        • J Jon Sagara

          This function doesn't do exactly what you're looking for, but it's pretty close. You just have to change it so that the socket binds to a specific port instead of the next available port.

          // ----------------------------------------------------------------------------
          // udp_recv_setup()
          //
          // This function sets the server socket. It lets the system determine the port
          // number. The function returns the server socket number.
          // ----------------------------------------------------------------------------
          //
          int udp_recv_setup()
          {
          struct sockaddr_in local; // socket address for local side
          int nLocalAddrLen = sizeof(local); // length of local address
          int nServerSocket = 0;

          //
          // Create the socket.
          //
          nServerSocket = socket(AF\_INET, SOCK\_DGRAM, 0);
          
          if ( nServerSocket < 0)
          {
          	cout << "%error opening datagram socket \\n" ;
          	exit(1);
          }
          
          //
          // Bind the socket to a port, let the system decide the number.
          //
          local.sin\_family		= AF\_INET;		// Internet family
          local.sin\_addr.s\_addr	= INADDR\_ANY;	// Wild card machine address  (defaults to local)
          local.sin\_port			= 0;			// Let system choose the port 
          
          bind(nServerSocket, (struct sockaddr \*) &local,sizeof(local));	//bind the name (address) to a port
          
          //
          // Get the port name and print it out.
          //
          getsockname( nServerSocket, (struct sockaddr \*) &local, &nLocalAddrLen);		
          cout << "socket has port " << local.sin\_port << endl;
          
          return  nServerSocket;
          

          }

          Jon Sagara "Ninety percent of baseball is mental, the other half is physical." -- Yogi Bera

          M Offline
          M Offline
          Masaaki Onishi
          wrote on last edited by
          #4

          Hello, the codegurus around the world.;) First of all, we must understand the difference between TCP and UDP. Since UDP is the connetion-less protocol, we don't need connect() in the code. On the other hands, if we use TCP (connection protocl), we have to use connect() in the code. Second, we need figure out which protocol is better for the target application. Third, we can download many sample codes around some web site. Try to use the keywork as "WinSock" in www.google.com, you can find the samples like "WinSock2"... Please, don't send me your email about your questions directly. Have a nice day! Sonork - 100.10571:vcdeveloper ;)

          -Masaaki Onishi-

          J 1 Reply Last reply
          0
          • M Masaaki Onishi

            Hello, the codegurus around the world.;) First of all, we must understand the difference between TCP and UDP. Since UDP is the connetion-less protocol, we don't need connect() in the code. On the other hands, if we use TCP (connection protocl), we have to use connect() in the code. Second, we need figure out which protocol is better for the target application. Third, we can download many sample codes around some web site. Try to use the keywork as "WinSock" in www.google.com, you can find the samples like "WinSock2"... Please, don't send me your email about your questions directly. Have a nice day! Sonork - 100.10571:vcdeveloper ;)

            -Masaaki Onishi-

            J Offline
            J Offline
            Jon Sagara
            wrote on last edited by
            #5

            Masaaki Onishi wrote: we don't need connect() I don't call connect() in my code. Were you addressing me or Rob? Masaaki Onishi wrote: Second, we need figure out which protocol is better for the target application. Agreed, but he asked specifically for UDP samples. Jon Sagara "Ninety percent of baseball is mental, the other half is physical." -- Yogi Bera

            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