UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » timing a socket for input

#1 2009-06-19 07:38 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Offline

#2 2009-06-19 11:58 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: timing a socket for input

Offline

#3 2009-06-20 12:19 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Thanks for the reply.

Yeah, this is purely a learning exercise using BSD sockets.  I think I understood what you said - *very roughly*, something like this:

struct player
{
	int socket;
	char name[20];
	int rating;
	/* etc */
};

struct game
{
	struct player *p1;
	struct player *p2;
	int END_OF_ROUND_TIME;
	/* etc */
};


while(1)
{

	select (fdmax+1, &read_fds, NULL, NULL, TIME_REMAINING);

	if (FD_ISSET(i, &read_fds))
	{
		if (i == listener) 
		{
			/* handle new connection */
		}
		else
		{
			/* handle data from a client */

			/* if a new round is starting */
			END_OF_ROUND_TIME = get_end_of_round_time();

			/* if the client is in a game at the moment */
			/* then the data will be their updated word */

			TIME_REMAINING = CURRENT_TIME - END_OF_ROUND_TIME;
		}
	}

}


??

I think I may have bitten off more than I can chew actually.  I've been thinking about all the things the server should do, and  I can see I'll probably struggle with it.  Ah well, I'm sure I'll have fun along the way :)

Offline

#4 2009-06-20 01:46 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: timing a socket for input

My advice is to not solve the problem in one go, but to split it up. Divide and
conquer:

- Make one networking module with a nice and clean API which does all the
  networking stuff you need in a way that fits bets with the rest of your code.

- Make a timer system with a nice and easy API which adds time awareness
  to the whole. As the network module can block, you need to add a time-out
  parameter to those network functions (and internally use the select timeout
  as Jeremy said).

- Have all the game logic in one separate module which uses the above two
  API and adds its owns.

Now you only need to decide where your main loop (or control loop) is. It's
probably cleanest to have that in a main.c next to all the initialisation stuff
and then uses APIs of the other modules to glue everything together and
make it all work. But depending on the game logic having it there can make
more sense.

Basically, you need to design the whole keeping in mind how you want all
the parts to interact with each other.

You can gobble it all together and add one feature after the other (first
networking, then timers and game logic, and then something else), but it
will get exponentially more complex if you use an unstructured approach.

Don't go overboard to the other side though, chopping it into a million
tiny pieces by over-engineering it is as counter-productive.

Offline

#5 2009-06-20 03:13 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Edited:

Offline

#6 2009-06-21 07:22 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: timing a socket for input

Offline

#7 2009-06-21 05:27 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Thanks jfriesne, I have uploaded what I've done thus far (see previous post's attachment).  I am surprised that it compiles!

$ gcc -Wall -c main.c game.c socket.c -o anagrammatic
$ ./anagrammatic
accept() failed: Invalid argument

So accept() fails when I connect through telnet.  I think it's a pointer issue.

Offline

#8 2009-06-21 06:17 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: timing a socket for input

Offline

#9 2009-06-21 07:43 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

(gdb) where
#0  0xb7ef1424 in __kernel_vsyscall ()
#1  0xb7e69931 in send () from /lib/i686/cmov/libc.so.6
#2  0x08048f46 in sendall (s=5, buf=0x807b650 "Enter your name: ", len=0xbfa0aac8) at socket.c:151
#3  0x08048e7c in get_name (s=5, set=0xbfa0abc8, name=0x807b014 "") at socket.c:124
#4  0x08048d2a in handle_new_client (sock=5, remoteaddr=0xbfa0ac4c, master=0xbfa0abc8, fdmax=0xbfa0ac48, p=0x807b008)
    at socket.c:78
#5  0x08048a07 in main () at main.c:50

Offline

#10 2009-06-25 04:47 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Didn't figure out the problem so I quickly re-wrote the function.  Seems to have done the trick.  I've only done the handling of new clients and haven't really touched on anything else yet. At the moment the client is just telnet. I'll probably write one after I've done the server though.

Offline

#11 2009-06-27 11:28 AM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Offline

#12 2009-06-27 05:51 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: timing a socket for input

Offline

#13 2009-06-27 06:55 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Offline

#14 2009-06-29 11:17 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: timing a socket for input

(Sorry, I'm quite busy lately, otherwise I would read your code and give feedback. :-/)

Offline

#15 2009-06-30 07:16 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

No worries at all.  I have enough to keep me going until I run into problems or finish it.  Thanks.

Offline

#16 2009-07-04 06:42 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Offline

#17 2009-07-06 04:21 PM

Tommo
Member
Registered: 2007-09-03
Posts: 76

Re: timing a socket for input

Offline

  • Index
  • » C
  • » timing a socket for input

Board footer

Powered by FluxBB