Click here to Skip to main content
11,927,600 members (47,786 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
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

#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….
void shutdown_close_and_exit(SOCKET s)

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",
	// Init Winsock
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
		fprintf(stderr, "Error: WSAStartup() failed");
    // 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());
    // 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());
	//listen for incomming client connections
	// 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());
		} 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),
		// '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());
		} else {
			printf("Echo'd message %s to client %s:%d\n", 
					recv_buf, inet_ntoa(clnt_addr.sin_addr),
Posted 7-Apr-13 11:54am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^][^]


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100