TCP网络编程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | public class TCPTest { @Test public void client() { Socket socket = null ; OutputStream os = null ; try { //1.创建Socket对象,指明服务器端的ip和端口号 InetAddress inet = InetAddress.getByName( "127.0.0.1" ); socket = new Socket(inet, 8899 ); //2.获取一个输出流,用于输出数据 os = socket.getOutputStream(); //3.写出数据的操作 os.write( "hello哈哈哈" .getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { //4.资源关闭 if (os != null ) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null ) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void server(){ ServerSocket serverSocket = null ; Socket socket = null ; InputStream is = null ; ByteArrayOutputStream baos = null ; try { //1.创建服务器端的ServerSocket,指明自己的端口号 serverSocket = new ServerSocket( 8899 ); //调用accept()表示接收来自于客户端的socket socket = serverSocket.accept(); //3.获取输入流 is = socket.getInputStream(); //4.读取输入流中的数据 baos = new ByteArrayOutputStream(); byte [] bcuf = new byte [ 5 ]; int len; while ((len = is.read(bcuf)) != - 1 ) { baos.write(bcuf, 0 , len); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (baos!= null ){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos!= null ){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos!= null ){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos!= null ){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class TCPTest2 { @Test public void client() throws IOException { InetAddress inet = InetAddress.getByName( "127.0.0.1" ); Socket socket = new Socket(inet, 8899 ); OutputStream os = socket.getOutputStream(); File file = new File( "hello.txt" ); FileInputStream fis = new FileInputStream(file); byte [] bytes = new byte [ 1024 ]; int len; while ((len=fis.read(bytes))!=- 1 ){ os.write(bytes, 0 ,len); } socket.shutdownOutput(); fis.close(); os.close(); socket.close(); } @Test public void server() throws IOException { FileOutputStream fos = new FileOutputStream( "hello3.txt" ); ServerSocket ss = new ServerSocket( 8899 ); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); byte [] bytes = new byte [ 1024 ]; int len; while ((len=is.read(bytes))!=- 1 ){ fos.write(bytes, 0 ,len); } is.close(); socket.close(); fos.close(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律