throws IOException

advertisement
Socket 101
Excerpt from Network Programming
EDA095 Nätverksprogrammering
Originals by
Roger Henriksson
Computer Science
Lund University
Nätverksprogrammering
Java I/O – Streams
Stream (swe. Ström)
- A stream is a sequential ordering of bytes (chars)
- Input/output is mostly done by streams, examples
include keyboard input, terminal window, files, and
network communication
- Bytes are written to streams and read from streams
2
Nätverksprogrammering
InputStream
Represents an incoming stream
public int abstract read() throws IOException;
public int read(byte[] input) throws IOException;
public int read(byte[] input,int offset,int length)
throws IOException;
public long skip(long n) throws IOException;
public int available() throws IOException;
public void close() throws IOException;
3
Nätverksprogrammering
OutputStream
Represents an outgoing stream
public abstract void write(int b) throws IOException;
public void write(byte[] data) throws IOException;
public void write(byte[] data, int offset, int length)
throws IOException;
public void flush() throws IOException;
public void close() throws IOException;
4
Nätverksprogrammering
Socket – end point for network communication
-
The socket object handles connection to other
sockets
The stream objects handle read/write of bytes
(created by the socket upon connection)
Two variants, UDP and TCP
5
Nätverksprogrammering
TCP – transmission control protocol
Connection between two “ports” on (usually) two different
computers. Keeps connection. Less efficient than UDP, but:
• Automatic resending / sorting of messages
• No size limit
TCP-connection works as two streams (in/out)
6
Nätverksprogrammering
Socket
Constructors
public Socket(String host, int port)
throws UnknownHostException, IOException;
public Socket(InetAddress host,int port)
throws IOException;
Get-methods
public InputStream getInputStream() throws IOException;
public OutputStream getOutputStream() throws IOException;
public InetAddress getInetAddress();
public int getPort();
public InetAddress getLocalAddress();
public int getLocalPort();
7
Nätverksprogrammering
Socket, cont’d
Settings
public void setTcpNoDelay(boolean on)
throws SocketException;
public boolean getTcpNoDelay() throws SocketException;
public void setSoTimeout(int milliseconds)
throws SocketException;
public int getSoTimeout() throws SocketException;
Disconnect
public void close() throws IOException;
8
Nätverksprogrammering
Create a socket (client)
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = new Socket(“argus-7.student.lth.se",80);
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (UnknownHostException e) {
System.out.println(e);
System.exit(1);
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
9
Nätverksprogrammering
Reading blocks
public int read(byte[] input,int offset,int length)
throws IOException;
No guarantee that length number of bytes is read!
Example: how to read exact 100 bytes
byte[] buffer = new byte[100];
int read = 0;
int result = 0;
while (read<100 && result!=-1) {
result = input.read(buffer,read,100-read);
if (result!=-1)
read = read+result;
}
10
Nätverksprogrammering
ServerSocket
Receiver for connection requests from sockets. Upon connection
creates a socket object
Server life cycle
1. Instantiate ServerSocket.
2. Wait for connection (accept()). Socket is created.
3. Fetch streams, getInputStream() and getOutputStream().
4. Communicate by reading and writing bytes.
5. Disconnect
6. Go to step 2 and wait for new connection.
11
Nätverksprogrammering
ServerSocket, cont’d
Constructors
public ServerSocket(int port) throws IOException;
public ServerSocket(int port, int queueLength)
throws IOException;
Wait for connection (blocking)
public Socket accept() throws IOException;
Disconnect server
public void close() throws IOException;
12
Nätverksprogrammering
ServerSocket, cont’d
Get-methods
public InetAddress getInetAddress();
public int getLocalPort();
public int getSoTimeout() throws IOException;
Set-methods
public void setSoTimeout(int timeout) throws IOException;
13
Nätverksprogrammering
UDP – user datagram protocol
Protocol for sending packages. Efficient (for media), but
• Limited package size – large messages must be divided
• No guarantee that packages arrive at destination
• No guarantee that packages arrive in order
Unconnected protocol
14
Download