首页 > Java > java教程 > 详解Java使用TCP实现数据传输实例

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

零下一度
发布: 2017-06-17 13:13:46
原创
1673 人浏览过

这篇文章主要介绍了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(); 
  } 
}
登录后复制

以上是详解Java使用TCP实现数据传输实例的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板