TCP网络编程
TCP网络编程例题
1 import org.junit.Test; 2 import sun.util.locale.provider.JRELocaleProviderAdapter; 3 4 import java.awt.event.InputEvent; 5 import java.io.ByteArrayOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.net.InetAddress; 10 import java.net.ServerSocket; 11 import java.net.Socket; 12 import java.net.UnknownHostException; 13 14 public class TCPTest1 { 15 16 //客户端 17 @Test 18 public void client() throws IOException { 19 20 Socket socket = null; 21 OutputStream os = null; 22 23 try{ 24 //1.创建socket对象,指明服务器端的ip和端口号 25 InetAddress inet = InetAddress.getByName("127.0.0.1"); 26 socket = new Socket(inet, 8899); 27 //2.获取一个输出流,用于输出数据 28 os = socket.getOutputStream(); 29 //3.写出数据的操作 30 os.write("你好,我是客户端".getBytes()); 31 }catch (IOException e){ 32 e.printStackTrace(); 33 }finally { 34 35 //4.资源的关闭 36 if(os != null){ 37 os.close(); 38 } 39 if(socket != null){ 40 socket.close(); 41 } 42 } 43 44 } 45 46 //服务器端 47 @Test 48 public void server() throws IOException { 49 ServerSocket ss = null; 50 Socket socket = null; 51 InputStream is = null; 52 ByteArrayOutputStream baos = null; 53 54 try{ 55 //1.创建服务器端的ServerSocket,指明自己的端口号 56 ss = new ServerSocket(8899); 57 //2.调用accept()表示接收来自客户端的socket 58 socket = ss.accept(); 59 //3.获取输入流 60 is = socket.getInputStream(); 61 //4.读取输入流中的数据 62 baos = new ByteArrayOutputStream(); 63 byte[] buffer = new byte[5]; 64 int len; 65 while((len = is.read(buffer)) != -1){ 66 baos.write(buffer, 0, len); 67 } 68 69 System.out.println(baos.toString()); 70 }catch (IOException e){ 71 e.printStackTrace(); 72 }finally { 73 //5.资源的关闭 74 if(ss != null){ 75 ss.close(); 76 } 77 if(socket != null){ 78 socket.close(); 79 } 80 if(is != null){ 81 is.close(); 82 } 83 if(baos != null){ 84 baos.close(); 85 } 86 } 87 88 } 89 }
例题2:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端,并关闭相应的连接
1 @Test 2 public void client(){ 3 Socket socket = null; 4 OutputStream os = null; 5 FileInputStream fis = null; 6 try { 7 socket = new Socket(InetAddress.getByName("127.0.0.1"),8080); 8 9 os = socket.getOutputStream(); 10 11 fis = new FileInputStream(new File("2390865548.jpg")); 12 13 byte[] buffer = new byte[1024]; 14 int len; 15 while ((len = fis.read(buffer)) != -1){ 16 os.write(buffer,0,len); 17 } 18 19 socket.shutdownOutput(); 20 21 //接收来自于服务器端的数据,并显示到控制台上 22 InputStream is = socket.getInputStream(); 23 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 24 byte[] buffer2 = new byte[1024]; 25 int len2; 26 while ((len2 = is.read(buffer2)) != -1){ 27 baos.write(buffer2,0,len2); 28 } 29 System.out.println(baos.toString()); 30 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } finally { 34 if(fis != null){ 35 try { 36 fis.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 if(os != null){ 42 try { 43 os.close(); 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 } 48 if(socket != null){ 49 try { 50 socket.close(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 // if(baos != null){ 55 // baos.close(); 56 // } 57 } 58 } 59 } 60 @Test 61 public void server(){ 62 ServerSocket ss = null; 63 Socket socket = null; 64 InputStream is = null; 65 FileOutputStream fos = null; 66 try { 67 ss = new ServerSocket(8080); 68 69 socket = ss.accept(); 70 71 is = socket.getInputStream(); 72 73 fos = new FileOutputStream("2390865548(2).jpg"); 74 byte[] buffer = new byte[1024]; 75 int len; 76 while ((len = is.read(buffer)) != -1){ 77 fos.write(buffer,0,len); 78 } 79 //服务器端给予客户端反馈 80 OutputStream os = socket.getOutputStream(); 81 os.write("你好,照片已收到!".getBytes()); 82 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } finally { 86 if(fos != null){ 87 try { 88 fos.close(); 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } 92 } 93 if(is != null){ 94 try { 95 is.close(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 if(socket != null){ 101 try { 102 socket.close(); 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 if(ss != null){ 108 try { 109 ss.close(); 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 } 114 // if (os != null){ 115 // os.close(); 116 // } 117 } 118 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)