I have two files: client.c and a server.c. You can type a line on the client, press enter, and it will be displayed on the server. Then type a line on the server, press enter, and it gets displayed on the client side.

The problem is when you type two messages on either side. Then it gets buffered in memory, but won't display until after you type something on the other side. Like this:

Client: type "Hello"
Client: type "Hello2"

Server shows: "Hello"

<Hello2 does not show>

Server: type "Hi there"

Server shows: "Hello2" (shows only after typing Hi there and pressing enter)

(The problem is exacerbated if one side types 3 or more lines. It all gets buffered and won't display until the other side types.)
Does anyone know how I could get around this problem? More of a conceptual problem I suppose.

-----

Here is my source code and a makefile (I put red/bold around the relevant while loops, the only areas I'm coding in):

Code:
CC = /usr/bin/g++
CFLAGS = -Wall -pthread -lpthread
LIBS = -lm

APP = client server
OBJS =

all: $(APP)
$(APP): $(OBJS)
	$(CC) $(CFLAGS) -g -o $@ $(OBJS) $(LIBS)

client: client.c
	$(CC) $(CFLAGS) -g -o client client.c

server: server.c
	$(CC) $(CFLAGS) -g -o server server.c

clean:
	rm -f *.o *~* $(APP)
./server 3200 // (random port #)
./client 127.0.0.1 3200 // (localhost ip and random port#)

Code:
// Client Program
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h> /*for sockaddr_in and inet_addr()*/ // V
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define BUFFSIZE 256
#define SERVER_PORT 6401

int main(int argc, char** argv)
{
	int sock;
	struct sockaddr_in echoserver;
	char buffer[BUFFSIZE]={0};
	unsigned int echolen;

	int command = 0;
	char input[100];

	int received = 0;

	if(argc < 3)
	{
		printf("Usage : cli <serverip> <String> \n");
		exit(1);
	}

	if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
	{
		perror("Failed to create socket \n");
		exit(0);
	}

	memset(&echoserver, 0, sizeof(echoserver));
	echoserver.sin_family = AF_INET;
	echoserver.sin_addr.s_addr = inet_addr(argv[1]);
	echoserver.sin_port = htons(SERVER_PORT);
	if(connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0)
	{
		perror("Failed to connect with the server \n");
		exit(0);
	}

	memcpy(buffer,argv[2],sizeof(buffer)-1);
	echolen = strlen(buffer);

	while (command != 3 && command != 4)
	{
		printf("Enter option (1 - 4): ");
		fgets(input, 100, stdin);
		command = atoi(input);

		if (command == 1)
		{
			while(input[0] != 'E' || input[1] != 'X' || input[2] != 'I' || input[3] != 'T')
			{
				fgets(input, 100, stdin);
				if (send(sock, input, sizeof(input), 0) == -1)
					perror("send error");

				if(( received = recv(sock,buffer,BUFFSIZE-1, 0)) < 0 )
				{
					perror("Failed to receive additional bytes from client \n");
				}
				if(received >0)
				{
					printf("Received from Server : &#37;s",buffer);
				}
			}
		}
		else if (command == 2)
		{
			printf("Do 2\n");
			// Client sends url to server in format http://<server name>/
			// Server downloads index.html and sends to client's local directory
			// File saved as index.html (over-write if necessary)
		}
		else if (command == 3)
		{
			printf("Thank You.\n");
			// Disconnect Client
		}
		else if (command == 4)
		{
			printf("Thank You Very Much.\n");
			// Disconnect Client and Server
		}
		else
		{
			printf("Improper input.\n");
		}
	}

	close(sock);
}
Code:
// Server Program
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define BUFFSIZE 256
#define MAX_PENDING 10
#define MAX_CLIENTS MAX_PENDING

pthread_t clients[MAX_CLIENTS];
#define SERVER_PORT 6401

void *ClientData(void* arg)
{
	int sock = *(int*)arg;
	char buffer[BUFFSIZE] = {0};
	int received = -1;

	fd_set fds;
	int maxfd;
	int nready;

	char input[100];

	maxfd = sock + 1;

	while(1)
	{
		FD_ZERO(&fds);
		FD_SET(sock,&fds);

		nready = select(maxfd,&fds,0,0,0);
		if(FD_ISSET(sock,&fds))
		{
			if(( received = recv(sock,buffer,BUFFSIZE-1, 0)) <0 )
			{
				perror("Failed to receive additional bytes from client \n");
			}
			if(received >0)
			{
				printf("Received from Client : %s",buffer);
			}
			if(received < 1)
			{
				printf("Closing this client\n");
				close(sock);
				pthread_exit(NULL);
			}
			memset(buffer,0,sizeof(buffer)); // V, 0
		}
		fgets(input, 100, stdin);
		if (send(sock, input, sizeof(input), 0) == -1)
			perror("send error");
	}
	close(sock);
}

int main()
{
	int threadCount =0;

	int sock,clientsock;
	struct sockaddr_in echoserver,echoclient;
	char buffer[BUFFSIZE] = {0};

	if(( sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 )
	{
		perror("Failed to created socket \n");
	}

	memset(&echoserver, 0, sizeof(echoserver));
	echoserver.sin_family = AF_INET;
	echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
	echoserver.sin_port = htons(SERVER_PORT);

	if(bind(sock,(struct sockaddr *) &echoserver, sizeof(echoserver)) < 0)
	{
		perror("Failed to bind the socket \n");
	}
	if(listen(sock, MAX_PENDING) < 0)
	{
		perror("Failed to listen on server socket \n");
	}

	while(1)
	{
		unsigned int clientlen = sizeof(echoclient);

		if(( clientsock = accept(sock, (struct sockaddr*) &echoclient, &clientlen)) < 0)
		{
			perror("Failed to accept client connection \n");
		}
		fprintf(stdout,"Client Connected : %s \n",inet_ntoa(echoclient.sin_addr));

		if(clientsock > -1)
		{
			if(threadCount<MAX_CLIENTS)
				pthread_create(&clients[threadCount++],NULL,ClientData,(void *) &clientsock);
		}
	}
}