黑马程序员-Java网络编程
2015-08-26 09:18
603 查看
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
TCP程序设计
在JAVA中使用Socket(即套接字)完成TCP程序的开发,使用此类可以方便的建立可靠的,双向的,持续的,点对点的通信连接。
在Socket的程序开发中,服务器端使用ServerSocke等待客户端的连接,对于java的网络程序来讲,每一个客户端都使用一个Socket对象表示。
.ServerSocket类的常用方法:
在服务器端每次运行时都要使用accept()方法等待客户端连接,此方法执行之后服务器端将进入阻塞状态,直到客户端连接之后程序才可以向下继续执行。此方法的返回值类型是Socket,每一个Socket都表示一个客户端对象。
.Socket类的常用方法:
.第一个TCP程序--Echo程序
Echo 程序是一个网络编程通信交互的一个典型的案例,称为回应程序,即客户端输入哪些内容,服务器端会在这些内容前加上“ECHO:”并将信息发回客户端。
之前的程序代码,服务器端每次执行完毕后,服务器都会退出,这是因为服务器端只能接受一个客户端的连接,主要是由于accept()方法只能使用一次,本程序中将通过
循环的方法使用accept()方法,这样,每一个客户端执行完毕后,服务器端都可以重新等待用户连接。
单线程范例:
单线程范例:
多线程范例:
多线程范例:
在服务器端,每一个连接到服务器的客户端Socket都会以一个线程的方式运行,这样无论有多少个客户连接都可以同时完成操作。
TCP程序设计
在JAVA中使用Socket(即套接字)完成TCP程序的开发,使用此类可以方便的建立可靠的,双向的,持续的,点对点的通信连接。
在Socket的程序开发中,服务器端使用ServerSocke等待客户端的连接,对于java的网络程序来讲,每一个客户端都使用一个Socket对象表示。
.ServerSocket类的常用方法:
序号 | 方法 | 类型 | 描述 |
1 | public ServerSocket(int port)throws IOException | 构造函数 | 创建ServerSocket实例,并监听指定的端口。 |
2 | public Socket accept()throws IOException | 普通函数 | 等待客户端连接,此方法连接之前一直阻塞 |
3 | public InetAddress getInetAddress() | 普通函数 | 返回服务器的IP地址 |
4 | public boolean isClosed() | 普通函数 | 返回ServerSocket的关闭状态 |
5 | public void close()throws IOException | 普通函数 | 关闭ServerSocket |
.Socket类的常用方法:
序号 | 方法 | 类型 | 描述 |
1 | public Socket(String host , int port)throws UnknownHostException,IOException | 构造 | 构造Socket对象,同时指定要连接服务器的主机名及连接端口号。 |
2 | public InputStream getInputStream() throws IOException | 普通 | 返回此套接字的输入流 |
3 | public OutputStream getOutputStream() throws IOException | 返回此套接字的输出流 | |
4 | public void close() throws IOException | 关闭此Socket | |
5 | public boolean isClosed() | 判断此套接字是否被关闭 |
.第一个TCP程序--Echo程序
Echo 程序是一个网络编程通信交互的一个典型的案例,称为回应程序,即客户端输入哪些内容,服务器端会在这些内容前加上“ECHO:”并将信息发回客户端。
之前的程序代码,服务器端每次执行完毕后,服务器都会退出,这是因为服务器端只能接受一个客户端的连接,主要是由于accept()方法只能使用一次,本程序中将通过
循环的方法使用accept()方法,这样,每一个客户端执行完毕后,服务器端都可以重新等待用户连接。
单线程范例:
EchoClient
1 public class EchoClient{
2 public static void main(String[] args) throws IOException {
3
4 Socket client = new Socket("localhost", 8888);
5 BufferedReader buf = null;
6 PrintStream out = null;
7 out = new PrintStream(client.getOutputStream());
8 out.println(1);
9 BufferedReader input = null;
10 input = new BufferedReader(new InputStreamReader(System.in));
11 buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
12 boolean flag = true;
13
14 while (flag) {
15 System.out.println("Please Key In:");
16 String str = input.readLine();
17 out.println(str);
18 if ("bye".equals(str)) {
19 flag = false;
20 } else {
21 String echo = buf.readLine();
22
23 System.out.println("1111" + echo);
24 }
25 }
26 client.close();
27 buf.close();
28 }
29 }
单线程范例:
EchoServer
1 public class EchoServer{
2 public static void main(String[] args) throws IOException {
3 ServerSocket server = null;
4 Socket client = null;
5 PrintStream out = null;
6 BufferedReader buf = null;
7 server = new ServerSocket(8888);
8 boolean f = true;
9 while (f) {
10 System.out.println("Server Running,Waiting for Client");
11 client = server.accept();
12 out = new PrintStream(client.getOutputStream());
13 // 获取客户端输入的信息
14 buf = new BufferedReader(new InputStreamReader(
15 client.getInputStream()));
16 // 实例化输出端
17 out = new PrintStream(client.getOutputStream());
18 boolean flag = true;
19 while (flag) {
20 String str = buf.readLine();
21 if (str == null || "".equals(str)) {
22 flag = false;
23 } else {
24 if ("bye".equals(str)) {
25 flag = false;
26 } else {
27 out.println("ECHO" + str);
28 }
29 }
30 }
31 out.close();
32 client.close();
33 }
34 server.close();
35 }
36
37 }
多线程范例:
EchoThread
1 public class EchoThreadimplements Runnable {
2
3 private Socket client = null;
4
5 public EchoThread(Socket client) {
6 this.client = client;
7 }
8
9 public void run() {
10 PrintStream out = null;
11 BufferedReader buf = null;
12
13 try {
14 buf = new BufferedReader(new InputStreamReader(
15 client.getInputStream()));
16 out = new PrintStream(client.getOutputStream());
17 boolean flag = true;
18
19 while (flag) {
20 String str = buf.readLine();
21 if (str == null || "".equals(str)) {
22 flag = false;
23 } else {
24 if ("bye".equals(str)) {
25 flag = false;
26 } else {
27 out.println("ECHO:" + str);
28 }
29 }
30 }
31 out.close();
32 client.close();
33 } catch (Exception e) {
34 }
35 }
36
37 }
多线程范例:
EchoServer
1 public class EchoServer{
2 public static void main(String[] args) throws IOException {
3 ServerSocket server = null;
4 Socket client = null;
5 PrintStream out = null;
6 BufferedReader buf = null;
7 server = new ServerSocket(8888);
8 boolean f = true;
9 while (f) {
10 System.out.println("Server Running,Waiting for Client");
11 client = server.accept();
12 new Thread(new EchoThread(client)).start();
13
14 }
15 server.close();
16 }
17
18 }
在服务器端,每一个连接到服务器的客户端Socket都会以一个线程的方式运行,这样无论有多少个客户连接都可以同时完成操作。
相关文章推荐
- 使用专用硬件加速深度卷积神经网络
- php对象的序列化(串行化)用作 写入文件或者网络传输
- HTTP网络协议(一)
- Linux网络相关命令
- TCP 服务器与客户端
- Volley网络通信框架学习笔记(二)
- 网络
- BeautifulSoup
- TCP/IP协议详解-TCP简介与TCP建立与终止
- 发送HTTP请求
- TCP/IP协议
- (一二五)手机网络状态的监听
- (一二五)手机网络状态的监听
- 网络爬虫:URL去重策略之布隆过滤器(BloomFilter)的使用
- 网络爬虫:URL去重策略之布隆过滤器(BloomFilter)的使用
- TCP客户端和服务端的通信,通过服务端将客户端的小写字母转化成大写发送回来
- 在Ubuntu 14.04 64bit上使用网络诊断工具mtr
- Python源码分析1 - Building Python http://blog.csdn.net/atfield/article/details/1396729
- 2016网络红人萧天 最新资料
- 网络相关的Linux命令总结