Guys...I badly need your help...I made a Hangman game and the requirement is it mus be a two player game so I need to make use of socket programming. The problem is I don't know what to do next..I do not know how to do the read and write the guess of a player using sockets..Can anybody please help me? This is my code for my server:
#include <iostream>
#include <windows.h>
#include <string>
#include <cstdlib>
#include <cctype>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sstream>
using namespace std;
//==============socket variables===========
struct sockaddr_in serv_addr, cli_addr;
int sockfd, newsockfd, portno, clilen, n;
char buffer[256];
//=======================================
void PlaceCursor(const int x, const int y) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD PlaceCursorHere;
PlaceCursorHere.X = x;
PlaceCursorHere.Y = y;
SetConsoleCursorPosition(hConsole, PlaceCursorHere);
return;
}
void MissedLetter(int miss) {
switch (miss) {
case 1: PlaceCursor(55, 4); printf("O"); break;
case 2: PlaceCursor(55, 5); printf("|"); break;
case 3: PlaceCursor(54, 5); printf("\\"); break;
case 4: PlaceCursor(56, 5); printf("/"); break;
}
return;
}
void ClearConsole() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten)) { return; }
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
if (!FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten)) { return; }
return;
}
int main(int argc, char *argv[]) {
if(argc < 2)
error("Usage: ./server <port no.>\n");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
error("Error Opening Socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
n = bind(sockfd,(struct sockaddr*) &serv_addr, sizeof(serv_addr));
if(n < 0)
error("Error on Binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) &clilen);
if(newsockfd < 0)
error("Error on Accept");
char *WordLibrary[] = {
"cat",
"dog",
"pan"
};
StartGame:
srand(time(NULL));
int Selected = rand() % 3;
int miss = 0;
char *Word = WordLibrary[Selected];
int WordLength = strlen(WordLibrary[Selected]);
char Guess[2] = {'\0'};
char CorrectlyPicked[WordLength];
strset(CorrectlyPicked, '\0');
int Completion = WordLength;
bool GotOne = false;
bool GotAlready = false;
PlaceCursor(30, 0);
printf("- Hangman -");
PlaceCursor(25, 1);
printf("1. Play a round");
PlaceCursor(25, 2);
printf("2. Exit game");
PlaceCursor(0, 4);
printf("Input: ");
int UserInput = 0;
scanf("%d", &UserInput);
switch (UserInput) {
case 1: ClearConsole(); _flushall(); break;
case 2: goto Exit; break;
default : PlaceCursor(0, 4); printf("Incorrect answer! Try again: ");
}
PlaceCursor(0, 2);
printf("Word: ");
PlaceCursor(0, 3);
printf("Victory status: FALSE");
for (int x = 0; x < WordLength; x++) { PlaceCursor(x + 6, 2); printf("_"); }
while (true) {
PlaceCursor(0, 1);
printf("Your guess: _\b");
cin.getline(Guess, 2);
_flushall();
for (int x = 0; x < WordLength; x++) {
if (Guess[0] == Word[x]) {
if (CorrectlyPicked[x] == Word[x]) { GotAlready = true; break; }
CorrectlyPicked[x] = Word[x];
PlaceCursor(x + 6, 2);
printf("%c", Word[x]);
Completion--;
GotOne = true;
break;
}
}
if (GotOne == false && GotAlready == false) { miss++; MissedLetter(miss); }
else { GotOne = false; GotAlready = false; }
if (Completion == 0) { break; }
}
PlaceCursor(0, 3);
printf("Victory status:");
printf(" TRUE ");
/* Reset all game values */
GotOne = false;
GotAlready = false;
miss = 0;
cin.get();
ClearConsole();
goto StartGame;
Exit:
cin.get();
return 0;
}
Can anybody please help me how to transfer the input of the user so that the second player can see it? Thanks