this code is a game that uses socket to be able to connect to each other so that there would be a 2player game.. but there something wrong here...here's the code..i'm using linux as my operating system ..this is the client side..

#include <stdio.h>
#include <iostream>
//#include <conio.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

using namespace std;

void error(char *msg)
{
  perror(msg);
  exit(0);
}


int main(int argc, char *argv[])
{
	int  sockfd,portno, n, n1; 
	struct sockaddr_in serv_addr;
	struct hostent *server;
	
	
	char buffer[256];
	if (argc < 3) {
	   fprintf(stderr,"usage %s hostname port\n", argv[0]);
	   exit(0);
	}
	portno = atoi(argv[2]);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
	   error("ERROR opening socket");
	server = gethostbyname(argv[1]);
	if (server == NULL) {
	   fprintf(stderr,"ERROR, no such host\n");
	   exit(0);
	}
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
	   (char *)&serv_addr.sin_addr.s_addr,
	   server->h_length);
	serv_addr.sin_port = htons(portno);
	//if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
	//   error("ERROR connecting");
	//if (connect(sockfd, &serv_addr,sizeof(serv_addr)) < 0) 
        //error("ERROR connecting");	
	printf("Welcome to Battle of Jack 'en Poy\n");
	printf("Please enter your name: ");
	bzero(buffer,256);
	fgets(buffer,255,stdin);
	n = write(sockfd,buffer,strlen(buffer));
	bzero(buffer,256);
	n = write(sockfd," ! ",3);
	
	if (n < 0)
	    error("ERROR writing to socket");
	bzero(buffer,256);
	n = read(sockfd,buffer,255);
	if (n < 0)
	    error("ERROR reading from socket");
		printf("%s\n",buffer);
	return 0;

       
}

this is the server side..

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
#include <windows.h>
#include <conio.h>

using namespace std;

#define splayer 1
#define cplayer 0


char buffer_name[256];
int newsockfd;
int n;

void error(char *msg)
{
  perror(msg);
  exit(1);
}

void game();

string player1="^^^";
string player2="^^^";

int main(int argc, char *argv[])
{
    
    int sockfd, newsockfd, portno, clilen, pid;
	struct sockaddr_in serv_addr, cli_addr;

	if (argc < 2) {
	   fprintf(stderr,"ERROR, no port provided\n");
	   exit(1);
	}
	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);
	if (bind(sockfd, (struct sockaddr *) &serv_addr,
		sizeof(serv_addr)) < 0)
		   error("ERROR on binding");
	listen(sockfd,5);
	clilen = sizeof(cli_addr);
	while (1) {

		if (newsockfd < 0)
	      		error("ERROR on accept");
		pid = fork();
			if (pid < 0)
 	  	        error("ERROR on fork");
			if (pid == 0) {
                        	close(sockfd);
 		        	dostuff();
 		        	exit(0);
     			}
     			else close(newsockfd);
 	} 
  	return 0; 
  	
  	//game();

}

void game()
{
     cout<<"+--------------------------------------+"<<endl;
    cout<<"|   This is a shooting game. You must  |"<<endl;
    cout<<"|   type as many as you can, the       |"<<endl;
    cout<<"|   letters on the screen that you can |"<<endl;
    cout<<"|   see. The player with the most      |"<<endl;
    cout<<"|   number of points wins the game!    |"<<endl;
    cout<<"|   Each player only has 3 lives,      |"<<endl;
    cout<<"|   so if it runs out, you lose!       |"<<endl;
    cout<<"+--------------------------------------+"<<endl;                      
   
    string blocks[25][25];
    //string player1="^^^";
    //string player2="^^^";

//BLAH BLAH BLAH BLAH BLAH BLAH BLAH 
//THIS IS THE MAIN GAME

you know what people...my professor didn't taught us socket..he simply game us this project..so any help would be appreciated..tnx

Recommended Answers

All 2 Replies

Are you being able to communicate from client to server ? what errors are u getting ?

"something wrong" is not a description.
There are plenty of coding errors which may be a problem in the end, but which may not be the most immediate problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.