Thread: Would anyone care to help me

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

    Would anyone care to help me

    I was wondering if anyone would be able to look at this
    accepting loop that I have for a network connection.
    It Kind of works. It accepts multiple connections and
    I believe fcnlt is working. I want to be able to have time
    while still processing the connections somehow.

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

    Socket question

    Also I have a socket question.
    can I make a socket equal
    a structure and use the structure to send data to?
    I have a mud that does it but I am
    not sure if there is something I'm missing
    or if you just gotta create the structure you want.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Where is your code?
    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.

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

    Here is my main loop.

    It's not hard to read so don't be to scared of the loop

    Code:
    int main(int argc, char *argv[])
    {
          fd_set masterdesc;
          fd_set readescrip;
          int fd;
    	
          struct sockaddr_in address;
          struct timeval seconds;
          int simpleSocket;
          int clientSocket;
          int intiger, yes = 1;
    
          seconds.tv_sec  = 0;      /* seconds for select to wait for connections     */
          seconds.tv_usec = 1000; /* microseconds for select to wait for connections
    	                           * there are 1,000 microseconds in a millisecond
    				   * there are 1,000 milliseconds in a second     
    				   * thus there are 1,000,000 Microseconds in one 
    				   * second, and there are 60 seconds in one minute */
    	FD_ZERO(&masterdesc);
            FD_ZERO(&readescrip);
    	printf("ragnarok: process id #%i", getpid());
    
    	if ((simpleSocket=socket(AF_INET, SOCK_STREAM,0)) == -1)
    	    fatal("creating simpleSocket, AF_INET SOCK_STREAM failed");
    	fcntl(simpleSocket, F_SETFL, O_NONBLOCK); 
    	setsockopt(simpleSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = INADDR_ANY;
    	address.sin_port = htons(PORT);
    
         if (bind(simpleSocket,(struct sockaddr *)&address,sizeof(address)) == -1)
         {
        	      fatal("binding simpleSocket, AF_INET SOCK_STREAM failed");
    	      close(simpleSocket);
         }
    
         if (listen(simpleSocket, 1000) == -1) 
         {
                  fatal("listening to simpleSocket, AF_INET SOCK_STREAM failed");
                  close(simpleSocket);
        }
    
        FD_SET(simpleSocket, &masterdesc);
    
        while (!ragnarok_shutdown)
        {
            readescrip = masterdesc;
            if (select(FD_SETSIZE, &readescrip, NULL, NULL, NULL) == -1)
    	    nonfatal("selecting file descriptor");
            clientSocket = accept(simpleSocket, NULL, NULL);
    	FD_SET(clientSocket, &readescrip);
    /*I think I want to request from users without
       stopping this god forsakin game loop. */
    	welcome_message(cliantSocket);
            prompt_for_username(clientSocket);
    /*I think I want to request from users without
       stopping this god forsakin game loop. */
    
            if (read(clientSocket, request, sizeof(request)) < 0) 
    	{
    			close(clientSocket);
    			FD_CLR(clientSocket, &readescrip);
    			printf("ragnarok: read(clientSocket, request, sizeof(request)) < 0");
    			printf("ragnarok: client on descriptor #%i disconnected\n", clientSocket);
    	}
        }
        for (intiger = 0; intiger < FD_SETSIZE; intiger++)
            if (FD_ISSET(intiger, &readescrip))
            {
    	    close(intiger);
    	    FD_CLR(intiger, &readescrip);
                printf("client on descriptor#%i disconnected\n", intiger);
            }
        close(simpleSocket);
        exit(EXIT_SUCCESS);
    }
    Last edited by errigour; 02-11-2011 at 12:06 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (select(FD_SETSIZE, &readescrip, NULL, NULL, NULL) == -1)
    1. readescrip appears to be empty
    2. Using FD_SETSIZE is a sledgehammer to crack a nut.

    I think you want
    readescrip = masterdesc;
    at the start of the loop, and make everything inside the loop update masterdesc.

    Something to keep track of the maximum allocated file descriptor would be good as well.
    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.

  6. #6
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I thought I didn't have to use the same descriptor that the master socket was on
    but I guess I was mistaken. That still leaves me with the problem of continuing the
    loop while accepting requests and prompting users with fun stuff every millisecond.

  7. #7
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    I guess I'm Looking for a way to wait for users requests without freezing my game loop.
    Last edited by errigour; 02-11-2011 at 12:13 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe add stdin to your masterdesc as well?

    Then you can wait for events on either your listen socket or the user input.

    Well assuming you're using real select, and not the broken MS implementation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obama on health care
    By MK27 in forum General Discussions
    Replies: 35
    Last Post: 09-11-2009, 07:05 PM
  2. Anyone care to help out a newbie?
    By televisual in forum C++ Programming
    Replies: 7
    Last Post: 10-02-2008, 07:19 PM
  3. Health Care for Programmers
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 02-16-2003, 02:08 AM
  4. you all care way too much
    By Overclocked in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 05-12-2002, 06:10 AM