Skip to content

Instantly share code, notes, and snippets.

@akkaash
Created January 31, 2014 16:34
Show Gist options
  • Save akkaash/8735715 to your computer and use it in GitHub Desktop.
Save akkaash/8735715 to your computer and use it in GitHub Desktop.
Hot Potato Game using network programming
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define ALL 0
#define ID_INFO 1
#define RECV_LISTEN_PORT 2
#define SEND_RIGHT_INFO 3
#define ACCE 4
#define CONN 5
#define ACKC 6
#define POTATO1 7
#define POTATO2 8
#define END_GAME 9
int N;
int max(int a, int b){
return ((a>b)?a:b);
}
void processFinalPacket(char *msg, int length){
char *tmp = (char *) malloc(length);
char *token = ":";
tmp = strtok(msg, token);
//printf("\trecvd hops = |%s|\n", tmp);
tmp = strtok(NULL, token);
printf("trace |%s|\n", tmp);
}
void sendShutDown(int *playerConnectedDesList){
int i;
for(i=0; i<N; i++){
//printf("\tshutting down %d\n", i);
}
}
int sendControlData(int sock, int type, long length){
char *controlData = malloc(11);
memset(controlData, 0, 11);
sprintf(controlData, "%d:%lu",type, length);
int sendLen = send(sock, controlData, 10, 0);
if(sendLen != 10){
perror("send");
exit(-1);
}
//printf("\tcontrolData %s\n", controlData);
//printf("\tafter send");
}
//returns the length of the message to be received
long recvControlData(int sock){
char *controlData = malloc(11);
memset(controlData, 0, 11);
//printf("\there\n"); fflush(stdout);
int recvLen = recv(sock, controlData, 10, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
//printf("\t\trecv controlData |%s|\n", controlData); fflush(stdout);
char *tmp = malloc(11);
memset(tmp, 0, 11);
tmp = strtok(controlData, ":");
//printf("\t\ttmp %s\n", tmp);
/* free(tmp);*/
/* */
/* tmp = malloc(11);*/
/* memset(tmp, 0, 11);*/
tmp = strtok(NULL, ":");
//printf("\t\ttmp %s\n", tmp);
return atoi(tmp);
}
int main(int argc, char *argv[]){
int masterSocket, masterPort, returnCode, HOPS;
int sendLen, recvLen;
if(argc < 4 || argc > 4){
fprintf(stderr, "Usage: master <port> <N> <HOPS>\n");
exit(-1);
}
N = atoi(argv[2]);
HOPS = atoi(argv[3]);
if(N < 2){
printf("Please provide more than 1 player\n");
exit(0);
}
struct addrinfo masterHints, *masterInfo, *p;
struct sockaddr_storage playerAddrStorage;
socklen_t sinSize;
memset(&masterHints, 0, sizeof masterHints);
masterHints.ai_family = AF_INET;
masterHints.ai_socktype = SOCK_STREAM;
masterHints.ai_flags = AI_PASSIVE;
returnCode = getaddrinfo(NULL, argv[1], &masterHints, &masterInfo);
if(returnCode != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(returnCode));
exit(-1);
}
int yes = 1;
for(p=masterInfo; p!=NULL; p=p->ai_next){
//printf("\tinside\n");
if((masterSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
perror("socket");
exit(-1);
}
if(setsockopt(masterSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror("setsockopt");
exit(-1);
}
if(bind(masterSocket, p->ai_addr, p->ai_addrlen) == -1){
close(masterSocket);
perror("bind");
continue;
}
}
/* if(p == NULL){*/
/* fprintf(stderr, "master failed to bind. Exiting!\n");*/
/* exit(-1);*/
/* }*/
if(listen(masterSocket, N) == -1){
perror("listen");
exit(-1);
}
int i=0;
int playerConnectedDes;
int *playerConnectedDesList;
playerConnectedDesList = malloc(N * sizeof(int));
char **listenPortList = (char **) malloc(N * sizeof(char *));
//char **playerConnectedDataList = (char**)malloc(N*sizeof(char *));
char **playerIps = (char **) malloc(N * sizeof(char*));
long lengthToBeRecvd;
char *hostName = (char *)malloc(NI_MAXHOST);
memset(hostName, 0, NI_MAXHOST);
gethostname(hostName, NI_MAXHOST);
printf("Potato Master on %s\n", hostName);
printf("Players = %d\n", N);
printf("Hops = %d\n", HOPS);
for(i=0;i<N;i++){
sinSize = sizeof playerAddrStorage;
//printf(" accepting..\n");
playerConnectedDes = accept(masterSocket, (struct sockaddr *)&playerAddrStorage, &sinSize);
if(playerConnectedDes < 0){
perror("accept");
exit(-1);
} else{
//printf("\tplayer %d connected \n", i);
char *playerName = (char *) malloc(64);
memset(playerName, 0, 64);
char *playerService = (char *) malloc(64);
memset(playerService, 0, 64);
returnCode = getnameinfo((struct sockaddr*)&playerAddrStorage, sizeof(playerAddrStorage), playerName, NI_MAXHOST, NULL, 0, 0);
if(returnCode < 0){
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(returnCode));
exit(-1);
}
printf("player %d is on %s\n", i, playerName);
//-----------sending information about the ids-----------
char *str = malloc(11);
memset(str, 0, 11);
sprintf(str, "%d", i);
sendControlData(playerConnectedDes, ID_INFO, strlen(str));
sendLen = send(playerConnectedDes, str, strlen(str), 0);
if(sendLen != strlen(str)){
perror("send");
exit(-1);
}
//printf("\tsent id |%s|\n", str);
//--------------recv the listenPort---------------
free(str);
lengthToBeRecvd = recvControlData(playerConnectedDes);
str = malloc(11);
memset(str, 0, 11);
recvLen = recv(playerConnectedDes, str, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
//printf("\trecv listenPort |%s|\n", str);
//--------------store all information-------------
//store the descriptor------------------
playerConnectedDesList[i] = playerConnectedDes;
//store the listenPort numbers--------------------
listenPortList[i] = (char *) malloc(strlen(str));
strcpy(listenPortList[i], str);
//store the sockaddr_storage----------------------
/* playerConnectedDataList[i] = (char *) malloc(15 * sizeof(char));*/
/* strcpy(playerConnectedDataList[i], ((struct sockaddr *)&playerAddrStorage)->sa_data);*/
/* //printf("\tstored: %s\n", ((struct sockaddr *)&playerAddrStorage)->sa_data);*/
/* //printf("\tstrlen %d\n", strlen(playerConnectedDataList[i]));*/
struct sockaddr_in *sa = (struct sockaddr_in*)&playerAddrStorage;
char *str1 = malloc(INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(sa->sin_addr), str1, INET_ADDRSTRLEN);
//printf("\t-> %d, %d |%s|\n", INET_ADDRSTRLEN, strlen(str1), str1);
playerIps[i] = (char *) malloc(INET_ADDRSTRLEN);
strcpy(playerIps[i], str1);
}
}
//printf("\toutside for\n");
//-------- send information about right...
for(i=0; i<N; i++){
char *info = malloc(100);
memset(info, 0, 100);
sprintf(info, "%d:%d:%s:%s", N, (i+1)%N, playerIps[(i+1)%N], listenPortList[(i+1)%N]);
//printf("\t\tinfo %s\n", info);
sendLen = sendControlData(playerConnectedDesList[i%N], 0, strlen(info));
sendLen = send(playerConnectedDesList[i%N], info, strlen(info), 0);
}
//printf("\toutside for2\n");
//start connections----------------------
for (i = 0; i < N; i += 1)
{
char *msg = malloc(11);
memset(msg, 0, 11);
sprintf(msg, "%s", "acce");
sendControlData(playerConnectedDesList[(i+1)%N], ACCE, strlen(msg));
int sendLen = send(playerConnectedDesList[(i+1)%N], msg, strlen(msg), 0);
if(sendLen != strlen(msg)){
perror("send");
exit(-1);
}
//printf("\tsent |%s|\n",msg);
free(msg);
msg = malloc(11);
memset(msg, 0, 11);
sprintf(msg, "%s", "conn");
sendControlData(playerConnectedDesList[i%N], CONN, strlen(msg));
sleep(1);
sendLen = send(playerConnectedDesList[i%N], msg, strlen(msg), 0);
if(sendLen != strlen(msg)){
perror("send");
exit(-1);
}
// printf("\tsent |%s|\n",msg);
free(msg);
//recv ackc
lengthToBeRecvd = recvControlData(playerConnectedDesList[(i+1)%N]);
msg = malloc(lengthToBeRecvd+1);
memset(msg, 0, lengthToBeRecvd+1);
//printf("\there1\n");
int recvLen = recv(playerConnectedDesList[(i+1)%N], msg, lengthToBeRecvd, 0);
if(recvLen<0){
perror("recv");
exit(-1);
}
// printf("\there2\n");
fflush(stdout);
// printf("\trecv %s\n", msg);
fflush(stdout);
free(msg);
}
//setting readfds---------------------------
fd_set readfds, masterSet;
FD_ZERO(&readfds);
FD_ZERO(&masterSet);
int maxFd;
maxFd = playerConnectedDesList[0];
for(i=0;i<N;i++){
FD_SET(playerConnectedDesList[i], &masterSet);
maxFd = max(maxFd, playerConnectedDesList[i]);
}
//set---------------------------------
//start game=--------------------------
srand(time(NULL));
int startPlayerId = rand() % N;
if(HOPS == 0){
printf("All players present, not sending potato\n");
char *shutDownMsg = "shutDown";
for(i=0;i<N;i++){
sendControlData(playerConnectedDesList[i], END_GAME, strlen(shutDownMsg));
sendLen = send(playerConnectedDesList[i], shutDownMsg, strlen(shutDownMsg), 0);
if(sendLen < 0){
perror("send");
exit(-1);
}
}
exit(0);
}
if(HOPS>0){
printf("All players present, sending potato to player %d\n", startPlayerId);
//prepare potato----
char *potato = (char*)malloc(11);
memset(potato, 0, 11);
sprintf(potato, "%d:", HOPS);
sendControlData(playerConnectedDesList[startPlayerId], POTATO1, strlen(potato));
sendLen = send(playerConnectedDesList[startPlayerId], potato, strlen(potato), 0);
if(sendLen != strlen(potato)){
perror("send");
exit(-1);
}
/* for(;;){*/
/* if(select(maxFd+1, &readfds, NULL, NULL, NULL) == -1){*/
/* perror("select");*/
/* exit(-1);*/
/* }*/
/* */
/* for(i=0;i<N;i++){*/
/* if(FD_ISSET(i, &readfds)){*/
/* if(i==playerConnectedDesList[i])*/
/* }*/
/* }*/
/* }*/
int endGameFlag = 0;
//while(1){
//------------------------
readfds = masterSet;
int finalPlayer;
returnCode = select(maxFd+1, &readfds, NULL, NULL, NULL);
if(returnCode < 0){
perror("select");
exit(-1);
}
for(i=0;i<=maxFd;i++){
if(FD_ISSET(i, &readfds)){
finalPlayer = i;
endGameFlag = 1;
break;
}
}
//if(endGameFlag == 1) break;
//}
lengthToBeRecvd = recvControlData(finalPlayer);
char *finalMsg = (char *) malloc(lengthToBeRecvd + 10);
memset(finalMsg, 0, lengthToBeRecvd+10);
recvLen = recv(finalPlayer, finalMsg, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
char *tmp;
tmp = strtok(finalMsg, ":");
tmp = strtok(NULL, ":");
printf("Trace of potato: \n%s\n", tmp);
if(endGameFlag == 1){
//recv the final messages
char *shutDownMsg = "shutDown";
for(i=0;i<N;i++){
sendControlData(playerConnectedDesList[i], END_GAME, strlen(shutDownMsg));
sendLen = send(playerConnectedDesList[i], shutDownMsg, strlen(shutDownMsg), 0);
if(sendLen < 0){
perror("send");
exit(-1);
}
//sleep(1);
//close(playerConnectedDesList[i]);
//printf("\tclose %d now chup chap", i);
}
exit(0);
}
}
//while(1){}
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#define ALL 0
#define ID_INFO 1
#define RECV_LISTEN_PORT 2
#define SEND_RIGHT_INFO 3
#define ACCE 4
#define CONN 5
#define ACKC 6
#define POTATO1 7
#define POTATO2 8
#define END_GAME 9
int masterConnectSocket, leftPlayerDes, rightPlayerConnectSocket;
int ID;
int sendLen, recvLen;
int leftID, rightID;
int sentZeroHopsToMaster = 0;
int max(int a, int b){
return ((a>b)?a:b);
}
int sendControlData(int sock, int type, long length){
char *controlData = (char *)malloc(11);
memset(controlData, 0, 11);
sprintf(controlData, "%d:%lu",type, length);
sendLen = send(sock, controlData, 10, 0);
if(sendLen != 10){
perror("send");
exit(-1);
}
//printf("\tsent controlData |%s|\n", controlData);fflush(stdout);
//printf("after send");
}
//returns the length of the message to be received
long recvControlData(int sock){
char *controlData = malloc(11);
memset(controlData, 0, 11);
//printf("here\n"); fflush(stdout);
recvLen = recv(sock, controlData, 10, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
//printf("\t recv controlData |%s|", controlData); fflush(stdout);
char *tmp = malloc(11);
memset(tmp, 0, 11);
tmp = strtok(controlData, ":");
//printf("\ttmp %s\n", tmp);
/* free(tmp);*/
/* */
/* tmp = malloc(11);*/
/* memset(tmp, 0, 11);*/
tmp = strtok(NULL, ":");
//printf("\ttmp %s\n", tmp);fflush(stdout);
//free(controlData);
//free(tmp);
return atoi(tmp);
}
/*int onNewHopsZero(int sock, char *msg){*/
/* */
/* //printf("\tonNewHopsZero\n");*/
/* sendControlData(sock, msg, strlen(msg));*/
/* */
/* sendLen = send(sock, msg, strlen(msg), 0);*/
/* if(sendLen != strlen(msg)){*/
/* perror("send");*/
/* exit(-1);*/
/* }*/
/*}*/
int processPotato(char *potato, int length, int recvFrom){
int sendTo;
if(recvFrom == masterConnectSocket){
//check for shutDownMsg
if(strncmp(potato, "shutDown", length) == 0){
//printf("\trecv shutDown\n");
/* close(masterConnectSocket);*/
/* close(leftPlayerDes);*/
/* close(rightPlayerConnectSocket);*/
exit(0);
}
}
//printf("\tprocessPotato |%s|\n", potato); fflush(stdout);
char *newPotato = (char *) malloc(length + 10);
memset(newPotato, 0, length + 10);
char *token = ":";
char *tmp = (char *) malloc(length + 10);
tmp = strtok(potato, token);
int recvdHops = atoi(tmp);
int newHops = recvdHops - 1;
//printf("\tnewHops %d\n", newHops);fflush(stdout);
//printf("\t\t****** PAY VERY CLOSE ATTENTION HERE ******");fflush(stdout);
tmp = strtok(NULL, token);
if(tmp == NULL){
tmp = (char *) malloc(length + 10);
strcpy(tmp, "");
}
//printf("\ttrace |%s|\n", tmp);
//printf("\t\t****** CHILL MAAR ****************");fflush(stdout);
char * trace = (char *) malloc(length + 10);
memset(trace, 0, length + 10);
sprintf(newPotato, "%d:%s%d", newHops, tmp, ID);
//strcat(trace, ID);
if(newHops == 0){
sendTo = masterConnectSocket;
sentZeroHopsToMaster = 1;
printf("I'm it\n");fflush(stdout);
} else{
//random
int nextPlayer = rand() % 2;
if(nextPlayer == 0)
sendTo = leftPlayerDes;
else
sendTo = rightPlayerConnectSocket;
//
strcat(newPotato, ",");
}
/* if(newHops == 0)*/
/* sendTo = masterConnectSocket;*/
/* else{*/
/* int nextPlayer = rand() % 2;*/
/* if(nextPlayer == 0)*/
/* sendTo = leftPlayerDes;*/
/* else*/
/* sendTo = rightPlayerConnectSocket;*/
/* }*/
/* if(recvFrom == masterConnectSocket){*/
/* sprintf(newPotato, "%d:%d", newHops, ID);*/
/* */
/* */
/* } else{*/
/* tmp = strtok(NULL, token);*/
/* char *trace = (char *) malloc(length+10);*/
/* memset(trace, 0, length + 10);*/
/* sprintf(trace, "%s", tmp);*/
/* */
/* sprintf(newPotato, "%d:%s,%d", newHops, trace, ID);*/
/* */
/* int nextPlayer = rand() % 2;*/
/* if(nextPlayer == 0)*/
/* sendTo = leftPlayerDes;*/
/* else*/
/* sendTo = rightPlayerConnectSocket;*/
/* */
/* }*/
if(sendTo == masterConnectSocket)
{//printf("\tsending to master\n");
fflush(stdout);}
else if(sendTo == leftPlayerDes)
{printf("\tsending to %d\n", leftID);fflush(stdout);}
else if(sendTo == rightPlayerConnectSocket)
{printf("\tsending to %d\n", rightID);fflush(stdout);}
//else
//printf("oops!!!\n");fflush(stdout);
//printf("\t newPotato |%s|\n", newPotato);fflush(stdout);
sendControlData(sendTo, POTATO2, strlen(newPotato));
sendLen = send(sendTo, newPotato, strlen(newPotato), 0);
if(sendLen!=strlen(newPotato)){
perror("send");
exit(-1);
}
//printf("\tsent newPotato\n");
}
int main(int argc, char *argv[]){
long lengthToBeRecvd;
int returnCode;
if(argc != 3){
fprintf(stderr, "Usage: player <master machine name> <port>\n");
exit(-1);
}
int masterPort = atoi(argv[2]);
//---- create and bind the left listening port
struct addrinfo playerListenHints, *playerListenInfo, *q;
struct sockaddr_storage playerAddrStorage;
socklen_t sinSize;
memset(&playerListenHints, 0, sizeof playerListenHints);
playerListenHints.ai_family = AF_INET;
playerListenHints.ai_socktype = SOCK_STREAM;
playerListenHints.ai_flags = AI_PASSIVE;
int listenPort = masterPort + 1;
int listenSocket;
int bindFail = 1;
while(bindFail == 1){
//first convert the listenPort to string for getaddrinfo
char *listenPortString = malloc(5);
sprintf(listenPortString, "%d", listenPort);
returnCode = getaddrinfo(NULL, listenPortString, &playerListenHints, &playerListenInfo);
if(returnCode != 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(returnCode));
exit(-1);
}
for(q = playerListenInfo; q!=NULL; q=q->ai_next){
if((listenSocket=socket(q->ai_family, q->ai_socktype, q->ai_protocol)) < 0){
perror("socket");
exit(-1);
}
if(bind(listenSocket, q->ai_addr, q->ai_addrlen) < 0){
close(listenSocket);
bindFail = 1;
//printf("bindFail\n");
listenPort++;
} else{
bindFail = 0;
}
}
}
//printf("bound\n");
//-------------
//-------------
//---------- talking to master
//int masterConnectSocket;
struct addrinfo hints, *masterInfo, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
returnCode = getaddrinfo(argv[1], argv[2], &hints, &masterInfo);
if(returnCode < 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(returnCode));
exit(-1);
}
// printf("\t%d\n", masterInfo->ai_socktype);
//int ID;
for(p=masterInfo; p!=NULL;p=p->ai_next){
masterConnectSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(masterConnectSocket < 0){
perror("socket");
continue;
}
if(connect(masterConnectSocket, p->ai_addr, p->ai_addrlen) < 0){
close(masterConnectSocket);
perror("connect");
continue;
} else{
//recv information about id
lengthToBeRecvd = recvControlData(masterConnectSocket);
char *buff = malloc(11);
memset(buff, 0, 11);
recvLen = recv(masterConnectSocket, buff, 10, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
//printf("recv id |%s|\n", buff);
ID = atoi(buff);
printf("Connected as player %d\n", ID);
//send the listenPort
free(buff);
buff = malloc(11);
memset(buff, 0, 11);
sprintf(buff, "%d", listenPort);
sendControlData(masterConnectSocket, RECV_LISTEN_PORT, strlen(buff));
sendLen = send(masterConnectSocket, buff, strlen(buff), 0);
if(sendLen < 0){
perror("send");
exit(-1);
}
//printf("send listenPort |%s|\n", buff);
break;
}
}
//------ recv right info
/* char *rightConnectPort = malloc(6);*/
/* char *rightConnectIp = malloc(INET_ADDRSTRLEN);*/
/* */
/* char *info = (char *)malloc(INET_ADDRSTRLEN + 6);*/
/* recvLen = recv(masterConnectSocket, info, 20, 0);*/
/* if(recvLen < 0){*/
/* perror("recv");*/
/* exit(-1);*/
/* }*/
/* printf("recv |%s|\n", info);*/
/* */
/* */
/* strcpy(rightConnectIp, info);*/
/* //free(info);*/
/* */
/* info = (char *)malloc(6);*/
/* recvLen = recv(masterConnectSocket, info, 5, 0);*/
/* if(recvLen < 0){*/
/* perror("recv");*/
/* exit(-1);*/
/* }*/
/* strcpy(rightConnectPort, info);*/
/* printf("recv |%s|\n", info);*/
/* //free(info);*/
//recv right information..~~~~~~~~~~~~~~~~~~~~~
lengthToBeRecvd = recvControlData(masterConnectSocket);
//printf("lengthToBeRecvd = %d\n", lengthToBeRecvd);
char *info = malloc(lengthToBeRecvd + 1);
memset(info, 0, lengthToBeRecvd+1);
recvLen = recv(masterConnectSocket, info, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
//printf("info recv |%s|\n", info);
//process the recvd information
char *tmp = (char*) malloc(lengthToBeRecvd + 1);
memset(tmp, 0, lengthToBeRecvd+1);
char *token = ":";
tmp = strtok(info, token);
//printf("\ttmp %s\n", tmp);
int N = atoi(tmp);
//printf("\tN |%d|\n", N);
tmp = strtok(NULL, token);
//printf("\ttmp %s\n", tmp);
rightID = atoi(tmp);
leftID = ID-1;
if(leftID<0)
leftID = N-1;
//printf("\trightID |%d|\n", rightID);
tmp = strtok(NULL, token);
//printf("\ttmp %s\n", tmp);
char *rightConnectIp = (char *)malloc(strlen(tmp)+1);
memset(rightConnectIp, 0, strlen(tmp)+1);
sprintf(rightConnectIp, "%s", tmp);
//printf("\trightConnectIp |%s|\n", rightConnectIp);
tmp = strtok(NULL, token);
//printf("\ttmp %s\n", tmp);
char *rightConnectPort = (char *) malloc(strlen(tmp)+1);
memset(rightConnectPort, 0, strlen(tmp)+1);
sprintf(rightConnectPort, "%s", tmp);
//printf("\trightConnectPort |%s|\n", rightConnectPort);
//-------------- recv messages and start connection...
int acceptFlag = 0, connectFlag =0;
//int rightPlayerConnectSocket;
//int leftPlayerDes;
while(acceptFlag == 0 || connectFlag == 0){
//printf("acceptFlag %d\n", acceptFlag);
// printf("connectFlag %d\n", connectFlag);
lengthToBeRecvd = recvControlData(masterConnectSocket);
// printf("lengthToBeRecvd = %d\n", lengthToBeRecvd);
char *msg = malloc(lengthToBeRecvd+1);
memset(msg, 0, lengthToBeRecvd+1);
recvLen=recv(masterConnectSocket, msg, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
// printf("connection est phase-> |%s|\n", msg);
fflush(stdout);
if (strncmp(msg, "acce", lengthToBeRecvd) == 0){
//-------------------------
// printf("listening\n");
returnCode = listen(listenSocket, 10);
if(returnCode<0){
perror("listen");
exit(-1);
}
struct sockaddr_in leftPlayerSockAddrIn;
int len = sizeof(leftPlayerSockAddrIn);
// printf("accepting.. %d\n", listenSocket);
leftPlayerDes = accept(listenSocket, (struct sockaddr*)&leftPlayerSockAddrIn, &len);
// printf("accepted..");
fflush(stdout);
//send ackc
char *ackcMsg;
ackcMsg = malloc(11);
memset(ackcMsg, 0, 11);
sprintf(ackcMsg, "%s", "ackc");
sendControlData(masterConnectSocket, ACKC, strlen(ackcMsg));
sendLen = send(masterConnectSocket, ackcMsg, strlen(ackcMsg), 0);
if(sendLen != strlen(ackcMsg)){
perror("send");
exit(-1);
}
// printf("sent accept..\n");
fflush(stdout);
acceptFlag=1;
//---------------------------
} else if(strncmp(msg, "conn", lengthToBeRecvd) == 0){
// printf("connecting...\n");
struct addrinfo rightHints, *rightInfo;
memset(&rightHints, 0, sizeof rightHints);
rightHints.ai_family = AF_INET;
rightHints.ai_socktype = SOCK_STREAM;
returnCode = getaddrinfo(rightConnectIp, rightConnectPort, &rightHints, &rightInfo);
if(returnCode < 0){
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(returnCode));
exit(-1);
}
int flag = 0;
for(p = rightInfo; p != NULL && flag == 0; p=p->ai_next){
rightPlayerConnectSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(rightPlayerConnectSocket < 0){
perror("socket");
exit(-1);
}
if(connect(rightPlayerConnectSocket, p->ai_addr, p->ai_addrlen) < 0){
close(rightPlayerConnectSocket);
perror("connect");
} else{
flag = 1;
connectFlag = 1;
// printf("connected");
}
}
}
// printf("\t acceptFlag = %d\n", acceptFlag);
// printf("\t connectFlag = %d\n", connectFlag);
}
srand(time(NULL));
fd_set masterSet;
fd_set readfds;
FD_ZERO(&masterSet);
FD_ZERO(&readfds);
FD_SET(masterConnectSocket, &masterSet);
FD_SET(leftPlayerDes, &masterSet);
FD_SET(rightPlayerConnectSocket, &masterSet);
int maxFd;
maxFd = masterConnectSocket;
maxFd = max(max(maxFd, leftPlayerDes), rightPlayerConnectSocket);
//printf("\tmasterConnectSocket %d\n", masterConnectSocket);
//printf("\tleftPlayerDes %d\n", leftPlayerDes);
//printf("\trightPlayerConnectSocket %d\n", rightPlayerConnectSocket);
//printf("\tmaxFd = %d\n", maxFd);
int i;
while(1){
readfds = masterSet;
//printf("\tselect call \n");
if(select(maxFd+1, &readfds, NULL,NULL, NULL) == -1){
perror("select");
exit(-1);
}
//printf("\tselect returned\n");
for(i=0;i<=maxFd;i++){
// printf("\t\ti=%d\n", i);
if(FD_ISSET(i, &readfds)){
if(i == masterConnectSocket){
// printf("\tgot something from master\t start recv\n");
lengthToBeRecvd = recvControlData(masterConnectSocket);
char *potato = (char *) malloc(lengthToBeRecvd + 1);
memset(potato, 0, lengthToBeRecvd + 1);
recvLen = recv(masterConnectSocket, potato, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
processPotato(potato, strlen(potato), masterConnectSocket);
} else if (i ==leftPlayerDes || i == rightPlayerConnectSocket){
/* if(i ==leftPlayerDes)*/
/* // printf("\tgot something from left\t start recv\n");*/
/* else if (i == rightPlayerConnectSocket)*/
/* // printf("\tgot something from right\t start recv\n");*/
lengthToBeRecvd = recvControlData(i);
char *potato = (char *) malloc(lengthToBeRecvd + 1);
memset(potato, 0, lengthToBeRecvd + 1);
recvLen = recv(i, potato, lengthToBeRecvd, 0);
if(recvLen < 0){
perror("recv");
exit(-1);
}
processPotato(potato, strlen(potato), i);
}
}
}
}
//printf("idhar nahi aana chahiye chutiye!!\n");
while(1){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment