Java网络编程TCP协议发送接收数据

一、客户端发送,服务器端接收

package net;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * TCP协议接收数据:
 * A:创建接收端Socket对象
 * B:监听客户端连接,返回一个对应的Socket对象
 * C:获取输入流,在控制台显示数据
 * D:释放资源
 */
public class ServerDemo {
	public static void main(String[] args) throws IOException {
		ServerSocket ss = new ServerSocket(10010);
		
		Socket s = ss.accept();
		
		InputStream is = s.getInputStream();
		byte[] bys = new byte[1024];
		int len = is.read(bys);
		String str = new String(bys, 0, len);
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"————"+str);
		
		s.close();
	}
}
package net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/*
 * TCP协议发送数据:
 * A:创建发送端的Socket对象
 * B:获取输出流,写数据
 * C:释放资源
 */
public class ClientDemo {
	public static void main(String[] args) throws IOException {
		Socket s = new Socket("192.168.1.22",10010);
		
		OutputStream os = s.getOutputStream();
		os.write("Hello TCP!".getBytes());
		
		s.close();
	}
}

二、客户端从键盘录入数据,服务器端输出到控制台

package net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientDemo {
	public static void main(String[] args) throws IOException {
		//创建客户端Socket对象
		Socket s = new Socket("192.168.1.22",10010);
		//键盘录入数据
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//包装通道内的流
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		
		String line = null;
		while((line = br.readLine()) != null){
			if("jieshu".equals(line)){
				break;
			}
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		
		br.close();
		s.close();
	}
}
package net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		//创建服务器端Socket对象
		ServerSocket ss = new ServerSocket(10010);
		//监听客户端连接
		Socket s = ss.accept();
		//包装通道内的流
		BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line = null;
		while((line = br.readLine()) != null){
			System.out.println(line);
		}
		
		s.close();
	}
}

三、客户端从键盘录入数据,服务器端输出到文本文件

客户端代码同二

服务器端:

package net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		//创建服务器端Socket对象
		ServerSocket ss = new ServerSocket(10010);
		//监听客户端连接
		Socket s = ss.accept();
		//包装通道内的流
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		//封装文本文件
		BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
		
		String line = null;
		while((line = br.readLine()) != null){
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		
		bw.close();
		s.close();
	}
}

三、客户端读文本文件,服务器端输出到控制台

客户端:

package net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientDemo {
	public static void main(String[] args) throws IOException {
		//创建客户端Socket对象
		Socket s = new Socket("192.168.1.22",10010);
		//封装文本文件
		BufferedReader br = new BufferedReader(new FileReader("fos.txt"));
		//包装通道内的流
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		
		String line = null;
		while((line = br.readLine()) != null){
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		
		br.close();
		s.close();
	}
}

服务器端代码同二

四、客户端上传文本文件,服务器端给出反馈

package net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		//创建服务器端Socket对象
		ServerSocket ss = new ServerSocket(10010);
		//监听客户端连接
		Socket s = ss.accept();
		//包装通道内的流
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		//封装文本文件
		BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
		
		String line = null;
		while((line = br.readLine()) != null){
			//此处当服务器读到"over"时,跳出循环,否则不会读到null,一直在循环内
			if("over".equals(line)){
				break;
			}
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		
		//给出反馈
		BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		bwServer.write("文件上传成功");
		bwServer.newLine();
		bwServer.flush();
		
		bw.close();
		s.close();
	}
}

五、客户端上传图片,服务器端给出反馈

package net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ClientDemo {
	public static void main(String[] args) throws IOException {
		//创建客户端Socket对象
		Socket s = new Socket("192.168.1.22",10010);
		//封装图片文件
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("tupian.jpg"));
		//封装通道内的流
		BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
		
		byte[] bys = new byte[1024];
		int len = 0;
		while((len = bis.read(bys)) != -1){
			bos.write(bys, 0, len);
		}
		
		s.shutdownOutput();
		
		//接收反馈
		InputStream is = s.getInputStream();
		byte[] bys2 = new byte[1024];
		int len2 = is.read(bys2);
		String client = new String(bys2, 0, len2);
		System.out.println(client);
		
		bis.close();
		s.close();
	}
}
package net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		//创建服务器端Socket对象
		ServerSocket ss = new ServerSocket(10010);
		//监听客户端连接
		Socket s = ss.accept();
		//封装通道内的流
		BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
		//封装图片文件
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("aaa.jpg"));
		
		byte[] bys = new byte[1024];
		int len = 0;
		while((len = bis.read(bys)) != -1){
			bos.write(bys, 0, len);
		}
		
		//给出反馈
		OutputStream os = s.getOutputStream();
		
		os.write("图片上传成功".getBytes());
		
		bos.close();
		s.close();
	}
}

猜你喜欢

转载自blog.csdn.net/weufengwangshi_/article/details/81713223
0条评论
添加一条新回复