首页 > 开发 > Java > 正文

Java使用TCP实现数据传输实例详解

2024-07-13 10:08:45
字体:
来源:转载
供稿:网友

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();   } } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表