Simple UDP server - problem with sending current time

This is a discussion on Simple UDP server - problem with sending current time within the C Programming forums, part of the General Programming Boards category; Hey, I'm trying to make a simple UDP server, which sends current time to the specified port on localhost (every ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Simple UDP server - problem with sending current time

    Hey,

    I'm trying to make a simple UDP server, which sends current time to the specified port on localhost (every 2 sec), but each time sendto() function returns error. I'll be grateful if you just take a quick look on the code, I suppose the solution may be quite obvious

    Code:
    /* http://www.gomorgan89.com (based on) */
    /* Link with library file wsock32.lib */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    #include <time.h>
    
    
    void wait ( int seconds )
    {
      clock_t endwait;
      endwait = clock () + seconds * CLOCKS_PER_SEC ;
      while (clock() < endwait) {}
    }
    
    
    int main()
    {
        WSADATA w;
        SOCKET sd;
        struct sockaddr_in server;
        struct sockaddr_in client;
        unsigned short port_number = 5123;
        int a1 = 127, a2 = 0, a3 = 0, a4 = 1;
        int client_length;
        time_t current_time;
        
        WSAStartup(0x0101, &w);
        sd = socket(AF_INET, SOCK_DGRAM, 0);
        memset((void *)&server, '\0', sizeof(struct sockaddr_in));
        server.sin_family = AF_INET;
        server.sin_port = htons(port_number);
        server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
        server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
        server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
        server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
        bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
    
    
        while(1)
        {
            client_length = (int)sizeof(struct sockaddr_in);
            current_time = time(NULL);
            if (sendto(sd, (char *)&current_time, (int)sizeof(current_time), 0, (struct sockaddr *)&client, client_length) == (int)sizeof(current_time))
            {
                printf("Time sent\n");    
            }
            else
            {
                printf("Error sending time\n");
            }
            wait(2);
        }
    
    
        closesocket(sd);
        WSACleanup();
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Are you sure about this?
    Code:
    server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
    server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;    
    server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;    
    server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;
    Even if it will compile (it certainly won't on *nix), it can't be the best way to do it.

    Also, you're not making the socket udp (change the last argument to IPPROTO_UDP).

    To find the actual error, use perror().

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Thanks for the answer,

    I've found out that if I add a line of code to the while function, which basically receives some sort of information from the client, sendto() function starts to work properly (after some information form the client is received), but I want it to be only one-way communication, i.e. server sends something to client every 2 sec, not waiting for any information from the client. Is there a way to do it?

    Code:
    while(1)
        {
            client_length = (int)sizeof(struct sockaddr_in);
            /********added line**********/
            recvfrom(sd, buffer, 1024, 0, (struct sockaddr *)&client, &client_length);
            /********added line**********/
            current_time = time(NULL);
            if (sendto(sd, (char *)&current_time, (int)sizeof(current_time), 0, (struct sockaddr *)&client, client_length) == (int)sizeof(current_time))
            {
                printf("Time sent\n");    
            }
            else
            {
                printf("Error sending time\n");
            }
            wait(2);
        }

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Okay I get it now, my doubts were caused by lack of knowledge about sockets, server has to receive something before sending something else:
    What is sockets? - Definition from Whatis.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-06-2009, 12:05 AM
  3. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  4. Converting Zulu Time to Current Time
    By Caldus in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 09:54 PM
  5. Simple? winsock client - server problem
    By knutso in forum Windows Programming
    Replies: 2
    Last Post: 03-26-2003, 04:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21