【高级网络程序设计】Week2-1 Sockets

一、The Basics

1. Sockets

定义 An abstraction of a network interface
应用

use the Socket API to create connections to remote computers

send data(bytes)

receive data(bytes)

2. Java network programming

the java network library import java.net.*;
similar to reading and writing files

on a remote machine

receive/send data

二、Java I/O

1. I/O

原因

computer programs need to interact with the world

- bring in information from an external source

- send out information to an external destination

interact 定义

Input/Output:

Input(Read): to bring in information

Output(Write): to send out information

Information

特点

anywhere;of any type

2. Streams

定义 a connection to a source of data or to a destination for data (sometimes both)
作用 Streams can represent any data, so a stream is a sequence of bytes that flow from a source to a destination

stream can carry data

byte streams, for machine-formatted data

• InputStream, OutputStream

• Writing and reading are very efficient.

– character streams (textual), for human-readable data • Reader / Writer

• Require translation

应用
 
 read information from an input stream and write information to an output stream.
 A program can manage multiple streams simultaneously
流程 【高级网络程序设计】Week2-1 Sockets_第1张图片
opening and closing a stream

Opening

• When you open a stream, you are making a connection to that external place.

• Once the connection is made, you forget about the external place and just use the stream

Closing

• A stream is an expensive resource.

There is a limit on the number of streams that you can have open at one time.

• You should not have more than one stream open on the same file.

• You must close a stream before you can open it again. 

using a stream

• Some streams can be used only for input, others only for output, others for both.

• Using a stream means doing input from it or output to it.

3. Using java I/O

read/write a text file

– involves creating multiple streams;

– the streams are connected to provide the required functionality;

– read from/write to text files require declaring and using the correct I/O streams.

writing to a Socket

•  The Socket object presents a stream to you (the programmer)

– You don’t need to worry about how the network sends bytes

– You just interact with the stream

• Java’s built-in multithreading:

– Handles multiple connections at once.

– E.g. a web server will create 1 thread for each request it receives

– So it can handle multiple clients in parallel

三、Port

1. IP addressing

作用

identifier:Identifying computers on a network

分类

Domain names: DNS (Domain Name System) form (www.qmul.ac.uk)

IP (Internet Protocol) address

- “dotted quad” format

-139.255.27.125, a 32-bit number

- java.net package:static InetAddress.getByName()

An object of type InetAddress that you can use to build a socket.

- 127.0.0.1 is for local machine.

DN maps to an IP address 

www.eecs.qmul.ac.uk -> 138.37.95.147

步骤:

- The Domain Name System (DNS) performs this mapping

- DNS servers handle lookups

- return the IP address that maps to a domain name

- send DNS queries to a DNS server

nslookup DN

【高级网络程序设计】Week2-1 Sockets_第2张图片

//Find out your IP address 
import java.net.*;
public class IPFinder { 
    public static void main(String[] args) throws Exception { 
        String domainName = “www.qmul.ac.uk”;
        InetAddress a = InetAddress.getByName(domainName); 
        System.out.println(a); 
    } 
}

2. Basics of the client-server model

Network

allows two machines to connect and talk to each other.

• One machine has to stay and listen: server.

• The other machine makes requests: client.

• The client is trying to connect to the server. Once connected, there is a two way communication.

【高级网络程序设计】Week2-1 Sockets_第3张图片
Testing programs with a network

• If your code is not working, check if both computers are online

• Open your terminal window: Type – ping www.eecs.qmul.ac.uk

Testing programs without a network

• A special address called localhost( 127.0.0.1. )

• Run both client and server on one machine (localhost)

• Producing a localhost:

InetAddress addr = InetAddress.getByName(null);  InetAddress.getByName("localhost");

InetAddress.getByName("127.0.0.1");

3. Port

原因

An IP address isn’t enough to identify a unique server:

Many services can exist on one machine

定义  A unique identifier for a particular service running on a machine.
应用

• When setting up a client or a server:

– Must choose a port.

– Both client and server agree to connect.

• The port is not a physical location in a machine, but a software abstraction.

常见 System services reserve the use of ports 0 through 1023. ( Do not use them)
Usual choice for web proxy is port 8080
Usually represented as IP address: port. ——127.0.0.1:8080 (localhost:8080)

There are several standard ports that always get used for the same applications

80 for web servers (HTTP)

443 for encrypted web servers (HTTPS)

22 for secure shell (SSH)

20 and 21 for File Transfer Protocol (FTP)

25 for Simple Mail Transfer Protocol (SMTP)

Door is the IP address Letter boxes are the ports;

Allows us to talk to multiple people (services) in one house (computer)

四、Sockets

1. Sockets

原因

• When a client wants a service, it attempts the connection with the server by supplying the port number associated with the service.

• There are likely to be multiple clients waiting for the same service at the same time (e.g. web browsers wanting a web page).

• The server needs a way to distinguish between clients and keeping their communication separate.

– This is achieved by the use of sockets.

• The client creates a socket at its end of the communication link.

• The server, upon receiving the client’s initial request (on a particular port number), creates a new socket at its end, dedicated to the communication with that specific client.

2. Sockets and Java Sockets

定义

A socket is the software abstraction used to represent the “terminals” of a connection between two machines.
Socket class

Server socket: a server is used to listen for incoming connections.

Socket: a client is used to initiate a connection.

• Making a socket connection:

– ServerSocket returns a corresponding Socket

– via the accept() method

– through which communications will take place on the server side.

• Then it is a true Socket to Socket connection:

– May treat both ends the same way. 

InputStream and OutputStream

• Once you’ve created a socket, you can get access to its input and output streams

• To produce the corresponding InputStream and OutputStream objects from each Socket, use the methods:

– mySocket.getInputStream()

– mySocket.getOutputStream()

Wrap inside buffers and formatting classes just like any other stream object
Creating a Socket

• Create a ServerSocket:

– Only need to give the port number.

– IP address is not necessary.

• Create a Socket:

– Give both the IP address and port number.

– Indicate where to connect.

你可能感兴趣的:(高级程序设计,网络)