Basic Socket Programming With Java
Basic Socket Programming With Java
with Java
What is a socket?
Generally refers to a stream connecting processes
Sockets, cont.
Sockets represent a low-level abstraction for
application communication.
Programmer is aware of a stream that connects two
computers.
Programmer fully responible for managing/interpreting
flow of bytes between computers
Higher-level techniques
message passing systems (MPI, SOAP, JMS),
extensions to web servers (ASP, JSP, servelets, etc),
distributed objects (CORBA, RMI), web services, etc.
languages (j2sdk)
Distinction between high and low-level
blurred somewhat by ability to wrap
streams (ObjectOutputStream, etc.)
Still, socket programming differs from other
distributed programming in its low-level
nature.
mail servers)
Online auctions, exchanges, etc.
Data locality
Localize computing power
Crash protection
Software maintainability
specific machine.
Either process can dial up the other process.
When connection is established,
applications talk at a peer level, rather than
one making requests and the other serving
up those requests.
Will see many examples soon.
InputStream getInputStream();
returns an instance of InputStream for getting info from the
implicit Socket object
OutputStream getOutputStream();
returns an instance of OutputStream for sending info to
implicit Socket object.
close();
close connection to implicit socket object, cleaning up
resources.
Socket accept();
blocks until connection is requested via Socket
request from some other process. When connection
is established, an instance of Socket is returned for
establishing communication streams.
Error Handling
Very important to ensure that server is
Examples
Best way to learn this is to study several
canonical examples
See many simple course examples under
standaloneClient package
Next, do simple EchoServer
Then, Threaded EchoServer
Then, fully synchronized tic-tac-toe
Then, chess game