//======================================================= file = client.c =====
//=  A message "client" program to demonstrate sockets programming            =
//=   - TCP/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 server to be running on another host.  Program   =
//=       server should be started first.                                     =
//=    4) This program assumes that the server has the IP address hardcoded   =
//=       into "#define IP_ADDR"                                              =
//=    5) The steps #'s correspond to lecture topics.                         =
//=---------------------------------------------------------------------------=
//=  Build: bcc32 client.c or cl client.c wsock32.lib for Winsock             =
//=         gcc client.c -lsocket -lnsl for BSD                               =
//=---------------------------------------------------------------------------=
//=  History:  KJC (11/02/98) - Genesis                                       =
//=            KJC (02/08/00) - Added client-to-server send                   =
//=            KJC (05/20/03) - Updated socket naming to match Kurose et al.  =
//=            KJC (12/14/05) - Minor clean-up                                =
//=============================================================================
#define  WIN                // WIN for Winsock and BSD for BSD sockets

//----- Include files ---------------------------------------------------------
#include <stdio.h>          // Needed for printf()
#include <string.h>         // Needed for memcpy() and strcpy()
#ifdef WIN
  #include <windows.h>      // Needed for all Winsock stuff
#endif
#ifdef BSD
  #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
#endif

//----- Defines ---------------------------------------------------------------
#define  PORT_NUM         1050   // Port number used at the server
#define  IP_ADDR "131.247.3.42"  // IP address of server (*** HARDWIRED ***)

//===== Main program ==========================================================
void main(void)
{
#ifdef WIN
  WORD wVersionRequested = MAKEWORD(1,1);       // Stuff for WSA functions
  WSADATA wsaData;                              // Stuff for WSA functions
#endif
  unsigned int         client_s;        // Client socket descriptor
  struct sockaddr_in   server_addr;     // Server Internet address
  char                 out_buf[100];    // Output buffer for data
  char                 in_buf[100];     // Input buffer for data

#ifdef WIN
  // This stuff initializes winsock
  WSAStartup(wVersionRequested, &wsaData);
#endif

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

  // >>> Step #2 <<<
  // Fill-in the server's address information and do a connect with the
  // listening server usingh the client socket.  The connect() will block.
  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
  connect(client_s, (struct sockaddr *)&server_addr, sizeof(server_addr));

  // >>> Step #3 <<<
  // Receive from the server using the client socket
  recv(client_s, in_buf, sizeof(in_buf), 0);
  printf("Received from server... data = '%s' \n", in_buf);

  // >>> Step #4 <<<
  // Send to the server using the client socket
  strcpy(out_buf, "Test message from client to server");
  send(client_s, out_buf, (strlen(out_buf) + 1), 0);

  // >>> Step #5 <<<
  // Close the client socket
#ifdef WIN
  closesocket(client_s);
#endif
#ifdef BSD
  close(client_s);
#endif

#ifdef WIN
  // Clean-up winsock
  WSACleanup();
#endif
}

