Receiving a syntax error

This is a discussion on Receiving a syntax error within the C Programming forums, part of the General Programming Boards category; Hello, the error I am receiving is below: I think I am missing a "{" somewhere but I can't find ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    23

    Receiving a syntax error

    Hello, the error I am receiving is below:

    I think I am missing a "{" somewhere but I can't find out where or I can be totally wrong. Does anybody have any suggestions? Thanks!

    Code:
    lab8a.c: In function ‘main’:
    lab8a.c:154: error: expected declaration or statement at end of input
    Here is my code:
    Code:
    /*
    ** multi.c
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define PORT 9034 // port we're listening on
    
    int main(void)
    {
    	struct timeval {
    	int tv_sec;
    	int tv_usec;
    	};
    
    	fd_set master, // master file descriptor list
    	           read_fds; // temp file descriptor list for select()
    	struct sockaddr_in myaddr, // server address
    	                              remoteaddr; // client address
    	int fdmax, // maximum file descriptor number
                    listener, // listening socket descriptor
    	     newfd, // newly accept()ed socket descriptor
    	     nbytes,
    	     yes=1, // for setsockopt() SO_REUSEADDR, below
    	      i, j;
    
    	char buf[256]; // buffer for client data
    	socklen_t addrlen;
    	
    	struct timeval timeout;
    	int flag;
    
    	FD_ZERO(&master); // clear the master and temp sets
    	FD_ZERO(&read_fds);
    	
    	// get the listener
    	if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    		perror("socket");
    		exit(1);
    	}
    	
    	// lose the pesky "address already in use" error message
    	if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    		perror("setsockopt");
    		exit(1);
    	}
    	
    	// bind
    	myaddr.sin_family = AF_INET;
    	myaddr.sin_addr.s_addr = INADDR_ANY;
    	myaddr.sin_port = htons(PORT);
    	memset(&(myaddr.sin_zero), '\0', 8);
    	if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
    		perror("bind");
    		exit(1);
    	}
    	
    	// listen
    	if (listen(listener, 10) == -1) {
    		perror("listen");
    		exit(1);
    	}
    	
    	// add the listener to the master set
    	FD_SET(listener, &master);
    	
    	// keep track of the biggest file descriptor
    	fdmax = listener; // so far, it's this one
    	
    	// main loop
    	for(;;) {
    		timeout.tv_sec = 15;
    		timeout.tv_usec = 0;
    		flag = 0;
    		
    		read_fds = master; // copy it
    		if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
    			perror("select");
    			exit(1);
    		}
    		
    		// run through the existing connections looking for data to read
    		for(i = 0; i <= fdmax; i++) {
    			if (FD_ISSET(i, &read_fds)) { // we got one!!
    				if (i == listener) {
    					
    					// handle new connections
    					addrlen = sizeof(remoteaddr);
    					if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen)) == -1) {
    						perror("accept");
    					}
    					else {
    						FD_SET(newfd, &master); // add to master set
    						if (newfd > fdmax) { // keep track of the maximum
    							fdmax = newfd;
    						}
    						printf("selectserver: new connection from %s on "
    							"socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
    					}
    				}
    				else {
    					// handle data from a client
    					if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
    						// got error or connection closed by client
    						if (nbytes == 0) {
    							// connection closed
    							printf("selectserver: socket %d hung up\n", i);
    						} else {
    							perror("recv");
    						}
    						close(i); // bye!
    						FD_CLR(i, &master); // remove from master set
    					} else {
    						// we got some data from a client
    						for(j = 0; j <= fdmax; j++) {
    							// send to everyone!
    							if (FD_ISSET(j, &master)) {
    								// except the listener and ourselves
    								if (j != listener && j != i) {
    									if (send(j, buf, nbytes, 0) == -1) {
    										perror("send");
    									}
    								}
    							}
    						}
    					}
    				}
    				flag = 1; 
    			}
    		}
             if(!flag)
    		{
    			strcpy(buf, "Fifteen seconds have passed since the last message.\n");
    			nbytes = strlen(buf) + 1;
    			for(j = 0; j <= fdmax; j++) {
                                                            // send to everyone!
                                                            if (FD_ISSET(j, &master)) {
                                                                    // except the listener and ourselves
                                                                    if (j != listener && j != i) {
                                                                            if (send(j, buf, nbytes, 0) == -1) {
                                                                                    perror("send");
                                                                              }
    }
    }
    } 
    }
    return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Well, if you would just indent properly, you would see:
    Code:
    /*
    ** multi.c
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define PORT 9034 // port we're listening on
    
    int main(void)
    {
    	struct timeval
    	{
    		int tv_sec;
    		int tv_usec;
    	};
    
    	fd_set master, // master file descriptor list
    		read_fds; // temp file descriptor list for select()
    	struct sockaddr_in myaddr, // server address
    		remoteaddr; // client address
    	int fdmax, // maximum file descriptor number
    		listener, // listening socket descriptor
    		newfd, // newly accept()ed socket descriptor
    		nbytes,
    		yes=1, // for setsockopt() SO_REUSEADDR, below
    		i, j;
    
    	char buf[256]; // buffer for client data
    	socklen_t addrlen;
    
    	struct timeval timeout;
    	int flag;
    
    	FD_ZERO(&master); // clear the master and temp sets
    	FD_ZERO(&read_fds);
    
    	// get the listener
    	if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		perror("socket");
    		exit(1);
    	}
    
    	// lose the pesky "address already in use" error message
    	if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    	{
    		perror("setsockopt");
    		exit(1);
    	}
    
    	// bind
    	myaddr.sin_family = AF_INET;
    	myaddr.sin_addr.s_addr = INADDR_ANY;
    	myaddr.sin_port = htons(PORT);
    	memset(&(myaddr.sin_zero), '\0', 8);
    	if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1)
    	{
    		perror("bind");
    		exit(1);
    	}
    
    	// listen
    	if (listen(listener, 10) == -1)
    	{
    		perror("listen");
    		exit(1);
    	}
    
    	// add the listener to the master set
    	FD_SET(listener, &master);
    
    	// keep track of the biggest file descriptor
    	fdmax = listener; // so far, it's this one
    
    	// main loop
    	for(;;)
    	{// Might there be a missing } for this for?
    		timeout.tv_sec = 15;
    		timeout.tv_usec = 0;
    		flag = 0;
    
    		read_fds = master; // copy it
    		if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
    		{
    			perror("select");
    			exit(1);
    		}
    
    		// run through the existing connections looking for data to read
    		for(i = 0; i <= fdmax; i++)
    		{
    			if (FD_ISSET(i, &read_fds))
    			{ // we got one!!
    				if (i == listener)
    				{
    
    					// handle new connections
    					addrlen = sizeof(remoteaddr);
    					if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen)) == -1)
    					{
    						perror("accept");
    					}
    					else
    					{
    						FD_SET(newfd, &master); // add to master set
    						if (newfd > fdmax)
    						{ // keep track of the maximum
    							fdmax = newfd;
    						}
    						printf("selectserver: new connection from &#37;s on "
    							"socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
    					}
    				}
    				else
    				{
    					// handle data from a client
    					if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0)
    					{
    						// got error or connection closed by client
    						if (nbytes == 0) 
    						{
    							// connection closed
    							printf("selectserver: socket %d hung up\n", i);
    						}
    						else
    						{
    							perror("recv");
    						}
    						close(i); // bye!
    						FD_CLR(i, &master); // remove from master set
    					}
    					else
    					{
    						// we got some data from a client
    						for(j = 0; j <= fdmax; j++)
    						{
    							// send to everyone!
    							if (FD_ISSET(j, &master))
    							{
    								// except the listener and ourselves
    								if (j != listener && j != i)
    								{
    									if (send(j, buf, nbytes, 0) == -1)
    									{
    										perror("send");
    									}
    								}
    							}
    						}
    					}
    				}
    				flag = 1; 
    			}
    		}
    		if(!flag)
    		{
    			strcpy(buf, "Fifteen seconds have passed since the last message.\n");
    			nbytes = strlen(buf) + 1;
    			for(j = 0; j <= fdmax; j++) {
    				// send to everyone!
    				if (FD_ISSET(j, &master)) {
    					// except the listener and ourselves
    					if (j != listener && j != i) {
    						if (send(j, buf, nbytes, 0) == -1) {
    							perror("send");
    						}
    					}
    				}
    			} 
    		}
    		return 0;
    	}
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Thank you for that - I thought I was missing one down at the bottom around the line 150 but it is suppose to be:

    Code:
    // main loop
    	for(;;)
    	{
    		timeout.tv_sec = 15;
    		timeout.tv_usec = 0;
    		flag = 0;
             }
    
    		read_fds = master; // copy it
    		if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
    		{
    			perror("select");
    			exit(1);
    		}
    Thanks again and sorry about my careless.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I note that you also have a lot of nested loop and if statements.
    Try to take care not to make it so, because it can make it harder to see and maintain. Like instead of if (success), if (success), etc, you can for example, make a new function, and return false if it fails and let the caller handle the error in such a way. Then there's no need for lots of if statements.
    Likewise, when writing a new block, write out both the { and }, so you can see where it starts and ends directly, then write the code inside it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Elysia View Post
    Try to take care not to make it so
    One of the lesser known Jean-Luc Picard quotes...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, having a main that is 150 lines seems a bit excessive - about 20 lines or so should be max for a function in general [1] (this is just reinforcing what Elysa said, in a different way).

    [1] No I don't always follow this rule.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    We don't always follow the rules and advice we give to others
    Nevertheless, it's a good idea to try to adhere to those principles and advice, since there is a reason we tell it to others
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by matsp
    about 20 lines or so should be max for a function in general
    Such a rule of thumb would depend on one's indent style as well, methinks. I never really thought about this myself until recently when I had to review a 900 line function written by two of my team members.

    Mankthetank19, remember that you should write code not only for the compiler, but for yourself and others to read.
    Last edited by laserlight; 11-27-2008 at 08:21 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Thanks again for all the suggestions - I will try and remember it for next time.

    Thanks again!

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    When your indent level gets past 4 levels, you should start thinking about adding some functions.
    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.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Well I thought I could get the program running properly but:

    I can compile the code but it is not running properly. When I run the code on the server (terminal 1) and the client on terminal 2 and terminal 3, I get notified that they are connected. I can send messages on both clients successfully. However, I never receive the message: "Fifteen seconds have passed since the last message." if I don't send anything after 15 seconds. Does anybody have any suggestions? Thanks!

    Here is the new fix that dealt with the initial syntax error
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define PORT 9034 // port we're listening on
    
    int main(void)
    {
            struct timeval {
            int tv_sec;
            int tv_usec;
            };
    
            fd_set master, // master file descriptor list
                       read_fds; // temp file descriptor list for select()
            struct sockaddr_in myaddr, // server address
                                          remoteaddr; // client address
            int fdmax, // maximum file descriptor number
                    listener, // listening socket descriptor
                 newfd, // newly accept()ed socket descriptor
                 nbytes,
                 yes=1, // for setsockopt() SO_REUSEADDR, below
                  i, j;
    
            char buf[256]; // buffer for client data
            socklen_t addrlen;
    
            struct timeval timeout;
            int flag;
    
            FD_ZERO(&master); // clear the master and temp sets
            FD_ZERO(&read_fds);
            // get the listener
            if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
                    perror("socket");
                    exit(1);
            }
    
            // lose the pesky "address already in use" error message
            if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
                    perror("setsockopt");
                    exit(1);
            }
    
            // bind
            myaddr.sin_family = AF_INET;
            myaddr.sin_addr.s_addr = INADDR_ANY;
            myaddr.sin_port = htons(PORT);
            memset(&(myaddr.sin_zero), '\0', 8);
            if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
                    perror("bind");
                    exit(1);
            }
    
            // listen
            if (listen(listener, 10) == -1) {
                    perror("listen");
                    exit(1);
            }
    
            // add the listener to the master set
            FD_SET(listener, &master);
    
            // keep track of the biggest file descriptor
            fdmax = listener; // so far, it's this one
    
            // main loop
            for(;;) {
                    timeout.tv_sec = 15;
                    timeout.tv_usec = 0;
                    flag = 0;
    
                    read_fds = master; // copy it
                    if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                            perror("select");
                            exit(1);
                    }
    
                    // run through the existing connections looking for data to read
                    for(i = 0; i <= fdmax; i++) {
                            if (FD_ISSET(i, &read_fds)) { // we got one!!
                                    if (i == listener) {
    
                                            // handle new connections
                                            addrlen = sizeof(remoteaddr);
                                            if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen)) == -1) {
                                                    perror("accept");
                                            }
                                            else {
                                                    FD_SET(newfd, &master); // add to master set
                                                    if (newfd > fdmax) { // keep track of the maximum
                                                            fdmax = newfd;
                                                    }
                                                    printf("selectserver: new connection from &#37;s on "
                                                            "socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
                                            }
                                    }
                                    else {
                                            // handle data from a client
                                            if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
                                                    // got error or connection closed by client
                                                    if (nbytes == 0) {
                                                            // connection closed
                                                            printf("selectserver: socket %d hung up\n", i);
                                                    } else {
                                                            perror("recv");
                                                    }
                                                    close(i); // bye!
                                                    FD_CLR(i, &master); // remove from master set
                                            } else {
                                                    // we got some data from a client
                                                    for(j = 0; j <= fdmax; j++) {
                                                            // send to everyone!
                                                            if (FD_ISSET(j, &master)) {
                                                                    // except the listener and ourselves
                                                                    if (j != listener && j != i) {
                                                                            if (send(j, buf, nbytes, 0) == -1) {
                                                                                    perror("send");
                                                                            }
                                                                    }
                                                            }
                                                    }
                                            }
                                    }
                                    flag = 1;
                            }
                    }
             if(!flag)
                    {
                            strcpy(buf, "Fifteen seconds have passed since the last message.\n");
                            nbytes = strlen(buf) + 1;
                            for(j = 0; j <= fdmax; j++) {
                                                            // send to everyone!
                                                            if (FD_ISSET(j, &master)) {
                                                                    // except the listener and ourselves
                                                                    if (j != listener && j != i) {
                                                                            if (send(j, buf, nbytes, 0) == -1) {
                                                                                    perror("send");
                                                                              }
    }
    }
    }
    }
    }
    return 0;
    }

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
    Er, pass the timeout as well?
    Also, check the result for a timeout response.

    Also as well, fix your indentation.

    > FD_CLR(i, &master); // remove from master set
    What about updating fdmax?
    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.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Thanks for the help Salem but I am having a hard time understanding you - forgive me, I am still new with this language

    for your first suggestion - I don't understand what I need to do to if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)

    My timeout response was a copy and paste from the last "else" statement which I was told was correct.

    I am just confused.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Read the manual page for select.

    select(fdmax+1, &read_fds, NULL, NULL, &timeout)

    Oh, and when we say split things up into functions, we mean something like this (this doesn't work by the way, it's just the idea).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    #define PORT 9034 // port we're listening on
    
    int checkConnections ( fd_set read_fds, int fdmax );
    
    int main(void)
    {
        struct timeval {
            int tv_sec;
            int tv_usec;
        };
    
        fd_set master, // master file descriptor list
        read_fds; // temp file descriptor list for select()
        struct sockaddr_in myaddr, // server address
        remoteaddr; // client address
        int fdmax, // maximum file descriptor number
        listener, // listening socket descriptor
        newfd, // newly accept()ed socket descriptor
        nbytes,
        yes=1, // for setsockopt() SO_REUSEADDR, below
        i, j;
    
        char buf[256]; // buffer for client data
        socklen_t addrlen;
    
        struct timeval timeout;
        int flag;
    
        FD_ZERO(&master); // clear the master and temp sets
        FD_ZERO(&read_fds);
        // get the listener
        if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
    
        // lose the pesky "address already in use" error message
        if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
    
        // bind
        myaddr.sin_family = AF_INET;
        myaddr.sin_addr.s_addr = INADDR_ANY;
        myaddr.sin_port = htons(PORT);
        memset(&(myaddr.sin_zero), '\0', 8);
        if (bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
            perror("bind");
            exit(1);
        }
    
        // listen
        if (listen(listener, 10) == -1) {
            perror("listen");
            exit(1);
        }
    
        // add the listener to the master set
        FD_SET(listener, &master);
    
        // keep track of the biggest file descriptor
        fdmax = listener; // so far, it's this one
    
        // main loop
        for(;;) {
            timeout.tv_sec = 15;
            timeout.tv_usec = 0;
            flag = 0;
    
            read_fds = master; // copy it
            if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
                perror("select");
                exit(1);
            }
            flag = checkConnections( read_fds, fdmax );
            if(!flag)
            {
                strcpy(buf, "Fifteen seconds have passed since the last message.\n");
                nbytes = strlen(buf) + 1;
                for(j = 0; j <= fdmax; j++) {
                    // send to everyone!
                    if (FD_ISSET(j, &master)) {
                        // except the listener and ourselves
                        if (j != listener && j != i) {
                            if (send(j, buf, nbytes, 0) == -1) {
                                perror("send");
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }
    
    int checkConnections ( fd_set read_fds, int fdmax ) {
        int flag = 0;
        int i;
        // run through the existing connections looking for data to read
        for(i = 0; i <= fdmax; i++) {
            if (FD_ISSET(i, &read_fds)) { // we got one!!
                if (i == listener) {
    
                    // handle new connections
                    addrlen = sizeof(remoteaddr);
                    if ((newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen)) == -1) {
                        perror("accept");
                    }
                    else {
                        FD_SET(newfd, &master); // add to master set
                        if (newfd > fdmax) { // keep track of the maximum
                            fdmax = newfd;
                        }
                        printf("selectserver: new connection from &#37;s on "
                        "socket %d\n", inet_ntoa(remoteaddr.sin_addr), newfd);
                    }
                }
                else {
                    // handle data from a client
                    if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
                        // got error or connection closed by client
                        if (nbytes == 0) {
                            // connection closed
                            printf("selectserver: socket %d hung up\n", i);
                        } else {
                            perror("recv");
                        }
                        close(i); // bye!
                        FD_CLR(i, &master); // remove from master set
                    } else {
                        // we got some data from a client
                        for(j = 0; j <= fdmax; j++) {
                            // send to everyone!
                            if (FD_ISSET(j, &master)) {
                                // except the listener and ourselves
                                if (j != listener && j != i) {
                                    if (send(j, buf, nbytes, 0) == -1) {
                                        perror("send");
                                    }
                                }
                            }
                        }
                    }
                }
                flag = 1;
            }
        }
        return flag;
    }
    Nice simple short functions which do one specific job well are much preferred over long rambling functions which try to do everything.

    Learning how to split up code is key, especially when your code no longer fits comfortably in a single file.
    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.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Thanks for the help - when I get home I will look at what you did.

    I will also research select.

    I like your code of not having code within all those breaks like I originally had (all those breaks). This is still something new to me but I do get frustrated sometime because the code compiled but didn't run properly. Since I am still new to C, it was to difficult for me to troubleshoot my issue when there wasn't an error. Again, I appreciate your time and assistance - I will post up what I learn tonight. I am a Visual Basic programmer that makes things easier when I can program objects.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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