/* Goal: send large amount of data over TCP to a remote host, and have everything arrive. While doing so, study
   the role of various system calls, ioctrls and socket options.

   Run as 'program-a 1.2.3.4' to connect to 1.2.3.4 port 9876

   Bert Hubert, Netherlabs Computer Consulting BV - bert.hubert@netherlabs.nl
*/

#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/ioctl.h>

#ifdef __linux__
#include <linux/sockios.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

/* in the interest of brevity, this code does not contain a lot of error checking */

int main(int argc, char** argv)
{
	signal(SIGPIPE, SIG_IGN);
	int sock=socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in remote;
	remote.sin_family=AF_INET;
	inet_pton(AF_INET, argc > 1 ? argv[1] : "127.0.0.1", &remote.sin_addr);
	remote.sin_port = ntohs(9876);

	if(connect(sock, (struct sockaddr*)&remote, sizeof(remote)) < 0) {
		perror("Connecting to remote");
		exit(1);
	}

	char buffer[4000];
	int n, res;

	for(n=0; n < 250 ; ++n) {
		res=write(sock, buffer, sizeof(buffer));
		if(res != sizeof(buffer)) {
			if(res < 0)
				perror("writing");
			else
				fprintf(stderr,"Partial write\n");
			exit(1);
		}
	}

	close(sock);
	return 0;
}
