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