java基础—网络编程——TCP客户端与服务端交互




import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;

import org.omg.CORBA_2_3.portable.OutputStream;


public class ServerSocketDemo {

	public static void main(String[] args) throws Exception {
		/**
		 * 客户端(Client)首先与服务端(Server)建立连接,形成通道,然后中,数据就可以在通道之间进行传输,并且单个Server端可以同时与多个Client端建立连接*/

		System.out.println("客户端启动");
		System.out.println(InetAddress.getLocalHost().getHostAddress());//192.168.3.4
		Socket socket = new Socket("192.168.3.4",10002);
		java.io.OutputStream out = socket.getOutputStream();
		out.write("我又来了".getBytes());
		socket.close();
	}

}

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


public class ServerSocketDemo2 {

	public static void main(String[] args) throws Exception 
	{
		// TODO Auto-generated method stub

		System.out.println("服务端启动");
		ServerSocket ss = new ServerSocket(10002);
		socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		//读取客户端的数据
		InputStream in = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		String text = new String(buf,0,len);
		System.out.println(ip+"  "+text);
		ss.close();
		s.close();
		
		//TCP协议传输数据必须先开服务端,再开客户端,否则 客户端根本连接不上服务端
	}

}

运行程序:



客户端接收服务端返回的消息


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

//建立TCP客户端
public class ServerSocketDemo3 
{

	public static void main(String[] args ) throws Exception
	{
		System.out.println("TCP协议服务端和客户端交互\n客户端启动");
		//建立TCP客户端
		Socket socket = new Socket("192.168.3.4",10002);
		
		OutputStream out = socket.getOutputStream();
		
		out.write("我来了".getBytes());
		//读取服务端返回的数据
		InputStream in = socket.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		String text = new String(buf,0,len);
		System.out.println("客户端:"+text);
		socket.close();
	}
}

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


public class ServerSocketDemo4 {

	public static void main(String[] args) throws Exception {
		// 建立TCP服务端
		System.out.println("服务端启动");
		ServerSocket ss = new ServerSocket(10002);
		Socket s = ss.accept();
		
		String ip = s.getInetAddress().getHostAddress();
		InputStream in = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		String text = new String(buf,0,len);
		System.out.println(ip+"  "+text);
		
		OutputStream out = s.getOutputStream();
		out.write("数据传输成功".getBytes());
		s.close();
		ss.close();
		
		

	}

}


客户端输入小写字母,发送到服务端
用血端将小写字母转换成大写字母返回客户端

双窗口


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

//建立TCP客户端
public class ServerSocketDemo3 
{

	public static void main(String[] args ) throws Exception
	{
		System.out.println("TCP协议服务端和客户端交互\n客户端启动");
		//建立TCP客户端
		Socket socket = new Socket("192.168.3.4",10002);
		//获取键盘录入的数据
			BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
			//输出流
			BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
			String len = null;
			
			//读取服务端返回的数据
			BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			
			while((len = bufr.readLine())!=null)
			{
				if("over".equals(len))
					break;
				out.println(len);
				String s = bufin.readLine();
				System.out.println(s);
			}
			
		
			
		}
		
		
	}

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


public class ServerSocketDemo4 {

	public static void main(String[] args) throws Exception {
		// 建立TCP服务端
		System.out.println("服务端启动");
		ServerSocket ss = new ServerSocket(10002);
		Socket s = ss.accept();
		
		String ip = s.getInetAddress().getHostAddress();
	     
		BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		String lin = null;
		while((lin=bufr.readLine())!=null)
		{
			System.out.println(lin);
			out.println(lin.toUpperCase());
		}
		s.close();
		ss.close();
		
		

	}

}


通过TCP上传文档:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


//通过Tcp上传图片
//客户端
public class ServerSocketDemo5 {

	public static void main(String[] args) throws Exception {
		String s = "C:\\Users\\Administrator\\Desktop\\测试文件夹\\112.java";
		TcpReaderFile(s);

	}

	private static void TcpReaderFile(String s) throws Exception {
		// TODO Auto-generated method stub
		File file = new File(s);
		
		Socket sk = new Socket("192.168.3.4",10007);
		
		BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		
		PrintWriter out = new PrintWriter(sk.getOutputStream(),true);
		
	
		
		BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
		
		String len = null;
		while((len=bfr.readLine())!=null)
		{
			out.println(len);
		}
		sk.shutdownOutput();
		
		BufferedReader bufrd = new BufferedReader(new InputStreamReader(sk.getInputStream()));
		String str = bufrd.readLine();
		System.out.println(str);
		}

}

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

//上传文件服务端

public class ServerSocketDemo6 {
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10007);
		Socket s = ss.accept();
		BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
		BufferedWriter bfwr = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\测试文件夹\\aaa.txt"));
		String str = null;
		while((str=bufr.readLine())!=null)
		{
			bfwr.write(str);
			bfwr.newLine();
			bfwr.flush();
		}
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.println("上传成功");
		
	}

}

运行程序






上传mp3 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


//通过Tcp上传图片,mp3
//客户端
public class ServerSocketDemo5 {

	public static void main(String[] args) throws Exception {
		String s = "C:\\Users\\Administrator\\Desktop\\测试文件夹\\凤舞九天.mp3";
		TcpReaderFile(s);

	}

	private static void TcpReaderFile(String s) throws Exception 
	{
		// TODO Auto-generated method stub
		File file = new File(s);
		
		Socket sk = new Socket("192.168.3.4",10007);
		
		BufferedInputStream bfr = new BufferedInputStream(new FileInputStream(file));
		
		BufferedOutputStream bos = new BufferedOutputStream(sk.getOutputStream());
		
	
		byte[] b = new byte[1024];
		int len = 0;
		while((len=bfr.read(b))!=-1)
		{
			bos.write(b);
			bos.flush();
		}
		sk.shutdownOutput();
		
		BufferedReader bufrd = new BufferedReader(new InputStreamReader(sk.getInputStream()));
		String str = bufrd.readLine();
		System.out.println(str);
		}

}

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

//上传文件服务端

public class ServerSocketDemo6 {
	public static void main(String[] args) throws Exception
	{
		String f = "C:\\Users\\Administrator\\Desktop\\测试文件夹\\aaa.mp3";
		OutputFile(f);
		
		
	}

	private static void OutputFile(String f) throws Exception {
		// TODO Auto-generated method stub
		File file = new File(f);
		ServerSocket ss = new ServerSocket(10007);
		Socket s = ss.accept();
		BufferedInputStream bufr =  new BufferedInputStream(s.getInputStream());
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
		byte[] b = new byte[1024];
		int str = 0;
		while((str=bufr.read(b))!=-1)
		{
			bos.write(b);
			bos.flush();
		}
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.println("上传成功");
	}

}



早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页