第9将 网络编程-TCP网络程序

来源:互联网 发布:火线福利轻松领软件 编辑:程序博客网 时间:2024/06/04 23:20

TCP网络程序
·TCP网络程序的工作原理


·ServerSocket类
·Socket类
·简单的TCP服务器程序
·完善的TCP服务器程序模型
·TCP客户端程序
·如何检测和解决端口冲突的问题
·通过TCP程序在网络上传递对象

TCP网络程序的工作原理
TCP客户端程序与TCP服务器端程序的交互过程:
(1)服务器程序创建一个ServerSocket,然后调用accept方法等待客户来连接。
(2)客户端程序创建一个Socket并请求与服务器建立连接。
(3)服务器接收到客户的连接请求,并创建一个新的Socket与该客户建立的专线连接。
(4)建立了连接的两个Socket在一个单独的线程(由服务器程序创建)上对话。
(5)服务器开始等待新的连接请求,当新的连接请求到达时,重复步骤(2)到步骤(5)的过程。

ServerSocket
·构造函数:
 -public ServerSocket()
 -public ServerSocket(int port)
 -public ServerSocket(int port,int backlog)
 -public ServerSocket(int port,int backlog,InetAddress bindAddr)
·close方法
·accept方法

Socket
·构造函数:
 -public Socket()
 -public Socket(String host,int port)
 -public Socket(InetAddress address,int port)
 -public Socket(String host,int port,InetAddress localAddr,int localPort)
 -public Socket(InetAddress address,int port,InetAddress localAddr,int localPort)
·getInputStream和getOutputStream方法