作者: | 来源:互联网 | 2023-07-19 14:41
一、TCP实现发送消息客户端代码:***客户端*publicclassTcpClientDemo{publicstaticvoidmain(String[]ar
一、TCP实现发送消息
客户端代码:
public class TcpClientDemo {public static void main(String[] args) {Socket socket = null;OutputStream os = null;try {InetAddress inetAddress = InetAddress.getByName("127.0.0.1");int port = 7777;socket = new Socket(inetAddress,port);os = socket.getOutputStream();Scanner input=new Scanner(System.in);String messge = input.next();os.write(messge.getBytes(StandardCharsets.UTF_8));} catch (Exception e) {e.printStackTrace();}finally {if (socket != null){try {socket.close();} catch (IOException e) {e.printStackTrace();}}if (os != null){try {os.close();} catch (IOException e) {e.printStackTrace();}}}}
}
服务端代码:
public class TcpServerDemo {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream byteArrayOutputStream = null;try {serverSocket = new ServerSocket(7777);while (true) {socket = serverSocket.accept();is = socket.getInputStream();byteArrayOutputStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int length;while ((length = is.read(buffer)) != -1) {byteArrayOutputStream.write(buffer, 0, length);}System.out.println(byteArrayOutputStream.toString());}} catch (Exception e) {e.printStackTrace();}finally {if (serverSocket != null){try {serverSocket.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null){try {socket.close();} catch (IOException e) {e.printStackTrace();}}if (is != null){try {is.close();} catch (IOException e) {e.printStackTrace();}}if (byteArrayOutputStream != null){try {byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}
客户端执行结果:
服务端接受结果:
总结: 看代码说话!
-
客户端
-
服务端
- 建立服务的端口ServerScoket
- 等待用户的连接accept
- 接收用户的消息
二、TCP实现文件上传
public class TcpUplodeFileClient {public static void main(String[] args) throws Exception{Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 7777);OutputStream os = socket.getOutputStream();FileInputStream fis = new FileInputStream(new File("C:\\Users\\大大丁大大\\Desktop\\有用文件\\test.jpg"));byte[] buffer = new byte[1024];int len;while ((len = fis.read(buffer)) != -1) {os.write(buffer, 0, len);}socket.shutdownOutput(); InputStream inputStream = socket.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer2 = new byte[1024];int len2;while ((len2 = inputStream.read(buffer2)) != -1) {baos.write(buffer2, 0, len2);}System.out.println(baos.toString());baos.close();inputStream.close();fis.close();os.close();socket.close();}
}
public class TcpUplodeFileServer {public static void main(String[] args) throws Exception{ServerSocket serverSocket = new ServerSocket(7777);Socket socket = serverSocket.accept();InputStream is = socket.getInputStream();FileOutputStream fos = new FileOutputStream(new File("test.jpg"));byte[] buffer = new byte[1024];int len;while ((len=is.read(buffer)) != -1){fos.write(buffer,0,len);}OutputStream os = socket.getOutputStream();os.write("接收完成".getBytes());serverSocket.close();socket.close();is.close();os.close();}
}
-
客户端执行结果:
-
服务端执行结果
关于TCP的学习就先到此为止啦~
Java网络编程扩展:Java网络编程学习汇总
路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧**转载请注明出处哦**