Thread: Queries on Server/Client Battleship Program

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    Question Queries on Server/Client Battleship Program

    Hi all, I'm new to client/server TCP socket programming. Just want to get some help to run this program...
    Code:
    #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 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);//reading client's attacked coordinate
    			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 warship[150];
    	char shipgrid[150];
    	char exitCheck[] = "exit";
    	char temp[150];
    	string fline;
    	string::size_type pos;
    
    
        	bzero(test,151);
        	read(connfd,test,150);// client's attacked coordinate  stored in test
    	//open the warship and shipgrid file
    	ifstream infile;
    	infile.open("position.txt");
    	strcpy(temp,test);
    
    
    	//case insensitive for 'exit'
    	for(int i=0;i<strlen(temp);i++)
    	{
    		temp[i] = toupper(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 - showing client attacked co-ordinate in variable test
    		cout << "Message Received: " << test << endl;
    
    
    		for(int i=0;i<strlen(test);i++)
    		{
    			test[i] = toupper(test[i]);
    		}
    
    
    	    	if (!infile)
    	    	{
    			cout << "Cannot open file" << endl;
    			return 0;
    	    	}
    	    	else
    	    	{
    			//extracts data from file and compare
    			while (getline(infile, fline) && questFound == false)
    			{
    		    		//cout<<fline<<endl;
    					pos = fline.find(test, 0);
    
    
    				      // ??? Need help here..
    	
    			}
    			//if warship not found, display this
    		 	if (questFound == false)
    	   	 	{
    	   		 	write(connfd,"Missed",999);
    	   	
    			}			
    			//close file
    			infile.close();}}}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > bzero(test,151);
    Don't lie about your buffer sizes.

    > read(connfd,test,150);// client's attacked coordinate stored in test
    Pay attention to the return result.
    You opened a SOCK_STREAM, which means it's perfectly valid to get your data 1 byte at a time. You might get more, but you have no control over that.
    Making sure you have a valid message (say you saw a \n) is up to you.

    Is this function designed to handle just one message, or a number of messages in a loop until it sees exit?

    > if(strcmp(temp,exitCheck)==0)
    You converted everything to uppercase, and then you compare it with a lowercase string.

    > if (!infile)
    Consider doing this right after opening the file, and not in the middle of unrelated code.

    Consider moving the code which searches a file into a separate function. It will help with the clarity of purpose of this function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with UDP Server/Client Program
    By handsomedan in forum C Programming
    Replies: 5
    Last Post: 03-24-2010, 09:18 AM
  2. Server and client in the same program
    By smithx in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-25-2008, 09:30 PM
  3. c program client server
    By steve1_rm in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-24-2008, 10:33 AM
  4. Client-server program
    By erictran in forum C Programming
    Replies: 3
    Last Post: 09-18-2006, 07:59 PM
  5. Server client program help please.
    By XiReDDeViLiX in forum C++ Programming
    Replies: 15
    Last Post: 04-03-2002, 09:21 PM

Tags for this Thread