Title: TCPIP Protocol Stack
1TCP/IP Protocol Stack
Application
Sockets
(Gate to network)
TCP
UDP
IP
Device Drivers
2Programming with Sockets
- Sockets are Berkeley software distribution UNIX
interface to network protocols - Endpoint of communication, has a type and one
associated process - Uses file system model (open-close-read-write)
- Internet vs. Unix domain
3Client-Server Architecture
request
Process request
client
response
Host www.vcu.edu Port 80
44 steps in programming a client
- Open a socket.
- Open an input and output stream to the socket.
- Read from and write to the socket according to
the server's protocol. - Clean up.
5Socket in Java
- Two important classes
- Socket a normal socket as communication end
- ServerSocket accept the incoming request
- ServerSocket is an analogy to the custom service
phone number, it will dispatch your call to a
specialized staff--- normal Socket.
6Client Socket Creation
Socket MyClientMyClient new Socket("Machine
name", PortNumber)
SOCKET DESCRIPTOR
port (a number) on which the server you want
the machine you are trying to open a connection
to
port numbers between 0 and 1,023 are reserved for
for standard services, such as email, FTP, and
HTTP. for your server, select one that is
greater than 1,023
7Create Server Socket
- If you are programming a server, then this is how
you open a socket - ServerSocket MyServicetry   MyServerice
new ServerSocket(PortNumber)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ca
tch (IOException e) Â Â Â Â Â Â Â Â Â Â
System.out.println(e)Â Â Â Â Â Â Â Â
8Use ServerSocket listen, accept, and Create
connections from clients.
- Socket clientSocket nulltry  Â
serviceSocket MyService.accept()Â Â Â Â Â Â Â Â catc
h (IOException e) Â Â System.out.println(e)
A blocking call, will return when a request arrive
9Send Data to Socket at Client
- PrintStream outputtry   output new
PrintStream(MyClient.getOutputStream()) - String inLhello
- output.writeBytes(inL"\n")
10Read data from Socket at Client
- DataInputStream inputtry   input new
DataInputStream(MyClient.getInputStream()) - ..
- String responseLine input.readLine()
- System.out.println("Server "
responseLine)
11Send Data to Socket at Server
- Use the serviceSocket returned from the accept()
call.
- PrintStream outputtry   output new
PrintStream(serviceSocket.getOutputStream())outp
ut.println(this is server reply)
12Read Data From Socket at Server
- Same as client case
- DataInputStream inputtry     input new
DataInputStream(serviceSocket.getInputStream()) - String reqinput.readLine()
13Close Socket Connection
- On the server side
- try   output.close()  input.close() Â
serviceSocket.close() Â Â MyService.close()
catch (IOException e) Â Â System.out.println(e)
- On the client side
- try        output.close()      Â
input.close()Â Â MyClient.close() catch
(IOException e) Â Â System.out.println(e)
14A Simple Echo Server
- // listen and accept connections.// Open input
and output streamstry           clientSocket
echoServer.accept()Â Â Â Â Â Â Â Â Â Â is
new DataInputStream (clientSocket.getInputStream(
)) os new PrintStream
(clientSocket.getOutputStream())// As long as
we receive data, echo that data back to the
client.         while (true)            Â
line is.readLine()Â Â Â Â Â Â Â Â Â Â Â Â
os.println(line) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
catch (IOException e) Â Â Â Â Â Â Â Â Â Â
System.out.println(e)Â Â Â Â Â Â Â Â Â Â Â Â
- import java.io.import java.net.public class
echo3 Â Â Â Â public static void main(String
args) Â Â Â Â Â Â Â Â ServerSocket echoServer
null        String line        DataInputStream
is        PrintStream os        Socket
clientSocket null        try          Â
echoServer new ServerSocket(9999)Â Â Â Â Â Â Â Â Â Â Â
     catch (IOException e)          Â
System.out.println(e)Â Â Â Â Â Â Â Â Â Â
15A simple client
- smtpSocket new Socket("128.172.167.167", 9998)
- os new DataOutputStream(smtpSocket.getOutputStr
eam()) - is new DataInputStream(smtpSocket.getInputStrea
m()) -
- while (true)
- inLd.readLine()
- os.writeBytes(inL"\n")
- if (inL.compareTo("quit")0) break
- // keep on reading from/to the socket till we
receive the "Ok" from SMTP, - // once we received that then we want to break.
responseLine is.readLine()
System.out.println("Server " responseLine)
responseLine is.readLine()
System.out.println("Server " responseLine)
responseLine is.readLine()
System.out.println("Server " responseLine)
16Reference
- Class ServerSocke
- A server socket waits for requests to come in
over the network. It performs some operation
based on that request, and then possibly returns
a result to the requester. - accept() Listens for a connection to be made to
this socket and accepts it. - getInetAddress()
- Returns the local address of this server socket.
17Class Socket
- This class implements client sockets (also called
just "sockets"). A socket is an endpoint for
communication between two machines. - Socket(InetAddress address, int port)
- connect(SocketAddress endpoint)    Connects this
socket to the server. - getPort() Â Â Â Returns the remote port to which
this socket is connected. - getLocalPort() Â Â Â Â Â Â Â Â Â Â Returns the local port
to which this socket is bound. - getInputStream() Â Â Â Â Â Â Â Â Â Â Returns an input
stream for this socket. - getOutputStream() Â Â Â Â Â Â Â Â Â Â Returns an output
stream for this socket. - getInetAddress() Â Â Â Â Â Â Â Â Â Â Returns the address
to which the socket is connected. - getLocalAddress() Â Â Â Â Â Â Â Â Â Â Gets the local
address to which the socket is bound.
18Other Parameters in Socket
- Buffer size
- Incoming buffer size
- Outgoing buffer size
- Time out or not
- Otherwise a bad read will block your browser for
ever. - Status check