Вы находитесь на странице: 1из 26

JAVA

NETWORK
PROGRAMMING (I)

A. Ngorora
INTRODUCTION

 Send and receive messages among


computers on the Internet
 Computers need addresses of other
computers to communicate
 Server and one or more clients
TERMS

 TCP
 IP
 Domain name
 Domain name server
 Stream-based communications
 Packet-based communications
INTERNET PROTOCOL (IP)
 IP is low-level protocol for delivering data
across the Internet in packets
 TCP and UDP delivers streams of data
 IP address uniquely identifies the
computer on the Internet
 IP address consist of 4 dotted decimal
numbers between 0 and 255, such as
130.254.204.33
IPCONFIG COMMAND
 Check your own ipaddress
DOMAIN NAME

 IP addresses are not easy to


remember
 Mapped to names
 Such as liang.armstrong.edu
COMPUTER NAME?
DOMAIN NAME SERVER(DNS)

 Special server on Internet

 Translate host names into IP addresses


TRANSMISSION CONTROL
PROTOCOL (TCP)

 Guarantees delivery of data


and also guarantees that
packets will be delivered in
the same order in which they
were sent
USER DATAGRAM PROTOCOL
(UDP)
 Standard low-overhead,
connectionless, host-to-host
protocol
 Used over the Internet
 Allows an app on one computer to
send a datagram to an app on
another computer
PACKET-BASED
COMMUNICATION
 Use UDP
 Cannot guarantee lossless
transmissions
STREAM-BASED
COMMUNICATION

 Use TCP
 Can detect lost transmissions
and then re-submit
 Transmissions are lossless
 And reliable
SOCKETS: SERVER & CLIENT

 Sockets are the endpoints of logical


connections between two hosts
 Sockets can be used to send and
receive data
 Java treat it similar to reading and
writing files
CLIENT-SERVER
COMMUNICATION
 The client sends requests to the server
 The server responds
 The client begins by attempting to
establish a connection to the server
 The server can accept or deny the
connection
 Once connection: communicate via
sockets
 Server must be running if client wants to
connect
SERVER SOCKET
 java.net package
 Create a server socket
 Attach to port
 ServerSocket serverSocket = new
ServerSocket(port)
 Java.net.BindException: port already in use
 Listen for connections:
Socket socket = serverSocket.accept();
PORTS
 Port identifies TCP service
 Port numbers : 0-65536
 Reserved port numbers: 0 - 1024
 Reserved for privileged services
 Examples:
Port 25: email
Port 80: Web server
PING COMMAND
 Use PING command at DOS prompt level
 to check if all packets send are received
CLIENT SOCKET
 Request connection to server:
 Socket socket = new Socket(serverName, port);
 serverName: Internet host name or IP address
 Socket socket = new Socket(“103.254.204.33”,
8000);
 Socket socket = new Socket(“liang.armstrong.edu”,
8000);
 Host not found: UnknownHostException
DATA TRANSMISSION - SOCKETS
 InputStream input =
socket.getInputStream();
 OutputStream output =
socket.getOutputStream();
 Input/OutputStream: bytes
 DataInputStream/DataOutputStream/Buffere
dReader/PrintWriter: data
 int, double, String
READING AND WRITING
PRIMITIVE DATA VALUES
 DataInputStream input = new
DataInputStream(socket.getInputStream());
 DataIOutputStream input = new
DataOutputStream(socket.getOutputStream()
);
 Server can use
 input.readDouble()
 output.writeDouble()
EXTRAS
 Connection to server on the same
machine
Socket socket = new Socket(“local
host”, 8000);
 Local host: current machine
 Server not running?
ConnectException
 To see local port on host:
socket.getLocalPort();
SUMMARY
 import java.net.* ;
 Create server program
 Create client program
 Make sure your know the Ip address/name
 Create sockets
 Connect client to server
 Communicate
PRACTICAL

 Type listing 33.1 and 33.2


 Own computer
 Various computers

Вам также может понравиться