欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

TCP的网络编程

时间:2023-06-09

package Internet;import org.junit.Test;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;public class TCPTest {// 客户端 @Test public void client() throws IOException { Socket socket = null; OutputStream os = null; try { InetAddress inet = InetAddress.getByName("192.168.1.104"); socket = new Socket(inet,8899); os = socket.getOutputStream(); os.write("你好呀我是客户端gg".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { // 资源关闭 if (os != null){ os.close(); } if (socket != null){ socket.close(); } } }// 服务端 @Test public void server() throws IOException { ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { ss = new ServerSocket(8899); socket = ss.accept(); is = socket.getInputStream();// 若构建的数组太小,可能会有乱码// byte[] buffer = new byte[1024];// int len;// while ((len=is.read(buffer))!=-1){// String str = new String(buffer,0,len);// System.out.println(str);// }// 在 baos中有一个可扩容的数组,会自动存取所有字符 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len ; while ((len = is.read(buffer))!=-1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if(baos!=null){ baos.close(); } if(is!=null){ is.close(); } if(socket!=null){ socket.close(); } if(ss!=null){ ss.close(); } } }}

二:

package Internet;import org.junit.Test;import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class TCPTest2 { @Test public void client() throws IOException {// 1. Socket socket = new Socket("127.0.0.1",8080);// 2. OutputStream os = socket.getOutputStream();// 3. FileInputStream fis = new FileInputStream(new File("1.jpg"));// 4. byte[] buffer = new byte[5]; int len ; while ((len = fis.read(buffer))!=-1){ os.write(buffer,0,len); }// 5. fis.close(); os.close(); socket.close(); } @Test public void server() throws IOException { ServerSocket ss = new ServerSocket(8080); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("2.jpg")); byte[] buffer = new byte[5]; int len ; while ((len = is.read(buffer))!=-1){ fos.write(buffer,0,len); } fos.close(); is.close(); socket.close(); ss.close(); }}

三:

package Internet;import org.junit.Test;import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;public class TCPTest3 { @Test public void client() throws IOException {// 1. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8080);// 2. OutputStream os = socket.getOutputStream();// 3. FileInputStream fis = new FileInputStream(new File("1.jpg"));// 4. byte[] buffer = new byte[5]; int len ; while ((len = fis.read(buffer))!=-1){ os.write(buffer,0,len); }// 图片传完了不在输出数据 socket.shutdownOutput();// 5.接受服务器端的数据并显示到控制台上 InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer1 = new byte[5]; int len1 ; while ((len = is.read(buffer1))!=-1){ baos.write(buffer1,0,len); } System.out.println(baos.toString());// 6, fis.close(); os.close(); socket.close(); is.close(); baos.close(); } @Test public void server() throws IOException {// 1. ServerSocket ss = new ServerSocket(8080);// 2. Socket socket = ss.accept();// 3. InputStream is = socket.getInputStream();// 4. FileOutputStream fos = new FileOutputStream(new File("3.jpg"));// 5. byte[] buffer = new byte[5]; int len ;// 由于read时阻塞式的方法,没有明确告诉就不会结束循环。因此需要明确的指示结束(29行) while ((len = is.read(buffer))!=-1){ fos.write(buffer,0,len); }// 6.服务器端给与客户端反馈 OutputStream os = socket.getOutputStream(); // 字符需要转换成字节流 os.write("照片已收到!很漂亮!".getBytes());// 7. fos.close(); is.close(); socket.close(); ss.close(); }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。