i cant get my code to work! (hawknl)

This is a discussion on i cant get my code to work! (hawknl) within the Networking/Device Communication forums, part of the General Programming Boards category; this wont work no matter how much i think i fix. i dont know wheather its the server or client ...

  1. #1
    Registered User Guido's Avatar
    Join Date
    Apr 2004
    Posts
    14

    i cant get my code to work! (hawknl)

    this wont work no matter how much i think i fix. i dont know wheather its the server or client part but i thinks its both

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <nl.h>
    #include <iostream.h>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    	//################VARS###################
        int			con; //listen or connect?
    	int			test=0; // send this variable?
    	//##############SOCKETS##################
    	NLsocket	clientsoc;
    	NLsocket	serversoc;
    	NLsocket	newclientsoc;
    	NLsocket	newserversoc;
    	//##############IP ADDRESS###############
    	NLbyte      server[] = "127.0.0.1";
    	NLbyte      client[] = "127.0.0.1:2500";
    	NLushort	serverport = 2500;
    	NLaddress	serveraddr; // host address
    	NLaddress	clientaddr; // host address
    	//#############BUFFERS###################
    	NLbyte		serverbuffer[32];
    	NLbyte		clientbuffer[32];
    	
    
    void init()
    {
    	//INIT CODE//
    	nlInit(); // like allegro init stuff
    	NLenum		type = NL_RELIABLE;
    	nlSelectNetwork(NL_RELIABLE);
    }
    
    int serverproc()
    {
    	// OPEN SERVERSOC
    	serversoc=nlOpen(0, NL_RELIABLE);
    	if(serversoc == NL_INVALID_ENUM)
    	{
    		printf("Quit Due To Invalid Socket\n");
    		return 1;
    	}
    	
    	if(nlListen(serversoc)==NL_INVALID_SOCKET) // LISTEN ON SERVERSOC
    	{
    		nlClose(serversoc);
    		printf("Quit Because Server Could Not Listen\n");
    		return 1;
    	}
    	while(1)	
    	{
    		newserversoc = nlAcceptConnection(serversoc);//when someone connects accept it
    		//ATTEMPT TO LISTEN ON THE SERVERSOC IF YOU CAN'T THEN QUIT
    		
    		if (newserversoc != NL_INVALID)
    		{
    			printf("Connection Established \n");
    			nlGetRemoteAddr(newserversoc, &clientaddr); // ask for the ip of connector
    			nlAddrToString(&clientaddr, client); //convert address to string (not needed)
    			printf(client,"\n ");
    		}
    			
    		if(nlRead(newserversoc, serverbuffer, sizeof(serverbuffer)) > 0)
            {
                serverbuffer[32] = 0; //null terminate the char string 
                nlRead(newserversoc, serverbuffer,strlen(serverbuffer));
    			printf(serverbuffer);
               }
    	}
    }
    
    int clientproc()
    {
    		//START CONNECTING
    		cin>>server; //get ip address
    		clientsoc=nlOpen(serverport, NL_RELIABLE);
    		
    		if(clientsoc == NL_INVALID)
            {
                printf("CLIENT: nlOpen error\n");
                return 0;
            }
    		nlStringToAddr(server, &serveraddr);
            nlSetAddrPort(&serveraddr, serverport);	
    		printf("Now Connecting to: ");
    
    		if(!nlConnect(clientsoc, &serveraddr))
    			{
    	            printf("CLIENT: nlConnect error\n");
    				return 0;
    		    }
    		while (1)
    		{
    			memset(clientbuffer, 0, sizeof(clientbuffer));
    			test++;
    			clientbuffer[32]=test;
    			nlWrite(clientsoc, clientbuffer, strlen(clientbuffer)+1);
    		}
    
    }
    
    int main()
    {
    	init();
    	//START PROGRAM
    	cout<<"Listen(1) Or Connect(2)?";
    	cin>>con;
    
    	// LISTENING (SERVER)
    	if (con == 1 ){
    		serverproc();
    	}
    	//CONNECTING (CLIENT)
    	if (con == 2)
    	{
    		clientproc();
    	}else{
    	//exit program
    	}
    nlShutdown(); //closes hawk library (allegro does this automatically)
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    1. Don't mix tabs and spaces for indenting your code. Your editor can cope, but the board cannot.
    Your otherwise nicely formatted code now looks a mess when posted on the board.

    2. if(nlRead(newserversoc, serverbuffer, sizeof(serverbuffer)) > 0)
    You need to store the value returned, it is the number of chars written to your buffer

    3. serverbuffer[32] = 0;
    This is off the end of the array
    array[N] has indices 0 to N-1

    4. printf(serverbuffer);
    NEVER pass a raw string as the first parameter to printf(). If someone types a %, you're sunk big-time
    Use printf("%s",serverbuffer);
    or puts( serverbuffer );

    5. Why is there an extra nlRead in there?


    In fact, these few lines should be
    Code:
    if( (n=nlRead(newserversoc, serverbuffer, sizeof(serverbuffer)-1)) > 0)
    {
        serverbuffer[n] = '\0';
        fputs(serverbuffer,stdout);
    }
    6. clientbuffer[32]=test;
    nlWrite(clientsoc, clientbuffer, strlen(clientbuffer)+1);
    More out of bound memory access
    Think - you just cleared the buffer with memset, what value will strlen() always return ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Guido's Avatar
    Join Date
    Apr 2004
    Posts
    14
    okay i think i fixed it up, but im still getting issues. the client won't connect and if it says it has connected the server isn't hearing anything.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <nl.h>
    #include <iostream.h>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    	//################VARS###################
        int			con; //listen or connect?
    	int			test=0; // send this variable?
    	int			n; // number of characters sent with thru write
    	//##############SOCKETS##################
    	NLsocket	clientsoc;
    	NLsocket	serversoc;
    	NLsocket	newclientsoc;
    	NLsocket	newserversoc;
    	//##############IP ADDRESS###############
    	NLbyte      server[] = "24.150.89.91";
    	NLbyte      client[] = "24.150.89.91";
    	NLushort	serverport = 250;
    	NLaddress	serveraddr; // host address
    	NLaddress	clientaddr; // host address
    	//#############BUFFERS###################
    	NLbyte		serverbuffer[32];
    	NLbyte		clientbuffer[32];
    	NLenum		type = NL_RELIABLE;
    
    void init()
    {
    	//INIT CODE//
    	nlInit(); // like allegro init stuff
    	nlSelectNetwork(NL_IP);
    }
    
    int serverproc()
    {
    	// OPEN SERVERSOC
    	serversoc=nlOpen(0, NL_RELIABLE);
    	
    	if(serversoc == NL_INVALID)
    	{
    		printf("Quit Due To Invalid Socket\n");
    		return 1;
    	}
    	
    	if(nlListen(serversoc)==NL_INVALID) // LISTEN ON SERVERSOC
    	{
    		nlClose(serversoc);
    		printf("Quit Because Server Could Not Listen\n");
    		return 1;
    	}
    	while(1)	
    	{
    		newserversoc = nlAcceptConnection(serversoc);//when someone connects accept it
    		//ATTEMPT TO LISTEN ON THE SERVERSOC IF YOU CAN'T THEN QUIT
    		if (nlAcceptConnection(serversoc) == NL_INVALID)
    		{
    			NLint err = nlGetError();
                if( err == NL_SYSTEM_ERROR || err == NL_NOT_LISTEN)
                {
                    printf("\n\nServer shutdown!!!!!!!!!!!!!\n");
    				return 0;
                }
            }else{
                printf("Connection Established \n");
    			nlGetRemoteAddr(newserversoc, &clientaddr); // ask for the ip of connector
    			nlAddrToString(&clientaddr, client); //convert address to string (not needed)
    			printf(client,"\n ");
            }
    			
    		if((n=nlRead(newserversoc, serverbuffer, sizeof(serverbuffer)-1)) > 0)
    		{
    			serverbuffer[n] = '\0';
    			fputs(serverbuffer,stdout);
    			printf("recieved\n");
    		}else{
    			printf("nothing to read\n");
    		}
    	}
    }
    
    int clientproc()
    {
    		//START CONNECTING
    		//cin>>server;
    		clientsoc=nlOpen(0, NL_RELIABLE);
    		nlStringToAddr(server, &serveraddr);
            //nlSetAddrPort(&serveraddr, serverport);	
    		printf("Now Connecting to: \n");
    		printf(server);
    		if(nlConnect(clientsoc, &serveraddr)==NL_FALSE)
    		{
    			printf("\nCLIENT: nlConnect error\n");
    			return 0;
    		}else if (nlConnect(clientsoc, &serveraddr)==NL_TRUE){
    			while(1)
    			{
    				memset(clientbuffer, 0, sizeof(clientbuffer));
    				test++;
    				memset(clientbuffer, test, sizeof(test));
    				nlWrite(clientsoc, clientbuffer, strlen(clientbuffer)+1);
    				printf("sent data..");
    				printf(server);
    				cout<<test;
    				printf("\n");
    			}
    		}
    		return 0;
    }
    
    int main()
    {
    	init();
    	//START PROGRAM
    	cout<<"Listen(1) Or Connect(2)?";
    	cin>>con;
    
    	// LISTENING (SERVER)
    	if (con == 1 ){
    		serverproc();
    	}
    	//CONNECTING (CLIENT)
    	if (con == 2)
    	{
    		clientproc();
    	}else{
    	//exit program
    	}
    nlShutdown(); //closes hawk library (allegro does this automatically)
    return 0;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > memset(clientbuffer, 0, sizeof(clientbuffer));
    > test++;
    > memset(clientbuffer, test, sizeof(test));
    How about storing a printable version of your integer

    sprintf( clientbuffer, "%d", test ); test++;

    > printf(server);
    Told you before about this

    Another point, get rid of all those global variables.
    Put the appropriate local variables inside each 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. this code COMPILES, but DOESNT WORK
    By Leeman_s in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 05:54 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21