标签:trace 性问题 打印 ace 实现 应用 com family 端口
在说明基于 TCP/IP 协议的网络编程之前,先来了解一下 Socket(网络套接字):
public class TestTCP {@Testpublic void client() {Socket socket = null;OutputStream os = null;try {// 创建 Socket 对象以指明目标 Server 的 IP 和端口socket = new Socket("127.0.0.1", 9876);// 调用该套接字对象的 getOutputStream()方法返回 OutputStream 对象以写出数据os = socket.getOutputStream();// 后面实际就是 IO 流的应用os.write("This is the message sent by client".getBytes());} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}// socket 也是稀有资源,记得及时关闭if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}@Testpublic void server() {ServerSocket ss = null;Socket socket = null;InputStream is = null;try {// 创建 ServerSocket 对象指明开放那个端口作为传输端口ss = new ServerSocket(9876);// 调用 accept()方法,返回一个 Socket 对象,监听 ServerSocket 对应的端口并且接受对该端口的连接socket = ss.accept();// 调用该套接字对象的 getInputStream()方法返回 InputStream 对象以写入从 client 接收到的数据is = socket.getInputStream();// 后面实际就是 IO 流的应用byte[] b = new byte[10];int len;while ((len = is.read(b)) != -1) {// 打印字节流System.out.print(new String(b, 0, len));}} catch (IOException e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}}
public class TestTCP {@Testpublic void client() {Socket socket = null;OutputStream os = null;InputStream is = null;try {// 创建 Socket 对象以指明目标 Server 的 IP 和端口socket = new Socket("127.0.0.1", 9876);// 调用该套接字对象的 getOutputStream()方法返回 OutputStream 对象以写出数据os = socket.getOutputStream();// 后面实际就是 IO 流的应用os.write("This is the message sent by client".getBytes());// ----------update----------// 显式的禁用此套接字的输出流socket.shutdownOutput();// 接受反馈is = socket.getInputStream();byte[] b = new byte[10];int len;while ((len = is.read(b)) != -1) {System.out.print(new String(b, 0, len));}// ----------update----------} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}// socket 也是稀有资源,记得及时关闭if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}@Testpublic void server() {ServerSocket ss = null;Socket socket = null;InputStream is = null;OutputStream os = null;try {// 创建 ServerSocket 对象指明开放那个端口作为传输端口ss = new ServerSocket(9876);// 调用 accept()方法,返回一个 Socket 对象,监听 ServerSocket 对应的端口并且接受对该端口的连接socket = ss.accept();// 调用该套接字对象的 getInputStream()方法返回 InputStream 对象以写入从 client 接收到的数据is = socket.getInputStream();// 后面实际就是 IO 流的应用byte[] b = new byte[10];int len;while ((len = is.read(b)) != -1) {// 打印字节流System.out.print(new String(b, 0, len));}// ----------update----------// 反馈一条消息给 Clientos = socket.getOutputStream();os.write("I have received your message".getBytes());// ----------update----------} catch (IOException e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}}
标签:trace 性问题 打印 ace 实现 应用 com family 端口
原文地址:http://www.cnblogs.com/chendifan/p/6602523.html