Need info on UDP

Started by
2 comments, last by Zemus 21 years, 6 months ago
I''ve programmed a program using TCP and I need info on how change it into using UDP,(commands and stuff) Thanks
Advertisement
Can I just have one port(on the server) that everyone will send to using UDP? Or do I need a socket for each client?
After you create a datagram (UDP) socket you can simply use sendto and recvfrom to send and receive data between two hosts. In addition to the parameters you pass to send and recv for streaming sockets, you will also need to also provide a sockaddr structure and it''s length to the send method. This will specify where the data should be routed to. For recvfrom, you will provide pointers to a sockaddr structure and a pointer to a value for it''s length. When recvfrom returns, these will provide you with the information about where the data had come from.

When you want to receive data through a specific port you will need to bind that socket to an address and port.

For your server, you do not need to create a socket for each client that connects.
also, here is some piss poor code as a sample:


  #include <iostream.h>#include <string.h>#include <stdio.h>#ifdef _WIN32#include <winsock2.h>#include <ws2tcpip.h>#else#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>typedef int SOCKET;#define INVALID_SOCKET  (SOCKET)(~0)#define SOCKET_ERROR            (-1)#endif//using namespace std;void doClient( char* host );void doServer();int main( int argc, char* argv[] ) {    cout << "init 1\n" << endl;#ifdef _WIN32    WSADATA wsaData;        memset( &wsaData, 0, sizeof( WSADATA ) );    int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData );    if( err != 0 )    {        cout << "damn!\n" << endl;;        return 0;    }#endif        struct addrinfo* info = NULL;    if( getaddrinfo( "localhost", NULL, NULL, &info ) != 0 )    {        cout << "damn!\n" << endl;;        goto exit_app;    }    if( info != NULL )    {        struct sockaddr_in* addr = (struct sockaddr_in*)info->ai_addr;        cout << (int) ( (addr->sin_addr.s_addr >> 0) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 8) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 16) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 24)& 0xFF) << "\n" << endl;;    }    if( argc > 1 )        doClient( argv[1] );    else        doServer();exit_app:#ifdef _WIN32    err = WSACleanup();    if( err != 0 )    {        cout << "damn 2!\n" << endl;;        return 0;    }#endif    cout << "everything A-ok!\n " << endl;;    return 0;}void doClient( char* host ){    SOCKET sock = socket( PF_INET, SOCK_DGRAM, 0 );    struct sockaddr_in server, from;    cout << "client mode.\n" << endl;;    struct addrinfo* info = NULL;    if( getaddrinfo( host, NULL, NULL, &info ) != 0 )    {        cout << "damn!\n" << endl;;        return;    }    if( info != NULL )    {        struct sockaddr_in* addr = (struct sockaddr_in*)info->ai_addr;        cout << (int) ( (addr->sin_addr.s_addr >> 0) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 8) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 16 ) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 24 )& 0xFF) << "\n" << endl;;        memcpy( &server.sin_addr, &addr->sin_addr, sizeof( addr->sin_addr ) );    }    char buffer[1024];    server.sin_port = 0x1313;    server.sin_family = AF_INET;    printf("Please enter the message: ");    memset(buffer,0,256);    fgets(buffer,255,stdin);        int length=sizeof(struct sockaddr_in);    int n=sendto(sock,buffer,            strlen(buffer),0,(struct sockaddr *)&server,length);            if( n == SOCKET_ERROR )    {        cout << "Error sending data.\n" << endl;;        return;    }        n = recvfrom(sock,buffer,1024,0,(struct sockaddr *)&from, (socklen_t*)&length);    if( n == SOCKET_ERROR )    {        cout << "Error receiving data.\n" << endl;;        return;    }    buffer[ n ] = 0;    cout << "Got an ack: " << buffer << "\n" << endl;;   #ifdef _WIN32    if( closesocket( sock ) == SOCKET_ERROR )    {        cout << "Unable to close socket.\n" << endl;;        return;    }#endif}void doServer(){    struct sockaddr_in bindaddr;        cout << "server mode.\n" << endl;;    bindaddr.sin_family = AF_INET;    bindaddr.sin_addr.s_addr = INADDR_ANY;    bindaddr.sin_port   = 0x1313;    SOCKET sock = socket( PF_INET, SOCK_DGRAM, 0 );    if( sock == INVALID_SOCKET )    {        cout << "Unable to create socket.\n" << endl;;        return;    }    if( bind( sock, (sockaddr*) &bindaddr, sizeof( sockaddr_in ) ) == SOCKET_ERROR )    {        cout << "unable to bind to socket.\n" << endl;;        return;    }       int fromlen = sizeof(struct sockaddr_in);   while( 1 )   {       char buf[ 1024 ];       struct sockaddr from;       int n = recvfrom( sock, buf, 1024, 0, (struct sockaddr *)&from, (socklen_t*)&fromlen);       if( n == SOCKET_ERROR )       {           cout << "error receiving data from socket.\n" << endl;;       }              buf[ n ] = 0;              struct sockaddr_in* addr = (struct sockaddr_in*) &from;       cout << "Received a datagram from (";        cout << (int) ( (addr->sin_addr.s_addr >>  0) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >>  8) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 16) & 0xFF) << ".";        cout << (int) ( (addr->sin_addr.s_addr >> 24) & 0xFF) << ":" << addr->sin_port << "): " << buf << "\n" << endl;;       n = sendto(sock,"Got your message\n",17,                  0,(struct sockaddr *)&from,fromlen);       if( n  == SOCKET_ERROR )            cout << "error sending data to socket.\n" << endl;;   }#ifdef _WIN32    if( closesocket( sock ) == SOCKET_ERROR )    {        cout << "Unable to close socket.\n" << endl;;        return;    }#endif}  

This topic is closed to new replies.

Advertisement