/ Programming Ib: Lab
Concurrency and Networking: Networking
Back to contents
You probably already know what networking is. In fact, you
certainly used networking to read this file, by downloading it off the
Internet. You may not know how simple networking really is, at least to
incorporate into our own programs. When it comes to our applications,
networking really just has two components. 1) pass some data over a
network, 2) set up some protocol so you know how to interpret the data
that you are receiving (and you know what to send in response). Of
course, the technical details of part 1 are more complicated than that,
but when it comes to actually networking in Java, just like many other
concepts like data structures or concurrency, all the hard work has
already been done for us. We just need to know how to use the tools.
Now you know the basics of networking. You have built a
and a server, and they communicate by just sending messages backwards
and forwards. Now we go back to the EchoClient from point 2.
- Open the Oracle
Java Tutorial on networking. You can read the first lessons
if you like, but the content we will work through here is All
About Sockets. This is the information we need about
connecting two computers directly (rather than getting things from the
- Implement the EchoClient from the tutorial. The code is
all there, but make sure you understand how it works. Bear in mind it
won't work without a
server. We'll make one later.
- This is in the first three pages of the tutorial.
After this you should understand how clients work in Java. The class
that allows you to connect to a server is Socket.
implement the KnockKnockClient and Server from the next few pages of
the tutorial. Again, the code is all provided for you, but don't just
copy it blindly, read and understand it.
You may need to
implement the KnockKnockProtocol as well, but remember that this part
is just Java. It is not using any networking features, it is just
providing the responses for the server. All you need to network is a
Socket and a ServerSocket.
When you are running a server on your own machine you can define what
port to use.
(When you try to connect, you can use the hostname localhost to
connect to your own machine.)
- After this you should understand how servers work in
Java. The class that you need to build a server is ServerSocket.
EchoServer that you can run and connect to with the EchoClient (you can
change the hostname in the client). When you send a message from the
EchoClient, the server should reply with the exact same message.
Now you should have a client that connects to a server. At the
moment the server is just repeating what the client says, which is
fine. This still shows two points, 1) networking is simple, most of the
work is done for you in Java, and 2) the messages being sent backwards
and forwards are just text. You may need a protocol, but the
communication itself is nothing more complicated.
We are now going to combine the last two sections and show you
why you need concurrency to properly use networking.
Concurrency Meets Networking >>
page author: Andrew Chinery
last updated: 01 March 2013