TCP通信 练习
1 package com.yhqtv.demo01; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.Socket; 7 8 /* 9 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 10 * @create 2020-05-14-7:57 11 * 12 */ 13 public class TCPClient { 14 public static void main(String[] args) throws IOException { 15 Socket s=new Socket("127.0.0.1",8888); 16 17 OutputStream os = s.getOutputStream(); 18 os.write("鑫淼的世界".getBytes()); 19 20 // s.getOutputStream().write("鑫淼的世界".getBytes());//对上面两行改写 21 22 InputStream is = s.getInputStream(); 23 24 byte[] bytes=new byte[1024]; 25 int len=is.read(bytes); 26 System.out.println(new String(bytes,0,len)); 27 28 s.close(); 29 30 } 31 }
1 package com.yhqtv.demo01; 2 3 import javax.net.ssl.SSLServerSocket; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 10 /* 11 * @author XMKJ yhqtv.com Email:yhqtv@qq.com 12 * @create 2020-05-14-8:12 13 * 14 */ 15 public class TCPServer { 16 public static void main(String[] args) throws IOException { 17 ServerSocket ss=new ServerSocket(8888); 18 Socket ac = ss.accept(); 19 InputStream is = ac.getInputStream(); 20 byte[] bytes=new byte[1024]; 21 int len=is.read(bytes); 22 System.out.println(new String(bytes,0,len)); 23 24 OutputStream os = ac.getOutputStream(); 25 os.write("收到信息".getBytes()); 26 27 ac.close(); 28 ss.close(); 29 30 31 } 32 }
个人官网yhqtv.com

浙公网安备 33010602011771号