티스토리 뷰

???/소켓프로그래밍

UDP TimeServer

BulGoGuMa 2009.07.14 09:25
/*
   udpTimeServer.c

   Author: Jeffrey Shantz

   Course: Computer Science 357a
   Assignment: 1

   Implementation of a simple UDP server that waits to receive a datagram.  Upon 
   receiving any datagram, it sends a datagram back with its current time of day.
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include "udpTimeServer.h"
#include "udpSockets.h"
#include "timeServer.h"
#include "sockets.h"
#include "timeOfDay.h"
#include "errorHandling.h"

/* waitForDatagram

   Input:
        int socketDescriptor - The file descriptor for the listening socket.

   Pre-conditions:
        A socket has been created and bound to a port number.

   Output:
        Returns a sockaddr_in structure containing the details of the client that sent
a datagram.

   Post-conditions:
A client has sent a datagram, and its details have been returned.  Otherwise, 
an error message has been displayed, and the program exited.

   Description:
Waits for a datagram to arrive from its listening socket.  Upon arrival, 
returns the details of the client that sent the datagram.
*/
struct sockaddr_in waitForDatagram(int socketDescriptor)
{
/* Details of the client */
struct sockaddr_in client;

/* Length of the client structure */
int clientLength = sizeof(client);

/* Number of bytes received in the datagram */
int bytesRead;

/* Buffer for reading the datagram */
char buffer[BUFFER_SIZE];

/* Wait for a datagram */
bytesRead = recvfrom(socketDescriptor, buffer, BUFFER_SIZE, FLAGS, (struct sockaddr *) &client, &clientLength);
if (bytesRead < MINIMUM_BYTES) 
exitWithError("Error receiving datagram");

return client;
}

/* sendTime

   Input:
        int socketDescriptor - The file descriptor for the listening socket.
struct sockaddr_in* client - A pointer to a sockaddr_in structure containing the client details

   Pre-conditions:
        A socket has been created and bound to a port number.  Valid client details have been obtained.

   Output:
None.

   Post-conditions:
The server's time has been sent to the client in a UDP datagram.  Otherwise, an error has 
been displayed, and the program exited.

   Description:
Send's the server's time of day to the client in a UDP datagram.
*/
void sendTime(int socketDescriptor, struct sockaddr_in* client)
{
/* The server's time */
struct timeval serverTime = getTime();

/* Send the time */
if (sendto(socketDescriptor,&serverTime,sizeof(serverTime),FLAGS,(struct sockaddr *)client, sizeof(*client)) < SUCCESS_BYTES)
exitWithError("Error sending UDP datagram");
printf("Sent: %ld sec, %ld usec\n", serverTime.tv_sec, serverTime.tv_usec);
}

/* main

   Input:
int argc - The number of arguments specified on the command line.
char *argv[] - The command line arguments

   Pre-conditions:
The program has been executed.

   Output:
Returns a status code to the operating system.

   Post-conditions:
A client has sent a datagram, the server has responded with its time, and the
connection closed.

   Description:
Main method for a simple UDP time server.  Waits for a datagram and, once
received, sends its time of day and closes the socket.
*/
int main(int argc, char *argv[])
{
/* Port number on which to listen */
int portNumber;

/* File descriptor for the socket */
int socketDescriptor;

/* Details of the client (IP, etc) */
struct sockaddr_in client;
/* Get the port number, and bind to a socket */
portNumber = parseArguments(argc, argv);
socketDescriptor = createUdpSocket();
bindSocket(socketDescriptor, portNumber);

/* Wait for a datagram, send the time, and close the socket */
client = waitForDatagram(socketDescriptor);
sendTime(socketDescriptor, &client);
destroySocket(socketDescriptor);

exit(EXIT_SUCCESS); 
}

'??? > 소켓프로그래밍' 카테고리의 다른 글

타임 클라  (2) 2009.07.14
UDP TimeServer  (0) 2009.07.14
댓글
댓글쓰기 폼