#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#define MAXMSGLEN 100
int main(int argc, char**argv) {
char *serverip;
char *serverport;
unsigned short port;
char *msg="Hello from client";
char buf[MAXMSGLEN+1];
int sockfd, rv;
struct sockaddr_in srv;
// Get environment variable indicating the ip address of the server
serverip = getenv("server15440");
if (serverip) printf("Got environment variable server15440: %s\n", serverip);
else {
printf("Environment variable server15440 not found. Using 127.0.0.1\n");
serverip = "127.0.0.1";
}
// Get environment variable indicating the port of the server
serverport = getenv("serverport15440");
if (serverport) fprintf(stderr, "Got environment variable serverport15440: %s\n", serverport);
else {
fprintf(stderr, "Environment variable serverport15440 not found. Using 15440\n");
serverport = "15440";
}
port = (unsigned short)atoi(serverport);
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0); // TCP/IP socket
if (sockfd<0) err(1, 0); // in case of error
// setup address structure to point to server
memset(&srv, 0, sizeof(srv)); // clear it first
srv.sin_family = AF_INET; // IP family
srv.sin_addr.s_addr = inet_addr(serverip); // IP address of server
srv.sin_port = htons(port); // server port
// actually connect to the server
rv = connect(sockfd, (struct sockaddr*)&srv, sizeof(struct sockaddr));
if (rv<0) err(1,0);
// send message to server
printf("client sending to server: %s\n", msg);
send(sockfd, msg, strlen(msg), 0); // send message; should check return value
// get message back
rv = recv(sockfd, buf, MAXMSGLEN, 0); // get message
if (rv<0) err(1,0); // in case something went wrong
buf[rv]=0; // null terminate string to print
printf("client got messge: %s\n", buf);
// close socket
close(sockfd);
return 0;
}