Client Server Game in C I have a server and client program t
Client Server Game in C-
I have a server and client program that allows the client to enter rock paper or scissors, then the server decides the winner between player 1 vs player 2(player 2 is a computer).
First how would I make it so the server is able to send the results back to the client. And then also maybe how would i implement 2 clients to play vs each other. Thanks for any help!
//server source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 1024
#define SERVER_IP \"127.0.0.1\"
#define SERVER_PORT 60000
int main(int argc, char *argv[])
{
int socket_listen;
int socket_receive;
struct sockaddr_in my_address;
struct sockaddr_in receive_address;
int i;
int address_size;
int bytes_received;
struct sockaddr remote_address;
int receive_message_size;
char buffer[BUFFER_SIZE];
int select_ret;
/* Create a socket for listening for incoming connections. */
socket_listen = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
printf(\"socket_listen = %d\ \", socket_listen);
if (socket_listen < 0)
{
printf(\"socket() failed\ \");
exit(0);
}
printf(\"Server socket created\ \");
/* Make address structure for my local address. */
memset(&my_address, 0, sizeof(my_address));
my_address.sin_family = AF_INET;
my_address.sin_addr.s_addr = inet_addr(SERVER_IP);
my_address.sin_port = htons((unsigned short int)SERVER_PORT);
/* Bind socket to the local address. */
i = bind(socket_listen, (struct sockaddr *)&my_address, sizeof(my_address));
if (i < 0)
{
printf(\"bind() failed\ \");
exit(0);
}
printf(\"Server socket bound\ \");
/* Listen for incoming connections. */
i = listen(socket_listen, 5);
if (i < 0)
{
printf(\"listen() failed\ \");
exit(0);
}
printf(\"Server waiting for connection\ \");
/* Accept the incoming connection. */
address_size = sizeof(receive_address);
socket_receive = accept(socket_listen, (struct sockaddr *) &receive_address, &address_size);
printf(\"socket_receive = %d\ \", socket_receive);
printf(\"Server received a connection\ \");
int player2;
while (1)
{
bytes_received = recv(socket_receive, buffer, BUFFER_SIZE, 0);
buffer[bytes_received] = \'\\0\';
printf(\"\ Player 1 used: %s\ \", buffer);
if (strcmp(buffer, \"rock\")==0)
{
srand(time(NULL));
player2 = (rand() % 3);
if (player2 == 0)
{
printf(\"Player 2 used: rock\ its a draw!\ \ \");
}
else if (player2 == 1)
{
printf(\"Player 2 used: paper\ Player 2 wins!\ \ \");
}
else if (player2 == 2)
{
printf(\"Player 2 used: scissors\ Player 1 wins!\ \ \");
}
}
else if (strcmp(buffer, \"paper\")==0)
{
srand(time(NULL));
player2 = (rand() % 3);
if (player2 == 0)
{
printf(\"Player 2 used: rock\ Player 1 wins!\ \ \");
}
else if (player2 == 1)
{
printf(\"Player 2 used: paper\ its a draw!\ \ \");
}
else if (player2 == 2)
{
printf(\"Player 2 used: scissors\ Player 2 wins!\ \ \");
}
}
else if (strcmp(buffer, \"scissors\")==0)
{
srand(time(NULL));
player2 = (rand() % 3);
if (player2 == 0)
{
printf(\"Player 2 used: rock\ Player 2 wins!\ \ \");
}
else if (player2 == 1)
{
printf(\"Player 2 used: paper\ Player 1 wins!\ \ \");
}
else if (player2 == 2)
{
printf(\"Player 2 used: scissors\ its a draw!\ \ \");
}
}
else if (strcmp(buffer, \"shutdown\")==0)
{
break;
}
else
{
printf(\"bad move, try again\ \ \");
}
}
close(socket_receive);
close(socket_listen);
return 0;
}
//client source code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 1024
#define SERVER_IP \"127.0.0.1\"
#define SERVER_PORT 60000
int main(int argc, char *argv[])
{
int socket_send;
struct sockaddr_in address_send;
int i;
char text[80];
char buffer[BUFFER_SIZE];
int send_length;
int bytes_sent;
/* Create the socket. */
socket_send = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
printf(\"socket_send = %d\ \", socket_send);
if (socket_send < 0)
{
printf(\"socket() failed\ \");
exit(0);
}
printf(\"Client socket created\ \");
/* Create address structure */
memset(&address_send, 0, sizeof(address_send));
address_send.sin_family = AF_INET;
address_send.sin_addr.s_addr = inet_addr(SERVER_IP);
address_send.sin_port = htons((unsigned short)SERVER_PORT);
/* Connect to the server. */
i = connect(socket_send, (struct sockaddr *)&address_send, sizeof(address_send));
if (i < 0)
{
printf(\"connect() failed\ \");
exit(0);
}
printf(\"Client connected to server\ \");
while (1)
{
printf(\"\ rock, paper, or scissors? \");
scanf(\"%s\", text);
if (strcmp(text, \"quit\") == 0)
{
break;
}
strcpy(buffer, text);
send_length = strlen(text);
bytes_sent = send(socket_send, buffer, send_length, 0);
}
close(socket_send);
return 0;
}
Solution
For the first question how to communicate you can design the program as a Standard Chat program where client and sever both can communicate with each other. The only catch is that instade of message it also allows the move of the game that can be send and both the client and sever are able to detect it.It can be done using TCP socket.
For the second question how to play with multiple clients you can use the concept of below
1.You can configure a router for the multiple clients.
2. The concept is same as one server with miltiple clients that can communicate with each other. As a commucating protocol it can use TCP protocol. We consider one client as a root client and it informs if any other client added along with the name and the position. One thing is that no program allows the same naming clients used multiple time. So it is required that the names of the clients sholud be different. So before adding it checks the name of the clients wheather it exists in the program or not. If exists then it request for a rename ,otherwise the client is added in the program. As per chat program first we have to run the server program first. After checking that the server program program runs well we can execute the client program. We can run client program multiple time as many client reqired by the user. Now we can communicate with multiple client at a time. This concept can be use here. Insteade of message the clients send the moves in case of the game as the output. The other client detects that output given by one client and take action according to that.
3. We can use the concept of threads here. We can creat multiple threads as per need as for example if we have 3 user then wecan creat 3 threads giving different names. They can communicate using some shared object and communicate ecah other whenever excute any event.




