Advertisement

a small mud

Started by May 01, 2005 12:51 PM
16 comments, last by ErUs 19 years, 9 months ago
i am making a small mud. most of the network code is working but i dont want to use multiple threads and i need to see if a socket has and data. i have tried select() but i cant figure out how todo it. can someone give me a snippit plz edit: winsock , C++
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
This should work (untested):
void Receive(SOCKET* theSockets, size_t nSockets)timeval theTimeout;fd_set theSet;int nResult;const int nBufferSize = 1024;BYTE byBuff[nBufferSize];   // Add all sockets to the set   theTimeout.tv_sec = 0;   theTimeout.tv_usec = 0;   FD_ZERO(&theSet);   for(size_t i=0; i<nSockets; ++i)      FD_SET(theSockets,&theSet);   // See if there's any pending data   nResult = select(0,&theSet,NULL,NULL,&theTimeout);   if(nResult == SOCKET_ERROR)      return; // Error   // Read from all sockets with pending data   for(DWORD i=0; i<theSet.fd_count; ++i)   {      // Read from the socket      nResult = recv(theSet.fd_array,(char*)byBuff,nBufferSize,0);      // Check return code and do something with the data in byBuff   }}
Advertisement
ok thats cool :)

how can i check wether theres a client waiting to connect? for accept()

oh and btw should it allways return -1 ( nResult )
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Using the exact same code. When a conmnection is pending, select() says that the listening socket is readable.
So, you'd do:
bool ConnectionPending(SOCKET sock){timeval theTimeout;fd_set theSet;int nResult;   // Add socket to the set   theTimeout.tv_sec = 0;   theTimeout.tv_usec = 0;   FD_ZERO(&theSet);   FD_SET(sock,&theSet);   // See if there's any pending connection   nResult = select(0,&theSet,NULL,NULL,&theTimeout);   if(nResult != 1) return false; // Error, or no connection waiting   return true; // select() says there's a pending connection}
fantastic thanks :)

shall be increasing your rating ;)
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
omg its taking shape allready :) got error checking, a client array ( only updates stuff is that client is active ) tested with multiple clients

btw, anyone know the character for enter?


:( not so nice anymore
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Advertisement
If you're supporting telnet, enter is "\r\n". If you're using Win32, it's usually '\r', and for output you should usually send "\r\n".
thanks man.
i also found my spammage problem, i wasnt zeromemeory() the buffer before each time i recv'ed

telnet sends one character at a time?
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Telnet usually sends one character at a time, but it can send several. Some telnet clients send one line at a time.
If you're using TCP/IP, there's no guarantee that one send() will result in one recv(). It might end up in multiple recv() calls. So, you should design your netcode so you can keep reading data into a buffer until you have a newline, or a flood. If you do that, then handling telnet should go perfectly well.
windows telnet does send everytime you type a letter but I think unix sends only when you hit return.

This topic is closed to new replies.

Advertisement