资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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实现数据传输实例详解
文章源于:http://cdkjz.cn/article/jdpgio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220