/*******************
 *  echoServer.c
 * By Hunter Pitelka
 *   15-213 s09
 *******************/
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>

#define RCVBUFSIZE 32

void HandleTCPClient(int clientSocket){

	char echoBuffer[RCVBUFSIZE];
	int recvMsgSize;

	/*block until we receive _some_ data from the client*/	
	if((recvMsgSize = recv(clientSocket,echoBuffer,RCVBUFSIZE,0)) <0){
		perror("recieve failed");
		return;
	}

	/*while we continue to receive data*/
	/*the recv call will return 0 when the client closes their connection*/
	while(recvMsgSize > 0){
		/*send the data back to the client*/
		if(send(clientSocket,echoBuffer,recvMsgSize,0) != recvMsgSize){
			perror("send failed");
			return;
		}
		/*try to receive more*/
		if((recvMsgSize = recv(clientSocket, echoBuffer, RCVBUFSIZE,0)) <0){
			perror("recv failed");
			return;
		}
	}
}
