Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this assignment that I'm modifying for a client-server. The client will send fifty strings, from a preset list of 3. The server will parse these sentences and display the first sentence in red, the second in black, and the third in blue. This is what I have so far, just wasn't sure how to output this in the right format with the colors


C++
#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>

#define MYBUFSIZE	128


void shutdown_close(SOCKET s)
{

	// Tell the operating system the socket is
	// about to be closed	
	shutdown(s, SD_BOTH); 

	// close the socket….
	closesocket(s); 
}

void shutdown_close_and_exit(SOCKET s)
{
	shutdown_close(s);
	exit(1);
}



void main(int argc, char *argv[])
{
    SOCKET srvr_socket, clnt_socket;
    struct sockaddr_in srvr_addr;
    struct sockaddr_in clnt_addr; // Client address
    int addr_len = 0;
    WSADATA wsaData;
	char recv_buf[MYBUFSIZE];
	int recv_msg_len;
	short portnum;
	DWORD WINAPI runThread(LPVOID args);
	HANDLE hThread;
	DWORD threadId;	

   	if (argc == 2)
	{
		portnum = atoi(argv[1]);
		printf("Setting port number to %d\n", portnum);

	} else
	{
		fprintf(stderr, "Usage: %s port_num_to_listen_at\n",
				argv[0]);
        exit(1);
	}

	// Init Winsock
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
    {
		fprintf(stderr, "Error: WSAStartup() failed");
        exit(1);
    }

    // Create TCP Stream socket for incoming connections
    if ((srvr_socket = socket(AF_INET, SOCK_STREAM, 0) < 0) == SOCKET_ERROR)
	{
		fprintf(stderr, "Error: socket() failed with error %d\n", WSAGetLastError());
		shutdown_close_and_exit(srvr_socket);
	}

    // Construct local address structure
    memset(&srvr_addr, 0, sizeof(srvr_addr));
    srvr_addr.sin_family = AF_INET;
    srvr_addr.sin_addr.s_addr = INADDR_ANY;  // Accept messages on any network interface.
    srvr_addr.sin_port = htons(portnum);	 // Bind the port number specified on the command line.

    /* Bind to the local address */
    if (bind(srvr_socket, (struct sockaddr *) &srvr_addr, sizeof(srvr_addr)) == SOCKET_ERROR)
	{
		fprintf(stderr, "Error: socket() failed with error %d", WSAGetLastError());
	    shutdown_close_and_exit(srvr_socket);
	}

	//listen for incomming client connections
	listen(srvr_socket,10); 
	{
		return;
	}

	// Loop forever
    for (;;)
    {
        // Set the size of the in-out parameter, where the client address
		// and port number will be stored by the OS
        addr_len = sizeof(clnt_addr);

		//accept() incoming client connections
		clnt_socket = accept(srvr_socket,(struct sockaddr*)&clnt_addr,&addr_len);

		//Create a new thread
		hThread = CreateThread( NULL, 0, runThread, &clnt_addr, 0, &threadId);

		int r;

		for(int i=0; i < 51; i++)
		{
			r = rand() % 3 + 1;
			if (r == 1)
				printf ("The King is Dead!");
			else if (r == 2)
				printf ("Long Live the King!");
			else if (r == 3)
				printf ("Elvis is the King of Rock!");
		}

		// Receive message from client
		if ((recv_msg_len = recvfrom(srvr_socket, recv_buf, MYBUFSIZE, 0,
			(struct sockaddr *) &clnt_addr, &addr_len )) == SOCKET_ERROR) {

			fprintf(stderr, "Error: recvfrom() failed with error %d\n", WSAGetLastError());
			shutdown_close_and_exit(srvr_socket);
		} else {
			printf("Received message of size %d: %s\n from client %s:%d\n", 
					recv_msg_len, recv_buf, inet_ntoa(clnt_addr.sin_addr),
					ntohs(clnt_addr.sin_port));
		}

		
		// 'echo' message back to client
		if ((recv_msg_len = sendto(srvr_socket, recv_buf, recv_msg_len, 0,
			(struct sockaddr *) &clnt_addr, addr_len )) == SOCKET_ERROR) {

			fprintf(stderr, "Error: sendto() failed with error %d\n", WSAGetLastError());
			shutdown_close_and_exit(srvr_socket);
			shutdown_close_and_exit(clnt_socket);

		} else {
			printf("Echo'd message %s to client %s:%d\n", 
					recv_buf, inet_ntoa(clnt_addr.sin_addr),
					ntohs(clnt_addr.sin_port));
		}
    }
}
Posted

1 solution

iirc, somewhere when your server is starting up, you get a handle to the console STDOUT

HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 


then before you wish to write text (using printf in your case) you

SetConsoleTextAttribute(consoleHandle, x);


the two links below show the details inc the values for 'x' ie attributes

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx[^]

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088(v=vs.85).aspx#_win32_character_attributes[^]

'g'
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900