Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Prisoner\'s Dilemma: The prisoner\'s dilemma is a standard example of a game

ID: 3910778 • Letter: T

Question

The Prisoner's Dilemma: The prisoner's dilemma is a standard example of a game analyzed in game theory that shows why two completely "rational" individuals might not cooperate, even if it appears that it is in their best interests to do so.1 It is presented as follows: "Two members of a criminal gang are arrested and imprisoned. Each prisoner is in solitary confinement with no means of communicating with the other. The prosecutors lack sufficient evidence to convict the pair on the principal charge. They hope to get both sentenced to a year in prison on a lesser charge. Simultaneously, the prosecutors offer each prisoner a bargain. Each prisoner is given the opportunity either to: betray the other by testifying that the other committed the crime, or to cooperate with the other by remaining silent. The offer is: . If A and B each betray the other, each of them serves 2 years in prison . If A betrays B but B remains silent, A will be set free and B will serve 3 years in prison (and vice versa) If A and B both remain silent, both will only serve 1 year in prison (on the lesser charge)"1 . Your Task: you will write simple client-server application(s) in which the S client acts as prisoner A and the server acts as prisoner B. The a protocol between the client and server should be as follows: » The server program is started on a user-defined port. » The client program is started and connects to the server 0 using the server IP and port number provided on the command line.

Explanation / Answer

Sender.c File:-
==========
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv[])
{
int clientSocket;
char *IpAddress;
int Portno;
char response_buffer[100];
char request_buffer[100];
char buf[100];
time_t curtime;
struct tm *loc_time;
struct sockaddr_in serverAddr;
socklen_t addr_size;
if(argc<3)
{
      printf(" Please Pass IP address and Port Number and Message as a Parameter");
      exit(0);
}
IpAddress=argv[1]; //Assigning first Arguement as a IPaddress.
printf(" The Ip address is : %s",IpAddress);   
Portno=atoi(argv[2]);    //Assigning Second Arguement as a Port Number.
cout<<" The Port Number is: "<<Portno<<endl;
request_buffer=argv[3];
    //create The Socket Connection
if(!(clientSocket = socket(PF_INET, SOCK_STREAM, 1)))
{
     printf(" Failed to create a Scoket: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(Portno);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
//Connecting with IPaddress
if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
{
   printf("Connect Error: ");
   exit(0);
}
curtime = time (NULL);
loc_time = localtime (&curtime);
strftime (buf, 140, "Time is %I:%M %p. ", loc_time);
fputs (buf, stdout);
   //Send Request to The Server
send(clientSocket,request_message,sizeof(request_message),0);
printf(" Request Successfully sent to the server: ");
//Receive Response from the Server
recv(clientSocket, response_buffer, sizeof(response_buffer), 0);
printf(" Received Response from the Server is %s",response_buffer);
return 0;
}

Receiver.c file:-
============

#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv)
{
int Server_Socket, newSocket;
char response_buffer[500];
char request_buffer[1000];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
      printf("Socket Error: ");
      exit(0);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(Server_Socket,5)==0)
{
      printf("Listening ");
}
else
{
      printf("Error ");
      exit(0);
}
addr_size = sizeof serverStorage;
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(response_buffer," Hello Welcome Please wait!: ");
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" Received Request from the Client is :%s",request_buffer);
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote