码迷,mamicode.com
首页 > 其他好文 > 详细

基于 TCP/IP 协议的网络编程

时间:2017-03-23 01:00:36      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:trace   性问题   打印   ace   实现   应用   com   family   端口   

在说明基于 TCP/IP 协议的网络编程之前,先来了解一下 Socket(网络套接字):
  • 利用套接字(Socket)开发网络应用程序早已被广泛的采用,以至于成为事实上的标准
  • 通信的两端都要有 Socket,是两台机器间通信的端点(API 原话)
  • 网络通信其实就是 Socket 间的通信
  • Socket 允许程序把网络连接当成一个数据在两个 Socket 间通过 IO 传输
  • 一般主动发起通信的应用程序属客户端,等待通信请求的服务
网络编程某种程度上可以称作“Socket 编程”

TCP/IP 编程,简单模拟客户端与服务端之间的交互(先开 Server 再开 Client):
  1. public class TestTCP {
  2. @Test
  3. public void client() {
  4. Socket socket = null;
  5. OutputStream os = null;
  6. try {
  7. // 创建 Socket 对象以指明目标 Server 的 IP 和端口
  8. socket = new Socket("127.0.0.1", 9876);
  9. // 调用该套接字对象的 getOutputStream()方法返回 OutputStream 对象以写出数据
  10. os = socket.getOutputStream();
  11. // 后面实际就是 IO 流的应用
  12. os.write("This is the message sent by client".getBytes());
  13. } catch (UnknownHostException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. if (os != null) {
  19. try {
  20. os.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. // socket 也是稀有资源,记得及时关闭
  26. if (socket != null) {
  27. try {
  28. socket.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. @Test
  36. public void server() {
  37. ServerSocket ss = null;
  38. Socket socket = null;
  39. InputStream is = null;
  40. try {
  41. // 创建 ServerSocket 对象指明开放那个端口作为传输端口
  42. ss = new ServerSocket(9876);
  43. // 调用 accept()方法,返回一个 Socket 对象,监听 ServerSocket 对应的端口并且接受对该端口的连接
  44. socket = ss.accept();
  45. // 调用该套接字对象的 getInputStream()方法返回 InputStream 对象以写入从 client 接收到的数据
  46. is = socket.getInputStream();
  47. // 后面实际就是 IO 流的应用
  48. byte[] b = new byte[10];
  49. int len;
  50. while ((len = is.read(b)) != -1) {
  51. // 打印字节流
  52. System.out.print(new String(b, 0, len));
  53. }
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. } finally {
  57. if (is != null) {
  58. try {
  59. is.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. if (socket != null) {
  65. try {
  66. socket.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. if (ss != null) {
  72. try {
  73. ss.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79. }
  80. }

若要实现多步交互,注意 Socket 方法的阻塞性问题,如果一直监听端口,就不能执行后续代码,故记得调用相关 shutdown 方法以关闭监听,以下例子修改自上例,实现在 Server 端收到字节流后,返回给 Client 一条反馈信息:
  1. public class TestTCP {
  2. @Test
  3. public void client() {
  4. Socket socket = null;
  5. OutputStream os = null;
  6. InputStream is = null;
  7. try {
  8. // 创建 Socket 对象以指明目标 Server 的 IP 和端口
  9. socket = new Socket("127.0.0.1", 9876);
  10. // 调用该套接字对象的 getOutputStream()方法返回 OutputStream 对象以写出数据
  11. os = socket.getOutputStream();
  12. // 后面实际就是 IO 流的应用
  13. os.write("This is the message sent by client".getBytes());
  14. // ----------update----------
  15. // 显式的禁用此套接字的输出流
  16. socket.shutdownOutput();
  17. // 接受反馈
  18. is = socket.getInputStream();
  19. byte[] b = new byte[10];
  20. int len;
  21. while ((len = is.read(b)) != -1) {
  22. System.out.print(new String(b, 0, len));
  23. }
  24. // ----------update----------
  25. } catch (UnknownHostException e) {
  26. e.printStackTrace();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. } finally {
  30. if (os != null) {
  31. try {
  32. os.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. // socket 也是稀有资源,记得及时关闭
  38. if (socket != null) {
  39. try {
  40. socket.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }
  47. @Test
  48. public void server() {
  49. ServerSocket ss = null;
  50. Socket socket = null;
  51. InputStream is = null;
  52. OutputStream os = null;
  53. try {
  54. // 创建 ServerSocket 对象指明开放那个端口作为传输端口
  55. ss = new ServerSocket(9876);
  56. // 调用 accept()方法,返回一个 Socket 对象,监听 ServerSocket 对应的端口并且接受对该端口的连接
  57. socket = ss.accept();
  58. // 调用该套接字对象的 getInputStream()方法返回 InputStream 对象以写入从 client 接收到的数据
  59. is = socket.getInputStream();
  60. // 后面实际就是 IO 流的应用
  61. byte[] b = new byte[10];
  62. int len;
  63. while ((len = is.read(b)) != -1) {
  64. // 打印字节流
  65. System.out.print(new String(b, 0, len));
  66. }
  67. // ----------update----------
  68. // 反馈一条消息给 Client
  69. os = socket.getOutputStream();
  70. os.write("I have received your message".getBytes());
  71. // ----------update----------
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. } finally {
  75. if (os != null) {
  76. try {
  77. os.close();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. if (is != null) {
  83. try {
  84. is.close();
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. if (socket != null) {
  90. try {
  91. socket.close();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. if (ss != null) {
  97. try {
  98. ss.close();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. }
  105. }

基于 TCP/IP 协议的网络编程

标签:trace   性问题   打印   ace   实现   应用   com   family   端口   

原文地址:http://www.cnblogs.com/chendifan/p/6602523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!