Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
SSC - Communication and Networking
SSC - Communication and Networking
Java Socket Programming (I)
Shan He
School for Computational Science
University 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 request
for services
I Server: a computer running a program that offers requested
services, e.g., processing database queries from one or more
clients
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 network
communication.
I Similar to performing file I/O, e.g., socket handle is treated
like file handle.
I Independent of a programming language
SSC - Communication and Networking
Sockets and Socket-based Communication
Sockets
I A socket consists of
I Local socket address: Local IP address and service port number
I Remote socket address: Only for established TCP sockets
I Protocol: A transport protocol, e.g., TCP or UDP.
I A socket address is the combination of an IP address (phone
number) 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 one
type of service or to talk to multiple hosts/computers at a
time
I Ports are used to distinguish these services
I Each service offered by a computer is identified by a port
number
I Represented as a positive (16-bit) integer value
I Some ports have been reserved to support common services
I FTP: 21/TCP
I HTTP: 80/TCP,UDP
I 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 port
number for a client to make a connection request
I Step 2: the server accepts the connection if everything goes
well
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 connection
request 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 address
identified by a IP address.
I Connection binds a port number to a socket number.
I Both TCP and UDP protocols use ports to map incoming
data 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 the
rapid development of network applications.
I Essentially provides classes, interfaces, and exceptions to
simplify the complexity involved in creating client and server
programs
I Two key classes for creating server and client programs
I ServerSocket
I 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 client
DataInputStream 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 client
I 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 the
socket.
I Step 3: Read from and write to the stream according to the
server’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 vs
DataOutputStream
I PrintWriter : “Prints formatted representations of objects
to a text-output stream.”
I DataOutputStream : “A data output stream lets an
application write primitive Java data types to an output
stream in a portable way”
I PrintWriter inherited from Writer class, while
DataOutputStream inherited from OutputStream .
I OutputStream (therefore DataOutputStream ) is
designed for binary data, while Writer (therefore
PrintWriter ) 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 the
local 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 have
agreed to use to communicate.
I Using Java.net class, you can build you own protocol using
socket
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 on
TCP socket communication
I Please take a look at my source code of send email using only
socket