SSC - Communication and Networking
SSC - Communication and NetworkingJava Socket Programming (I)
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Communication and Networking
Outline
Outline of Topics
Sockets and Socket-based Communication
TCP Socket Programming in Java
SSC - Communication and Networking
Sockets and Socket-based Communication
Client/Server Communication
I The simplest level of network communication
I Consists of a server, client, and a network for communication
I Client: a computer running a program that makes a requestfor services
I Server: a computer running a program that offers requestedservices, e.g., processing database queries from one or moreclients
Client Server
Network (TCP/IP)
Request
Results
SSC - Communication and Networking
Sockets and Socket-based Communication
TCP/IP Application layer: Sockets
Transport
Application
Internet
Network Interface
FTP SMTP HTTP IMAP
SSL/TLS ACSII
TCP UDP
IP
Physical
ProtocolsTCP/IP model
Sockets
SSC - Communication and Networking
Sockets and Socket-based Communication
TCP/IP model: Application layer - Sockets
I The lowest level before Transport layer
I Definition: “endpoint of a two-way communication link”.
I Provides an interface for programming networkcommunication.
I Similar to performing file I/O, e.g., socket handle is treatedlike file handle.
I Independent of a programming language
SSC - Communication and Networking
Sockets and Socket-based Communication
Sockets
I A socket consists ofI Local socket address: Local IP address and service port numberI Remote socket address: Only for established TCP socketsI Protocol: A transport protocol, e.g., TCP or UDP.
I A socket address is the combination of an IP address (phonenumber) and service port number (extension).
I A socket API is an application programming interface (API),usually provided by the operating system.
SSC - Communication and Networking
Sockets and Socket-based Communication
Service ports
I Computers often communicate and provide more than onetype of service or to talk to multiple hosts/computers at atime
I Ports are used to distinguish these services
I Each service offered by a computer is identified by a portnumber
I Represented as a positive (16-bit) integer valueI Some ports have been reserved to support common services
I FTP: 21/TCPI HTTP: 80/TCP,UDPI IMAP: 143/TCP
I User-level process/services use port number value > 1024
SSC - Communication and Networking
Sockets and Socket-based Communication
Establish socket connection
I Step 1: The server listens to the socket of a specific portnumber for a client to make a connection request
I Step 2: the server accepts the connection if everything goeswell
I Step 3: the server gets a new socket bound to a different(usually random) port for connection with the client
I Go to Step 1
I Question: the service port in the TCP socket connectionrequest is different from the service port for the connection.Why?
SSC - Communication and Networking
Sockets and Socket-based Communication
Establish socket connection
Service Port
Server Client
Service Port
Server Client
Service Port
Connection request
Connection
SSC - Communication and Networking
Sockets and Socket-based Communication
Put them together
I Client is connected to a server at the physical addressidentified by a IP address.
I Connection binds a port number to a socket number.
I Both TCP and UDP protocols use ports to map incomingdata to a particular process running on a computer.
FTP HTTP SMTP App
Port Port Port Port
TCP or UDP
Port# Data
PacketData
Sockets
Apps
SSC - Communication and Networking
TCP Socket Programming in Java
Java.net class
I Java provides a set of classes, java.net , to enable therapid development of network applications.
I Essentially provides classes, interfaces, and exceptions tosimplify the complexity involved in creating client and serverprograms
I Two key classes for creating server and client programsI ServerSocketI Socket
SSC - Communication and Networking
TCP Socket Programming in Java
Socket-based client and server Java programming
I Server: ServerSocket creates a new service socket (port)
I Clinet: Socket establishes connection with service socket
I Both: exchange data using input and output streams
SSC - Communication and Networking
TCP Socket Programming in Java
Java.net class
ServiceSocket
1254
Server (IP: 121.2.21.25) Client
Socket((“121.2.21.25”, 1254)
Output/write stream Input/read stream
SSC - Communication and Networking
TCP Socket Programming in Java
A simple Server Program in Java (I)
Steps to create a simple server program:
I Step 1: Open the Server Socket:ServerSocket server = new ServerSocket( PORT );
I Step 2: Wait for the Client Request:Socket client = server.accept();
I Step 3: Create I/O streams for communicating to the clientDataInputStream is =
new DataInputStream(client.getInputStream());
DataOutputStream os =
new DataOutputStream(client.getOutputStream());
SSC - Communication and Networking
TCP Socket Programming in Java
A simple Server Program in Java (II)
I Step 4: Perform communication with clientI Receive from client: String line = is.readLine();
I Send to client: os.writeBytes("Hello!");
I Step 5: Close the stream is.close(); os.close();
I Step 6: Close the socket: client.close();
SSC - Communication and Networking
TCP Socket Programming in Java
A simple Client Program in Java
I Step 1: Open a socket.
I Step 2: Open an input stream and output stream to thesocket.
I Step 3: Read from and write to the stream according to theserver’s protocol.
I Step 4: Close the streams.
I Step 5: Close the socket.
SSC - Communication and Networking
TCP Socket Programming in Java
A few explainations: PrintWriter vsDataOutputStream
I PrintWriter : “Prints formatted representations of objectsto a text-output stream.”
I DataOutputStream : “A data output stream lets anapplication write primitive Java data types to an outputstream in a portable way”
I PrintWriter inherited from Writer class, whileDataOutputStream inherited from OutputStream .
I OutputStream (therefore DataOutputStream ) is
designed for binary data, while Writer (thereforePrintWriter ) is designed for text data
SSC - Communication and Networking
TCP Socket Programming in Java
A few explainations: InetAddress
I InetAddress class: “represents an Internet Protocol (IP)address.”
I getLocalHost() method: “Returns the address of thelocal host.”
SSC - Communication and Networking
TCP Socket Programming in Java
A few explainations: Protocol
I A protocol is the language that the client and server haveagreed to use to communicate.
I Using Java.net class, you can build you own protocol usingsocket
I One example: KnockKnockProtocol can be found at here
SSC - Communication and Networking
TCP Socket Programming in Java
Socket programming for email
I POP3, IMAP and SMTP are all email protocols based onTCP socket communication
I Please take a look at my source code of send email using onlysocket
Top Related