Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Communicate over the Net?

Status
Not open for further replies.

marine88

Technical User
Dec 20, 2002
7
CA
Does anyone know where i can get started to find out how to communicate my programs over LAN networks and the NET ? Like a chess game for example. If you can give me a place with a good tutorial that will help. Or if you have another idea?

Thanks Guys Marine

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The basis of life , is death and destruction, so just teach me c++ while im here.
 
Hello Marine, there's a site which gives excellent example of a step-by-step build of a client/server program using the MFC CSocket class. It is at:


Take into account that these are STREAM sockets. If I was going to write an online chess game, I would probably opt for the much easier UDP (datagram) sockets. UDP sockets are "connectionless" - you simply create a CAsyncSocket object, bind it to a port and IP address and then send some info. You can have your send/recieve functions for the socket in one thread that continually loops and listens for incoming data. This isn't actual code sample but it should give you an idea of how UDP sockets work:

[tt]// create your CAsyncSocket here!

// Bind the socket to a port/IP address here!

// other stuff here..
int bytesRead = -1;
CString msgOut = "";

// now do a continuous loop here...
while(1)
{
bytesRead = mySocket.ReceiveFrom(....);
if (bytesRead > 0)
{
// we got some data in - process it here!
}

// to send a message out on the socket, we simply
// assign some data to the "msgOut" CString:
if (msgOut.GetLength()>0)
{
// send the data out here...
socket.SendTo(......);
// dont forget to clear the string once the
// message has been sent so it doesn't get sent again
msgOut = "";
}
}[/tt]


A couple of points to note about UDP sockets - they are not as relaible as STREAM sockets BUT this only means you may drop one package in every 1,000 sent. For something like a chess game - this is not a big deal!
Also, it will be far, far easier to write your "servlet" application PLUS that servlet app will not need to be spawning new threads/processes for each new connection it receives because UDP sockets are connectionless!
Your servlet application (the program that sits on your web server and listens for incoming requests) can be written in a variety of languages including C/C++, Perl, Java, etc. (these all handle UDP sockets very well).

In a recent issue of CUJ Magazine (C/C++ User's Journal), there was an article stating that many applications would be better off being in UDP format rather than streaming sockets.

There's also a PDF document available for download off the web called "Beej's Guide to Network Programming" that'll give you a full insight into all types of sockets and also several examples of client/server programs written in C/C++. Although mainly for UNIX, 99% of it applies to windows too.

Hope this helps!

:)
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Im not finding the client / server stuff on the site =(

Anyway. thanks for the help. What did you mean in the sections that says: msgOut = "";
Im not understanding that part. Can you explain it in a bit more depth.

Thanks Marine

- - - - - - - - - - - - - - - - - - - - - - - - - - - -

The basis of life , is death and destruction, so just teach me c++ while im here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top