Thread: Receiving a syntax error

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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 09:21 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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.

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, 03:07 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10: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