12 May 2014 11:37
Post deleted by an administrator.
Included page "inc:signature" does not exist (create it now)
27 May 2011 06:14 |
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!
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();
// 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(); }
Submit Feedback
Please rate this page based on the quality and helpfulness of the content to you.
Average 100% rating from 1 votes.
OLD COMMENT SYSTEM (6 comments)
12 May 2014 11:37
Post deleted by an administrator.
Included page "inc:signature" does not exist (create it now)
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.
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 :)
~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server | NEW: Wikiroo, backup tool (in development)
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.
~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server | NEW: Wikiroo, backup tool (in development)
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
~ Leiger - Wikidot Community Admin - Volunteer
Wikidot: Official Documentation | Wikidot Discord server | NEW: Wikiroo, backup tool (in development)
Post preview:
Close preview