//====================================================== file = client1.c =====
//=  A message "client" program to demonstrate sockets programming            =
//=   - UDP/IP client/server model is implemented                             =
//=============================================================================
//=  Notes:                                                                   =
//=    1) This program conditionally compiles for Winsock and BSD sockets.    =
//=       Set the initial #define to WIN or BSD as appropriate.               =
//=    2) There is *no* error checking in this program.  Error checking was   =
//=       omitted to simplify the program.                                    =
//=    3) This program needs server1 to be running on another host.  Program  =
//=       server1 should be started first.                                    =
//=    4) This program assumes that the server1 has the IP address hardcoded  =
//=       into "#define IP_ADDR"                                              =
//=    5) The steps #'s correspond to lecture topics.                         =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 client1.c or cl client1.c wsock32.lib for Winsock           =
//=         gcc client1.c -lsocket -lnsl for BSD                              =
//=---------------------------------------------------------------------------=
//=  History:  KJC (03/02/99) - Genesis (from client.c)                       =
//=            KJC (05/20/03) - Updated socket naming to match Kurose et al.  =
//=============================================================================

//----- Include files ---------------------------------------------------------
#include <stdio.h>          // Needed for printf()
#include <string.h>         // Needed for memcpy() and strcpy()
#include <sys/types.h>    // Needed for system defined identifiers.
#include <netinet/in.h>   // Needed for internet address structure.
#include <sys/socket.h>   // Needed for socket(), bind(), etc...
#include <arpa/inet.h>    // Needed for inet_ntoa()
#include <fcntl.h>        // Needed for sockets stuff
#include <netdb.h>        // Needed for sockets stuff

//----- Defines ---------------------------------------------------------------
#define  PORT_NUM           1050  // Port number used
#define  IP_ADDR "131.247.166.56" // IP address of server1 (*** HARDWIRED ***)

//===== Main program ==========================================================
void main(void)
{

  unsigned int         client_s;        // Client socket descriptor
  struct sockaddr_in   server_addr;     // Server Internet address
  int                  addr_len;        // Internet address length
  char                 out_buf[100];    // 100-byte buffer for output data
  char                 in_buf[100];     // 100-byte buffer for input data

  // >>> Step #1 <<<
  // Create a socket
  //   - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
  client_s = socket(AF_INET, SOCK_DGRAM, 0);

  // >>> Step #2 <<<
  // Fill-in server1 socket's address information
  server_addr.sin_family      = AF_INET;            // Address family to use
  server_addr.sin_port        = htons(PORT_NUM);    // Port num to use
  server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use

  // Assign a message to buffer out_buf
  strcpy(out_buf, "Test message from client1 to server1");

  // >>> Step #3 <<<
  // Now send the message to server1.  The "+ 1" is for the end-of-string
  // delimiter
  sendto(client_s, out_buf, (strlen(out_buf) + 1), 0,
    (struct sockaddr *)&server_addr, sizeof(server_addr));

  // >>> Step #4 <<<
  // Wait to receive a message
  addr_len = sizeof(server_addr);
  recvfrom(client_s, in_buf, sizeof(in_buf), 0,
    (struct sockaddr *)&server_addr, &addr_len);

  // Output the received message
  printf("Message received is: '%s' \n", in_buf);

  // >>> Step #5 <<<
  // Close all open sockets
  close(client_s);

}
