Help with network game - socket programming /w c++

I managed to find this 2 files from this web:
http://www.linuxhowtos.org/C_C++/socket.htm

I need to create a network game using socket programming.
For example battleship game, where we have the interface of the grids.
Have a textfile to store the "coordinates" of the different types of ships.

And when play on client's side enter a grid square number like "F6", system will check on server side if there is a ship on that spot or not. If there is then reply the client with a "<Ship type> hit!" and the position of the shot on the grid will be marked with a red version of the previous letter, like "SSS" to "SS<red>S"

But if the shot is missed. It will mark an X on the grid and feedback to user "You missed!".

The 2 file is helpful in the sense that it demonstrates the communication between server and client with the stored "question/reply" in an external textfile.

Can someone guide me on how to go about achieving this?

This is the code from the two demo files

server file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <string.h>

#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>

using namespace std;

//function to handle the file and comparision

int clientside (int);

int main(int argc, char *argv[])
{
	int connfd, pid, portNo, listenfd;
	socklen_t len;
	bool loop = false;
	struct sockaddr_in servaddr, client_addr;

	//make sure the user setting up server enters in correct arguments

    	if (argc < 2)
    	{
        	cerr << "Syntax : ./server <port>" << endl;
        	return 0;
    	}

	portNo = atoi(argv[1]);	

	//only allows user setting up server to enter a certain range of port number

	if((portNo>65535) || (portNo<2000))
	{
		cerr<<"Please enter port number between 2000 - 65535"<<endl;
		return 0;
	}

    	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	//error msg if socket cannot be opened
	if (listenfd < 0)
	{
		cout<<"Cannot open socket!"<<endl;
		return 0;
	}

    	bzero((char *) &servaddr,sizeof(servaddr));
	servaddr.sin_family = AF_INET;
    	servaddr.sin_addr.s_addr = INADDR_ANY;
    	servaddr.sin_port = htons(portNo);

	//error msg if hostname and ip address cannot be binded

    	if(bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
	{
		cout<<"Cannot bind!"<<endl;
		return 0;
	}     	listen(listenfd,5);

    	len = sizeof(client_addr);

	//endless while loop for server to keep listening for incoming connections

    	while (loop==false)
    	{
		cout<<"Listening....."<<endl;	

        	connfd = accept(listenfd,(struct sockaddr *)&client_addr,&len);

		//error msg if server cannot accept incoming connection		

		if (connfd < 0)
		{
			cout<<"Cannot accept connection!"<<endl;
			return 0;
		}	

		//everytime a connection is made, the parent fork out a copy
		//this allows multiple connections to the server

        	pid = fork();
        	if (pid < 0)
        	{
            		cerr << "ERROR forking!" << endl;;
            		return 0;
        	}

        	if (pid == 0)
        	{
			close(listenfd);
            		clientside(connfd);
			return 0;
       		}
        	else 
		{
			close(connfd);
		}
    	}
    	return 0;		
}

//function to handle the file and comparision

int clientside (int connfd)
{
	bool questFound = false;
    	char test[150];
	char question[150];
	char answer[150];
	char exitCheck[] = "exit";
	char temp[150];

    	bzero(test,151);
    	read(connfd,test,150);

	//open the question and answer file
	ifstream infile;
	infile.open("questions.txt");
	strcpy(temp,test);

	//case insensitive for 'exit'

	for(int i=0;i<strlen(temp);i++)
	{
		temp[i] = tolower(temp[i]);
	}

	//if client enters 'exit', server will be notified

	if(strcmp(temp,exitCheck)==0)
	{
		cout<<"Client has disconnected!"<<endl;
		cout<<"Listening....."<<endl;
	}

	else
	{
		//server displays msg from client

		cout << "Message Recieved: " << test << endl;

	    	if (!infile)
	    	{
			cout << "Cannot open file" << endl;
			return 0;
	    	}
	    	else
	    	{

			//extracts data from file and compare

			while (!infile.getline(question,150,';').eof() && questFound == false)

			{
		    		infile.getline(answer,150);

				//msg from client is case insensitive

				for(int i=0;i<strlen(question);i++)
				{
					question[i] = tolower(question[i]);
				}

				for(int i=0;i<strlen(test);i++)
				{
					test[i] = tolower(test[i]);
				}

				//comparing input from client and data from file

		    		if (strcmp(test,question)==0)
		    		{
					//if got match, write to client
		        		questFound = true;
					write(connfd,answer,50);
		    		}
			}

			//if question not found, display this

		 	if (questFound == false)
	   	 	{
	   		 	write(connfd,"Sorry I do not have the answer to your msg",999);

			}			

			//close file
			infile.close();
	  	}
	}

}


client file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <string.h>

#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])

{
	int listenfd, portNo;
	bool loop = false;
	struct sockaddr_in servaddr;
	struct hostent *server;
	char exitCheck[] = "exit";
	char test[150];

	//endless loop for questions
	while(loop==false)
	{
		//make sure the user connecting to the server enters in correct arguments

		if(argc < 3)
		{
			cerr<<"Syntax : ./client <host name> <port>"<<endl;
			return 0;
		}

		portNo = atoi(argv[2]);

		//only allows user setting up server to enter a certain range of port number

		if((portNo>65535) || (portNo<2000))
		{
			cerr<<"Please enter port number between 2000 - 65535"<<endl;
			return 0;
		}		

		listenfd = socket(AF_INET, SOCK_STREAM, 0);

		//error msg if socket cannot be opened

		if (listenfd < 0)
		{
			cout<<"Cannot open socket!"<<endl;
			return 0;
		}

		server = gethostbyname(argv[1]);

		//if server does not exist, display error msg

		if(server==NULL)
		{
			cout<<"Host does not exist!"<<endl;
			return 0;
		}	

		bzero((char *) &servaddr, sizeof(servaddr));
		servaddr.sin_family = AF_INET;

		bcopy((char *)server->h_addr,(char *) &servaddr.sin_addr.s_addr,server->h_length);

		servaddr.sin_port = htons(portNo);

		int checker = connect(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

		//if unable to connect to server, display error msg

		if (checker < 0)
		{
			cout<<"Cannot connect!"<<endl;
			return 0;
		}

		cout<<"Please enter a question : "<<endl;

		bzero(test,151);
		cin.getline(test,150);

		string tester (test);

		//if user enters nothing, prompt user to enter a question

		if (tester.length()==0)
		{
			cout<<"No question asked!"<<endl;
		}
		else
		{
			//write input to server
			int checker2 = write(listenfd,test,strlen(test));

			//error msg if cannot write to socket

			if (checker2 < 0)
			{
				cout<<"Cannot write to socket!"<<endl;
				return 0;
			}

			//input case insensitive
			for(int i=0;i<strlen(test);i++)
			{
				test[i] = tolower(test[i]);
			}

			//check if user enters 'exit', if yes, exit loop and close socket

			if(strcmp(test,exitCheck)==0)
			{
				loop = true;
			}
			else
			{
				bzero(test,151);
				int checker3 = read(listenfd,test,150);

				//error if cannot read from socket

				if (checker3 < 0)
				{
					cout<<"Cannot read from socket!"<<endl;
					return 0;
				}	

				//display msg from server

				cout<<test<<endl;
			}	
		}
	}

	//close connection if user types exit

	close(listenfd);
	return 0;
}

	
Last edited on
needed some divine guidance...
Topic archived. No new replies allowed.