Presentation is loading. Please wait.

Presentation is loading. Please wait.

实验4 基于Socket的C/S程序开发 实验目的

Similar presentations


Presentation on theme: "实验4 基于Socket的C/S程序开发 实验目的"— Presentation transcript:

1 实验4 基于Socket的C/S程序开发 实验目的
理解和巩固传输层与套接字的基本知识,掌握利用套接字实现面向连接的数据传输的一般方法,深入理解客户/服务器工作模式,学会简单的客户/服务器程序的开发。 实验内容 利用Java语言提供的Socket技术,建立一个C/S模式的应用,允许客户端用户输入2个整数,服务器端接收这2个整数,并计算出它们的和、差、积、商,最后送回客户端。 计算机工程学院 伍俊明

2 实验4 基于Socket的C/S程序开发 Socket的工作层次 Socket API 应用层 应用程序 表示层 高层 会话层 传输层
低层 网络层 网际层(IP) 数据链路层 网络接口层 物理层 计算机工程学院 伍俊明

3 利用socket实现面向连接的数据传输 Server端 Client端 创建ServerSocket对象,在某个端口监听服务
建立连接 创建Socket对象, 向Server的监听端口请求 接受Client端的请求,用返回Socket建立连接 数据通信 通过Socket读写数据,实现与Server端的通信 通过Socket读写数据,实现与Client端的通信 释放连接 关闭Socket, 结束与Server端的通信 关闭Socket, 结束与Client端的通信,等待其他请求 关闭ServerSocket对象结束监听 计算机工程学院 伍俊明

4 实验4 基于Socket的C/S程序开发 服务器端ServerSocket类 类:java.net.ServerSocket 构造方法
public ServerSocket() throws IOException; Creates an unbound server socket. public ServerSocket(int port) throws IOException; Creates a server socket, bound to the specified port. public ServerSocket(int port, int backlog) throws IOException; ; Creates a server socket and binds it to the specified local port number, with the specified backlog. public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException; Create a server with the specified port, listen backlog, and local IP address to bind to. 计算机工程学院 伍俊明

5 实验4 基于Socket的C/S程序开发 服务器端ServerSocket类 主要方法
public void bind(SocketAddress endpoint) throws IOException Binds the ServerSocket to a specific address (IP address and port number). public Socket accept() throws IOException ; Listens for a connection to be made to this socket and accepts it. void close() throws IOException; Closes this socket. 计算机工程学院 伍俊明

6 实验4 基于Socket的C/S程序开发 客户端Socket类 类:java.net.Socket 构造方法 public Socket()
Creates an unconnected socket, with the system-default type of SocketImpl. public Socket(String host, int port) throws UnknownHostException, IOException Creates a stream socket and connects it to the specified port number on the named host. public Socket(InetAddress address, int port) throws IOException Creates a stream socket and connects it to the specified port number at the specified IP address. public Socket(InetAddress address, int port, InetAddres localAddr, int localPort) throws IOException Creates a socket and connects it to the specified remote address on the specified remote port. 计算机工程学院 伍俊明

7 实验4 基于Socket的C/S程序开发 客户端socket 主要方法
public void connect(SocketAddress endpoint) throws IOException Connects this socket to the server. public InputStream getInputStream() throws IOException Returns an input stream for this socket. public void bind(SocketAddress bindpoint) throws IOException Binds the socket to a local address. public OutputStream getOutputStream() throws IOException Returns an output stream for this socket. public void close() throws IOException Closes this socket 计算机工程学院 伍俊明

8 实验4 基于Socket的C/S程序开发 服务器端编程 创建ServerSocket对象
ServerSocket server=new ServerSocket(8000); 监听来自客户端的请求 Socket linkSocket=server.accept(); 与客户端进行通信 利用linkSocket.getInputStream() 接收客户端数据 利用linkSocket.getoutputStream() 向客户端发送数据 监听结束时关闭服务器 server.close(); 计算机工程学院 伍俊明

9 实验4 基于Socket的C/S程序开发 客户端编程 建立连接
Socket client=new Socket(“ServerComputerName”, 8000); 与服务器进行通信 利用client.getInputStream() 接收服务器端数据 利用client.getoutputStream() 向服务器端发送数据 释放连接 Client.close(); 要求:在一台主机上安装并运行服务器端程序,在另一台计算机上安装并运行客户端程序,测试所编写的程序能否完成规定的功能。 计算机工程学院 伍俊明

10 思考题 Java语言中,服务器端和客户端套接字对象类相同吗?如不同,有何差别?
程序编写完毕并编译后,先运行客户端程序,还是先运行服务器端程序?为什么? Java语言中如何使用UDP协议进行数据传输的? 计算机工程学院 伍俊明


Download ppt "实验4 基于Socket的C/S程序开发 实验目的"

Similar presentations


Ads by Google