JavaSockets 2
JavaSockets 2
This means, roughly, create input and output streams for sending data between programs running simultaneously on each machine.
The programs can then talk to each other. In Java this is lowest-level form of communication from application developers view
Programmer is aware of a stream that connects two computers. Programmer fully responsible for managing and 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.
One of the good things about Java Supported in the standard language (j2sdk) Distinction between high and low-level blurred somewhat by ability to wrap streams (e.g. ObjectOutputStream)
Still, socket programming differs from other distributed programming in its low-level nature.
Shared resources (web servers, ftp servers, mail servers) Online auctions, exchanges, etc. Data locality Localize computing power Crash protection Software maintainability
Write a program that dials up another program at a specified IP address running on a specified port. Call this program the client. Second program server accepts connection and establishes input/output stream to client. When server accepts, client can establish input/ouput stream to server
Client makes request of server by sending data. Server sends replies to client. Protocol must be defined so client/server understand can interpret messages.
Two processes running on specific port of specific machine. Either process can dial up the other process. When connection is established, applications talk at a peer level, each making requests of each other, rather than one making requests and the other serving up those requests. Will see many examples soon.
Good news: This is very simple in Java Really only 3 additional classes are needed java.net.InetAddress java.net.Socket java.net.ServerSocket
java.net.InetAddress
representing that name (basically encapsulates name and IP associated with name);
java.net.Socket
InputStream getInputStream();
returns an instance of InputStream for getting info from the
OutputStream getOutputStream();
returns an instance of OutputStream for sending info to
close();
close connection to implicit socket object, cleaning up
resources.
java.net.ServerSocket
ServerSocket(int port);
enables program to listen for connections on port port
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.
Very important to ensure that server is robust and will not crash. Important Exceptions:
InterruptedIOException ConnectException
Be sure to close your sockets either after a crash or upon expected completion.
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 or reversi game
Technically, a structured piece of info sent from one agent to another. Can be thought of as a set of commands with arguments that each agent understands and knows how to act upon. Groupings of such commands are commonly referred to as a protocol. HTTP, FTP, etc. are all protocols (get, put, ...)
Existing protocols might be totally inappropriate for the needs of an application. An existing protocol may work but be too inefficient (e.g. SOAP). In general, can fine-tune the protocol exactly to your application to minimize memory and bandwidth overhead.
Can be overkill when communication needs are simple. Can be inefficient when transaction throughput is critical.
Crucial point:
Synchronous: each agent waits for response after sending their message, then does work handshaking.
Aysnchronous (typical) work needs to be done after sending message. Doesnt know when reply will come, but single thread busy so cant process message.
We will illustrate some of these concepts by creating the framework for a two-person game of chess. I say framework because the entire class structure and all methods are in place. However, the implementations of the methods are left incomplete. The hard work has been done, though, and what remains is totally isolated and algorithmic.
When a message is received by an agent, the Do method is called to perform the appropriate action. Individual messages that subclass BasicMessage must implement this method.
public abstract class BasicMsgHandler implements Runnable{ InputStream in; OutputStream out; //constructors, accessor/mutators public BasicMessage readMsg() throws IOException; public void send Msg(BasicMessage) throws IOException; public void run(){} //calls readMsg continuously and processes protected abstract BasicMessage buildMessage(String) ; //creates object
Stores the input and output streams for reading and writing, wherever these come from. readMsg marshals raw data and builds BasicMessage object using buildMessage(). sendMsg unmarshals BasicMessage object and sends raw data tokens to stream. run reads a message and processes (do method) when something is in the input stream.
At this point well step aside from the networking aspect and create the local, standalone ChessPlayer class. The important point is that this class should have no knowledge that it will live in a networked environment. This division of labor greatly facilitates code maintenance, testing, and reuse.