


这篇文章主要介绍了Java使用TCP实现数据传输实例详解的相关资料,需要的朋友可以参考下
Java使用TCP实现数据传输实例详解
TCP所提供服务的主要特点:
1.面向连接的传输;
2.端到端的通信;
3.高可靠性,确保传输数据的正确性,不出现丢失或乱序;
4.全双工方式传输;
5.采用字节流方式,即以字节为单位传输字节序列;
6.紧急数据传送功能。
TCP传输需要建立客户端和服务器端,即Socket和Server Socket , 建立连接后,通过Socket中的IO流进行数据的传输 。传输结束后关闭Socket。
客户端和服务器端是两个独立的应用程序。
以下是实现基本的TCP数据传输的Demo code:
package javase.day18; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class TransTextDemo { public static void main(String[] args) { } } class TcpClient2{ public static void main(String[] args) throws Exception{ System.out.println("client start..."); Socket s = new Socket("192.168.1.2",10005); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String sendDataStr=null; while((sendDataStr=br.readLine())!=null){ if(sendDataStr.equals("bye")){ break; } bw.write(sendDataStr); bw.newLine(); bw.flush(); } bw.close(); } } class TcpServer2{ public static void main(String[] args) throws Exception{ System.out.println("server start..."); ServerSocket ss = new ServerSocket(10005); Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String receiveDataStr=null; while((receiveDataStr=br.readLine())!=null){ if(receiveDataStr.equals("bye")){ break; } System.out.println("receive data:"+receiveDataStr); } } }
使用TCP实现文本文件上传的Demo code:
package javase.day18; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class UploadText { public static void main(String[] args) { // TODO Auto-generated method stub } } class TextServer{ public static void main(String[] args) throws Exception{ System.out.println("Server start..."); ServerSocket ss = new ServerSocket(10008); Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter(new FileWriter("C:\\java_test\\server.txt"),true); String line=null; while((line=br.readLine())!=null){ System.out.println(line); out.println(line); } PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); pw.println("upload successful"); pw.close(); s.close(); ss.close(); } } class TextClient{ public static void main(String[] args) throws Exception{ System.out.println("Client start..."); Socket clientSocket = new Socket("192.168.1.2",10008); BufferedReader br = new BufferedReader(new FileReader("C:\\java_test\\SystemDemo.java")); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true); String line=null; while((line=br.readLine())!=null){ System.out.println(line); out.println(line); } clientSocket.shutdownOutput(); BufferedReader br2 = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String confirmMsgStr=br2.readLine(); System.out.println(confirmMsgStr); br2.close(); clientSocket.close(); } }
实现图片上传的Demo code:
package javase.day18; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.ServerSocket; import java.net.Socket; public class UploadImage { public static void main(String[] args) { } } class ImageClient{ public static void main(String[] args) throws Exception{ Socket imageClientSocket = new Socket("192.168.1.2",10010); BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\java_test\\client\\生活用品01.jpg")); BufferedOutputStream bos = new BufferedOutputStream(imageClientSocket.getOutputStream()); byte[] buf = new byte[1024*100]; int len = 0 ; while((len=bis.read(buf))!=-1){ bos.write(buf,0,len); } imageClientSocket.shutdownOutput(); BufferedInputStream bis2 = new BufferedInputStream(imageClientSocket.getInputStream()); byte[] buf2 = new byte[1024]; int len2 = 0 ; while((len2=bis2.read(buf2))!=-1){ System.out.println(new String(buf2,0,len2)); } bis.close(); imageClientSocket.close(); } } class ImageServer{ public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(10010); Socket s = ss.accept(); BufferedInputStream bis = new BufferedInputStream(s.getInputStream()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\java_test\\server\\生活用品01.jpg")); byte[] buf = new byte[1024*100]; int len = 0 ; while((len=bis.read(buf))!=-1){ bos.write(buf, 0, len); } BufferedOutputStream bos2 = new BufferedOutputStream(s.getOutputStream()); bos2.write("upload successful".getBytes()); bos2.close(); s.close(); ss.close(); } }
The above is the detailed content of Detailed explanation of Java's use of TCP to implement data transmission examples. For more information, please follow other related articles on the PHP Chinese website!

很多朋友朋友在更换苹果手机时,想把旧手机内的所有数据全部导入到新手机里,理论上是完全可行的,但在实际操作中是无法做到“转移所有”数据的,本期文章列举几种“转移部分数据”的方法。一、iTunesiTunes是苹果手机预装的软件,可用于迁移旧手机中的所有数据,但需要与电脑配合使用。可通过在电脑上安装iTunes,然后通过数据线连接手机和电脑,使用iTunes进行备份手机内的应用和数据,最后将备份恢复到新的苹果手机上来完成迁移。二、iCloudiCloud是苹果专属的“云空间”工具,可以先在旧手机内登

ReactAPI调用指南:如何与后端API进行交互和数据传输概述:在现代的Web开发中,与后端API进行交互和数据传输是一个常见的需求。React作为一个流行的前端框架,提供了一些强大的工具和功能来简化这一过程。本文将介绍如何使用React来调用后端API,包括基本的GET和POST请求,并提供具体的代码示例。安装所需的依赖:首先,确保在项目中安装了Axi

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

随着科技的发展,网络通信已经成为了现代社会信息传输的重要工具之一。但同时,网络上的信息传输面临着被恶意攻击和窃取的风险,因此安全性显得尤为重要。基于此,HTTPS协议就应运而生。它是在HTTP协议上加入SSL/TLS加密的方式来保证网络传输安全性的一种协议。Java作为一门广泛应用于网络开发的语言,自然也提供了丰富的API来支持HTTPS协议。本文将

PHPtraitDTO:优化数据传输过程的关键工具,需要具体代码示例概述:在PHP开发中,数据传输是一项非常常见的任务,例如将数据从控制器传递给视图、将数据从接口传递给前端等。然而,在传输数据的过程中,往往需要对数据进行处理、转换和封装,这可能会导致代码冗余和不易维护。为了解决这个问题,我们可以使用PHPtraitDTO(DataTransfer

PHPtraitDTO:优化数据传输过程的关键利器,需要具体代码示例简介:在开发过程中,数据传输是一个非常常见的需求,尤其是在不同层级之间传递数据时。在传输这些数据过程中,我们常常需要对数据进行处理、验证或者转换,以满足不同的业务需求。为了提高代码的可读性和可维护性,我们可以使用PHPtraitDTO(DataTransferObject)来优化

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!
