1306476866|%A, %e %B %Y, %H:%M
This is my first attempt at network programming with Java.
In this case, the client and server are the same machine - my MacBook. However in theory it should work the same way when using two different machines.
My short-term plan is to get the client to send messages to the server, so that I can write something into the client, and the server can then display this message.
The next goal will be to make this go both ways - where two computers alternate between the role of Client and Server, sending messages to each other.
If you are a Java programmer looking for sample code, scroll down!
Simplified code
Use this to get started with setting up a TCP connection in Java. If you want to learn how to use networking for Java games, I'd suggest looking at UDP instead - it's less reliable and packets aren't necessary kept in the correct order, but it is faster.
Client
// Connect to Socket Socket sock = new Socket(SERVER_HOST, SERVER_PORT); // ... // When you are finished, remember to close the socket! sock.close();
Server
// Create the ServerSocket object ServerSocket sock = new ServerSocket(SERVER_PORT); // At the moment, we're just looping indefinitely, waiting for connections while (true) { // Listen for new sockets Socket newSock = sock.accept(); // If a connection is found, deal with it in a separate thread // This allows us to continue listening on the current thread if (newSock != null) new MyThread(newSock).start(); }



Speed and reliability are not the only differences between UDP and TCP. With UDP the individual packets can arrive at the destination out of sequence.
That wont usually happen with localhost, nor even on two machines on the same Ethernet, but it can and will happen once you move your application out onto the Internet.
Copyright waived on Wikidot official and community sites — see profile for details.
Website: www.soronlin.org.uk
Diaspora*: ku.em.ecatstam.dop|nilnoros#ku.em.ecatstam.dop|nilnoros
Yep - I remember being told that a while ago, back in a networking fundamentals course (but that was all about how networks actually work at each layer, rather than anything programming-related).
It was just an off-hand comment above (not meant to be comprehensive), but I should probably mention that difference as well for completeness. Thanks :)
Shane Smith ★ Website: Wikidot Development & Programming. ★ STE Wikidot Editor: Windows. Mac. Linux. ★ Twitter. Google+.
If, like in my case, your just sending data one time every few minutes, such as high scores ect, you can send POST data to a PHP page and let the php page handle it, similarly if you are trying to get data only every know and again, you can make yuour program read from a php page
PS, your editor box is a little missaligned.
At the moment I'm doing it just to experiment, but I'll probably turn it into a chat application as that seems to be the simplest possible way to use this for something useful.
Yep, that seems to be a problem with the CSS I'm using here. Are you using Firefox?
I'm going to remove it from my main theme for now.
Shane Smith ★ Website: Wikidot Development & Programming. ★ STE Wikidot Editor: Windows. Mac. Linux. ★ Twitter. Google+.
Need some help with the syntax jannatmehra? Let me know :)
This blog uses Wikidot.com syntax. Here's a long list of example syntax: doc:wiki-syntax
Shane Smith ★ Website: Wikidot Development & Programming. ★ STE Wikidot Editor: Windows. Mac. Linux. ★ Twitter. Google+.
Post preview:
Close preview