/******************************************************************** * client.c * * CLIENT CODE * * from https://www.programminglogic.com/ * example-of-client-server-program-in-c-using-sockets-and-tcp/ * * ... with various minor modifications by Jim Mahoney on Nov 2018. *********************************************************************/ #include #include #include #include #include #include /* Pick a port to use for this client/server connection. The server needs to use also use this same port. It's an arbitrary number between 1024 and 65535 (16 bit unsigned). */ /* The low ports are reserved for system use; google "port numbers". */ #define PORT 7891 #define BUFFER_SIZE 1024 /* host where server is running : localhost. */ #define HOST "127.0.0.1" /* shannon.marlboro.college */ #define SHANNON "96.126.107.158" int main(){ char buffer[BUFFER_SIZE]; int connect_socket; struct sockaddr_in address; socklen_t address_size = sizeof(struct sockaddr_in); /*---- Create the socket. The three arguments are: ---- 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */ connect_socket = socket(PF_INET, SOCK_STREAM, 0); /*---- Configure settings of the address struct ----*/ /* ... which specifies what we want to connect to. */ /* Address family = Internet */ address.sin_family = AF_INET; /* Set port number, using htons function to use proper byte order */ address.sin_port = htons(PORT); /* Set IP address to localhost */ /* address.sin_addr.s_addr = inet_addr(HOST); */ /* ... or shannon.marlboro.college */ address.sin_addr.s_addr = inet_addr(SHANNON); /* Set all bits of the padding field to 0 */ memset(address.sin_zero, '\0', sizeof(address.sin_zero)); /*---- Connect the socket to the server using the address struct ----*/ /* See e.g. http://manpages.ubuntu.com/manpages/bionic/man2/connect.2.html */ connect(connect_socket, (struct sockaddr *) &address, address_size); printf("Client: connection established.\n"); /*---- Read the message from the server into the buffer ----*/ recv(connect_socket, buffer, BUFFER_SIZE, 0); /*---- Print the received message ----*/ printf("Client: received: \"%s\"\n", buffer); printf("Client exiting.\n"); close(connect_socket); return 0; }