Java使用TCP实现数据传输

Java SE 专栏收录该内容
43 篇文章 0 订阅

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


  • 8
    点赞
  • 3
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值