Thread: What the shoot is wrong?

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    What the shoot is wrong?

    relik world.c: In function ‘main’:
    relik world.c:71: error: invalid lvalue in assignment
    relik world.c:72: error: expected ‘;’ before ‘{’ token
    relik world.c: At top level:
    relik world.c:134: error: expected ‘)’ before ‘simpleChildSocket’

    line 71 and 72 are
    the white(1=1) loop and the line just above it.

    number 134 us the line just above menu_select

    Can anyone tell me whats wrong here. Im gonna keep looking but gcc on this computer isn't perect either. So it could be just fine code.
    Code:
    #include <stdio.h>  /* sleep(350)*/
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include "relik server.h"
    
    //Syntax:
    //int socket(int af, int type, int protocol);
    
    //C source:
    int main(int argc, char **argv)
    {
    	int pos = 1, port = 2000;	
    	while ((pos < argc) && (*(argv[pos]) == '-')) {
    		switch (*(argv[pos] + 1)) 
    		{
    			case 'f': break;
    			case 'o': break;
    			case 'd': break;
    			case 'm': break;
    			case 'c': break;
    			case 'q': break;
    			case 'r': break;
    			case 's': break;
    			case 'h': break;
    			default: break;
    		}
    		pos++;
    	}
    
    	if (pos < argc) {
    		if (!isdigit(*argv[pos])) {
    			printf("Usage: %s [-c] [-m] [-q] [-r] [-s] [-d pathname] [port #]\n", argv[0]);
    			exit(EXIT_FAILURE);
    		}
    		else if ((port = atoi(argv[pos])) <= 1024) {
    			printf("SYSERR: Illegal port number %d.\n", port);
    			exit(EXIT_FAILURE);
    		}
    	}
    	
    	struct sockaddr_in address;		
    	int mastersocket;
    	fd_set descrip;
    	int activity, user;
    	int comment;	
    	int client_socket[700];
    	int maxclients = 700;
    	char buffer[1000];
    	
    	if (mastersocket=socket(AF_INET,SOCK_STREAM,0)==-1)
    		fatal("Creating mastersocket, AF_INET SOCK_STREAM failed");
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = INADDR_ANY;
    	address.sin_port = htons(port);                                        /* port is the port to use for connections */
    	/* Binding to mastersocket */
    	if(bind(mastersocket,(struct sockaddr *)&address,sizeof(address))==-1) /* bind the socket to the port specified above */
    		fatal("Binding mastersocket, AF_INET SOCK_STREAM failed");
    	/* LIstening to mastersocket */
        	if(listen(mastersocket, 3) == -1) 
    		fatal("Listening to mastersocket, AF_INET SOCK_STREAM failed");
    	
    	fcntl(mastersocket, F_SETFL, FNDELAY);
    
    
    
    	While(1=1)                                                                                              //
    	{                                                                                                       //
    /****************************************************************************************************************/
    		FD_ZERO(&descrip);                 /* create a list of sockets to check for activity            */
    		FD_SET(mastersocket, &descrip);    /* specify mastersocket - ie listen for new connections      */
                                                              
    		for (user=0; user<max_clients; user++) {                 !!!!!
    			if (client_socket[user] > 0)                     !!!!!
    				FD_SET(client_socket[user], &descrip);   !!!!!
    		}
                                                              
    		if ((activity=select(max_conns, &descrip, NULL, NULL, NULL)<0) && (errno!=EINTR))/* wait for a connection*/
    			fatal("Selecting a file descriptor");
    		if (FD_ISSET(mastersocket, &descrip)) {
    			/* Open the new socket */
    			/* Open the new socket as 'new_socket' */
    			int time, new_socket, addrlen=sizeof(address);
    			if ((new_socket = accept(mastersocket, (struct sockaddr *)&address, &addrlen))<0)
    				fatal("Accept failed to return a socket descriptor");
    			/* inform user of socket number - used in send and receive commands */
    			printf("New socket is fd %d\n", new_socket);
    			/* transmit message to new connection */
    			for (int time = 0; time = 5; time++){
    				char *message =".";
    				if (send(new_socket, message, strlen(message), 0) != strlen(message)) {
    					/* if send failed to send all the message, display error */
    					nonfatal("sending message to a new socket.");
    				}
    				sleep(1);
    			}
    			puts("Welcome message sent successfully");
    			/* add new socket to list of sockets */
    			for (user=0; user<max_clients; user++) {
    				if (client_socket[user] == 0) {
    					client_socket[user] = new_socket;
    					printf("Adding to list of sockets as %d\n", user);
    					user = max_clients;
    				}
    			}
    			for (user=0; user<max_clients; user++) {
    				if (FD_ISSET(client_socket[user], &readfds)) {
    					if ((read(client_socket[user], buffer, 1024)) < 0) {
    						close(client_socket[user]);
    						client_socket[user] = 0;
    					}
    					else 
    					{
    						/* set the terminating NULL byte on the end of the data read */
    						buffer[1000] = 0;
    						for (comment=0; comment<max_clients; comment++) {
    							/* note, flags for send() are normally 0, see man page for details */
    							send(client_socket[loop2], buffer, strlen(buffer), 0);
    						}
    					}
    				}
    			}
    		}
    	}
    
    	if (close(mastersocket))exit(EXIT_SUCCESS);
    }
    
    
    int menu_select(fdset simpleChildSocket)
    {
    	char buffer[1024];
    	int recv_length=0;
    
    	//system("clear");
    	//snprintf(ch str, sizeof(str), "", ...) copies the first sizeof() bytes from "" to
    	// ch str, also accepting the same variables as printf for formating the string.
    	send(simpleChildSocket, "--------------       A 20th century 2 demensional multi      ^       \r\n   ", 70, 0);
    	send(simpleChildSocket, "\\________      \\     user dungeon game not just for the    /   \\     \r\n", 70, 0);
    	send(simpleChildSocket, " |             /     average role playing adventurer. A    | | |     \r\n   ", 70, 0);
    	send(simpleChildSocket, " |             \\     sensation of its own. Created by,     \\ / /     \r\n ", 70, 0);
    	send(simpleChildSocket, " |      |       \\                     Eric Justin Allan    / / \\     \r\n ", 70, 0);
    	send(simpleChildSocket, " |______|_____  /                                          | | |     \r\n   ", 70, 0);
    	send(simpleChildSocket, "              \\/                                           \\   /     \r\n ", 70, 0);
    	send(simpleChildSocket, "  ^                                                          v       \r\n   ", 70, 0);
    	send(simpleChildSocket, "/   \\ \r\n", 7, 0);
    	send(simpleChildSocket, "| | | \r\n", 7, 0);
    	send(simpleChildSocket, "\\/  / \r\n", 7, 0);
    	send(simpleChildSocket, "/ / \\ \r\n", 7, 0);
    	send(simpleChildSocket, "| | | \r\n", 7, 0);
    	send(simpleChildSocket, "\\   / \r\n", 7, 0);
    	send(simpleChildSocket, "  v   \r\n", 7, 0);
    	send(simpleChildSocket, "                                         Read Our Policy \"Rules\"\r\n", 65, 0);
    	send(simpleChildSocket, "                                         1. New game\r\n", 53, 0);
    	send(simpleChildSocket, "                                         2. Continue\r\n", 53, 0);
    	send(simpleChildSocket, "                                         #", 42, 0);
    
    	recv(simpleChildSocket, &buffer, 1024, 0);
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    "1=1" is illegal. I think you mean "1==1". Although that is equal to "1".

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You got big stones calling out gcc! The problem on lines 72 and 73 are because you put

    Code:
    While(1=1)
    "Invalid lvalue" means invalid left-hand-side value in assignment. You used only one = sign, which is assignment in C, but you can't assign a value to a literal (the left hand side is a literal 1). There is no storage space in memory associated with the left had side of that assignment, hence no place to put the value from the right hand side.

    The "expected ‘;’ before ‘{’ token" is because you capitalized while. C is case sensitive, so it thinks While(1=1) is a function call, which can't have a '{' after it. You would need a ; or some other arithmetic/logical operator. Use an editor with syntax highlighting if you can. The 'while' keyword will show up differently than your function calls, and it will help you notice these hard-to-see mistakes, for which gcc doesn't give a "are you sure you want the word 'while' capitalized" error.

    As for the error on line 134, I'm not sure as of yet. I'm guessing it's because it doesn't know what the fdset type is. Fixing the errors on lines 72 and 73 gave me a whole slew of other errors. Resolve those and provide needed header files (relik server.h and maybe others) for me to properly compile your program, and I might be able to help you track down the last error.
    Last edited by anduril462; 11-09-2010 at 01:18 PM. Reason: lvalue is "left value" as in the left-hand-side of the assignment

  4. #4
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    You right it fixed. I just got one problem left.

    Ill probably be bale to find the problem but if you could find it or want to
    heres the error message i got.

    relik world.c:136: error: expected ‘)’ before ‘simpleChildSocket’

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    int menu_select(fdset simpleChildSocket)
    Can I assume fdset is the type and simpleChildSocket is the parameter name? If so, then your error is because the compiler doesn't know what fdset is, meaning you are likely missing the header file that includes the "typedef ... fdset" command. This confuses gcc and it assumes that fdset is a parameter name as well, and that you are going for old school function declaration format (Old-style Function Declarations), but you don't have a comma between the two parameter names and you never declare their types.

  6. #6
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    I fixed all the errors accept the one above.

    relik world.c:136: error: expected ‘)’ before ‘simpleChildSocket’


    Code:
    #include <stdio.h>  /* sleep(350)*/
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include "relik server.h"
    
    //Syntax:
    //int socket(int af, int type, int protocol);
    
    //C source:
    int main(int argc, char **argv)
    {
    	int pos = 1, port = 2000;	
    	while ((pos < argc) && (*(argv[pos]) == '-')) {
    		switch (*(argv[pos] + 1)) 
    		{
    			case 'f': break;
    			case 'o': break;
    			case 'd': break;
    			case 'm': break;
    			case 'c': break;
    			case 'q': break;
    			case 'r': break;
    			case 's': break;
    			case 'h': break;
    			default: break;
    		}
    		pos++;
    	}
    
    	if (pos < argc) {
    		if (!isdigit(*argv[pos])) {
    			printf("Usage: %s [-c] [-m] [-q] [-r] [-s] [-d pathname] [port #]\n", argv[0]);
    			exit(EXIT_FAILURE);
    		}
    		else if ((port = atoi(argv[pos])) <= 1024) {
    			printf("SYSERR: Illegal port number %d.\n", port);
    			exit(EXIT_FAILURE);
    		}
    	}
    	
    	struct sockaddr_in address;		
    	int mastersocket, nullbyte;
    	fd_set   descrip;
    	int time, new_socket, activity, user;
    	int comment, addrlen=sizeof(address);
    	int client_socket[700];
    	int maxclients =   700;
    	char      buffer[4000];
    
    
    	if (mastersocket=socket(AF_INET,SOCK_STREAM,0)==-1)
    		fatal("Creating mastersocket, AF_INET SOCK_STREAM failed");
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = INADDR_ANY;
    	address.sin_port = htons(port);                                        /* port is the port to use for connections */
    	/* Binding to mastersocket */
    	if(bind(mastersocket,(struct sockaddr *)&address,sizeof(address))==-1) /* bind the socket to the port specified above */
    		fatal("Binding mastersocket, AF_INET SOCK_STREAM failed");
    	/* LIstening to mastersocket */
        	if(listen(mastersocket, 3) == -1) 
    		fatal("Listening to mastersocket, AF_INET SOCK_STREAM failed");
    	
    	fcntl(mastersocket, F_SETFL, FNDELAY);
    
    
    
    	while(1==1)                                                                                              //
    	{                                                                                                       //
    /****************************************************************************************************************/
    		FD_ZERO(&descrip);                 /* create a list of sockets to check for activity            */
    		FD_SET(mastersocket, &descrip);    /* specify mastersocket - ie listen for new connections      */
                                                              
    		for (user=0; user<maxclients; user++) {                  //!!!!!
    			if (client_socket[user] > 0)                     //!!!!!
    				FD_SET(client_socket[user], &descrip);   //!!!!!
    		}
                                                              
    		if ((activity=select(maxclients, &descrip, NULL, NULL, NULL)<0) && (errno!=EINTR))/* wait for a connection*/
    			fatal("Selecting a file descriptor");
    		if (FD_ISSET(mastersocket, &descrip)) {
    			/* Open the new socket */
    			/* Open the new socket as 'new_socket' */
    			if ((new_socket = accept(mastersocket, (struct sockaddr *)&address, &addrlen))<0)
    				fatal("Accept failed to return a socket descriptor");
    			/* inform user of socket number - used in send and receive commands */
    			printf("New socket is fd %d\n", new_socket);
    			/* transmit message to new connection */
    			for (time = 0; time = 5; time++){
    				char *message =".";
    				if (send(new_socket, message, strlen(message), 0) != strlen(message)) {
    					/* if send failed to send all the message, display error */
    					nonfatal("sending message to a new socket.");
    				}
    				sleep(1);
    			}
    			puts("Welcome message sent successfully");
    			/* add new socket to list of sockets */
    			for (user=0; user<maxclients; user++) {
    				if (client_socket[user] == 0) {
    					client_socket[user] = new_socket;
    					printf("Adding to list of sockets as %d\n", user);
    					user = maxclients;
    				}
    			}
    			for (user=0; user<maxclients; user++) {
    				if (FD_ISSET(client_socket[user], &descrip)) {
    					if ((read(client_socket[user], buffer, 1024)) < 0) {
    						close(client_socket[user]);
    						client_socket[user] = 0;
    					}
    					else 
    					{
    						/* set the terminating NULL byte on the end of the data read */
    						buffer[nullbyte] = 0;
    						for (comment=0; comment<maxclients; comment++) {
    							/* note, flags for send() are normally 0, see man page for details */
    							send(client_socket[comment], buffer, strlen(buffer), 0);
    						}
    					}
    				}
    			}
    		}
    	}
    
    	if (close(mastersocket))exit(EXIT_SUCCESS);
    }
    
    
    int menu(fdset simpleChildSocket[])
    {
    	//system("clear");
    	//snprintf(ch str, sizeof(str), "", ...) copies the first sizeof() bytes from "" to
    	// ch str, also accepting the same variables as printf for formating the string.
    	send(simpleChildSocket, "--------------       A 20th century 2 demensional multi      ^       \r\n   ", 70, 0);
    	send(simpleChildSocket, "\\________      \\     user dungeon game not just for the    /   \\     \r\n", 70, 0);
    	send(simpleChildSocket, " |             /     average role playing adventurer. A    | | |     \r\n   ", 70, 0);
    	send(simpleChildSocket, " |             \\     sensation of its own. Created by,     \\ / /     \r\n ", 70, 0);
    	send(simpleChildSocket, " |      |       \\                     Eric Justin Allan    / / \\     \r\n ", 70, 0);
    	send(simpleChildSocket, " |______|_____  /                                          | | |     \r\n   ", 70, 0);
    	send(simpleChildSocket, "              \\/                                           \\   /     \r\n ", 70, 0);
    	send(simpleChildSocket, "  ^                                                          v       \r\n   ", 70, 0);
    	send(simpleChildSocket, "/   \\ \r\n", 7, 0);
    	send(simpleChildSocket, "| | | \r\n", 7, 0);
    	send(simpleChildSocket, "\\/  / \r\n", 7, 0);
    	send(simpleChildSocket, "/ / \\ \r\n", 7, 0);
    	send(simpleChildSocket, "| | | \r\n", 7, 0);
    	send(simpleChildSocket, "\\   / \r\n", 7, 0);
    	send(simpleChildSocket, "  v   \r\n", 7, 0);
    	send(simpleChildSocket, "                                         Read Our Policy \"Rules\"\r\n", 65, 0);
    	send(simpleChildSocket, "                                         1. New game\r\n", 53, 0);
    	send(simpleChildSocket, "                                         2. Continue\r\n", 53, 0);
    	send(simpleChildSocket, "                                         #", 42, 0);
    }

  7. #7
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Wow Im an idiot. I meant to define it as fd_set and i didnt i used fdset. im gonna compile this thing.
    Last edited by errigour; 11-09-2010 at 01:40 PM.

  8. #8
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Is this ok?

    Code:
    int menu(int client_socket[])
    {
    	int simpleChildSocket = (int)client_socket;
    	//snprintf(ch str, sizeof(str), "", ...) copies the first sizeof() bytes from "" to
    	// ch str, also accepting the same variables as printf for formating the string.
    	send(simpleChildSocket, "--------------       A 20th century 2 demensional multi      ^       \r\n   ", 70, 0);

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Why would you cast a int array to a int?

    What are you trying to do?
    Woop?

  10. #10
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I was thinking about using a *client_socket for the parimiters but then
    there might be a problem when I use client_socket[user] for
    simpleChildSocket

  11. #11
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Im trying to send client_socket[user]/one of my clients, the menu, but send doesn't accept anything but intigers so I wanted to make send accept the array/descriptor as a destination, Im wondering if this will work because gcc gives me no errors on this one and i think that the client_socket[user] booleaned as an intiger will be an acceptable destination and paramiter for send.

    The question I was asking is will send, send the message to the client_socket[user].

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That will not work. The value of (int)client_socket is the address stored in client_socket (it's effectively an int pointer when used as a function parameter, regardless of the []), not the first element of client_socket. I suggest you just pass in the socked descriptor instead of a whole array. You don't appear to be using multiple elements out of the array. Try the following:
    Code:
    int menu(int child_socket)   // notice the lack of * and []
    If you care for an explanation of * vs [], read on:

    As function parameters int foo[] and int *foo are effectively both integer pointers and thus foo can be used the same way within the function regardless of how you declare it. Without brackets, they refer to the same value, the address passed into the function (presumably the name of an integer array from the caller's scope). With brackets, they still refer to the same data, and foo[3] would represent the 4th element in an array, as you would expect.

    The difference between the two is when you define a variable and give it an initial value.
    Code:
    int foo[];                             // compiler doesn't know how big this should be.  gcc assumes 1 element, may vary on different systems/compilers
    int bar[] = {1, 2, 3, 4, 5};  // 5 bytes are allocated in the appropriate section (data/stack) and filled with the values
    int *baz = bar;                   // one word is allocated, and the address of bar is stored in baz
    Last edited by anduril462; 11-09-2010 at 02:14 PM. Reason: Hit 'Post Quick Reply' WAY too early

  13. #13
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I dont understand what your trying to say?

  14. #14
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    will send(smpleChildSocket, "message", 7);
    send that message to
    client_socket[7]
    if
    simpleChildSocket=client_socket[7];?

  15. #15
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    cats like ice water.
    I almost always have ice water by my side and
    this cat of my sisters drinks the water almost
    everytime he notices that I have a glass with ice water.
    Last edited by errigour; 11-09-2010 at 02:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM