// this is the TCP server that notifies clients that the game has already started. /////////////////////////////////////////////////////////// ////// Released under the GPL ////// see gpl.txt ////// Written by and copyrighted by: ////// Michael Gibson ////// (c)2003, 2004, 2006, 2007 /////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include /* modules */ void load_config(); //Read config file void socket_connect(); // Setup TCP and UDP server connect void socket_bind(); // Setup TCP and UDP server Bind port to card void socket_listen(); // TCP server listen as server void socket_accept(); // TCP accept as server void TCP_proc(); // Process TCP server packets /* socket variables */ struct sockaddr_in fsin; //TCP server structure struct servent *pse; struct protoent *ppe; //What protocal will the server talk? struct sockaddr_in sin; int cliLen; int tcp_sock = 0, // TCP server socket value type1 = 0, // UDP or TCP? 1 for TCP 2 for UDP bind1 = 0, // Server bind successful? lis = 0; // Server listen successful? int alen = 0, server1 = 0; // TCP server file discriptor char send_buff[114]; // TCP send buffer char recv_buff[39]; // TCP recieve buffer int result = 0; // success return int n = 0; // TCP return value int portno; // port server is listening on [configurable] struct sockaddr_in serv_addr, cli_addr; /////////////////////////////////////////////////// // main() /////////////////////////////////////////////////// int main(){ socket_connect(); //TCP server SOCKET CONNECTION socket_bind(); //TCP server SOCKET BINDING socket_listen(); //TCP server SOCKET LISTEN TCP_proc(); //Process TCP server packets return(0); } /////////////////////////////////////////////////// // CONNECT SOCKET /////////////////////////////////////////////////// void socket_connect() { bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; //Load TCP server address load_config(); sin.sin_port = htons(portno); //Load and Convert TCP port to Little Ind ppe = getprotobyname("tcp"); //Declare TCP setbuf(stdout, NULL); server1 = socket(PF_INET, SOCK_STREAM, ppe->p_proto); printf("**********************************************\n"); printf("* MASE Game Server *\n"); printf("**********************************************\n"); printf("Watch out for numbers without a value of zero or greater\n\n"); printf("%d is the socket id of the listening socket\n\n",server1); if (server1 < 0 ) printf("- Cannot create socket\n"); else printf("+ Socket Created Successfully\n"); printf("+ Port %d\n",portno); } ///////////////////////////////////////////////// // SOCKET BINDING ///////////////////////////////////////////////// void socket_bind() { bind1 = bind(server1, (struct sockaddr *)&sin, sizeof(sin)); printf("\n%d value of bind\n", bind1); } ////////////////////////////////////////////////// // SOCKET - LISTEN ////////////////////////////////////////////////// void socket_listen() { setbuf(stdout, NULL); lis = listen(server1, 6); printf("\n%d value of listen \n", lis); if (lis < 0) printf("\n- Cann't listen to port ERROR:"); else printf("\n+ Great! Port is listening \n"); } //////////////////////////////////////////////////// // SOCKET - ACCEPT //////////////////////////////////////////////////// void socket_accept() { alen = sizeof(fsin); tcp_sock = accept(server1, (struct sockaddr *)&fsin, &alen); printf("\n%d socket id of the accepted connection\n", tcp_sock); printf("+ client has connected!\n\n"); } ///////////////////// // //////////////////// void TCP_proc() { char tmp_str[10]; //temp string for grabbing data printf("The TCP Server is Listening ............\n"); //output STDOUT for(;;){ socket_accept(); //accept connection from client strcpy(recv_buff,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //Clear recv buffer with nulls n = recv( tcp_sock, recv_buff, 30, 0); //Recieve first packet printf("recv buffer is: %s\n",recv_buff); //print content of recv_buff1 to out printf("length is %d\n", strlen(recv_buff)); //print recv_buff length to out strcpy(send_buff,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //empty send buffer // 0123 // PQ S if(recv_buff[3] == 'S'){ //Checking to see if it is a Start printf("Recieved Start request, Sending response\n"); //game started already sprintf(send_buff,"gamestarted"); //Format responce packet result=send(tcp_sock, send_buff, strlen(send_buff), 0); //send response packet printf("send buffer: '%s'\n",send_buff); //show content of sent packet printf("length is %d\n\n",strlen(send_buff)); //show length } } } //////////////////////////////////// // load_ config //////////////////////////////////// void load_config() { FILE *fp; fp = fopen("gameserv.conf","r"); //Open config file fscanf(fp,"%d",&portno); //Read Server port fclose(fp); }