欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java tcp socket客户端和服务端

程序员文章站 2024-03-23 09:36:10
...

传字符串

package study_04;

import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
	public static void main(String[] args) throws Exception {
		System.out.println("--------Server---------");
		ServerSocket server = new ServerSocket(7777);//开启服务端
		Socket client = server.accept();//阻塞 等待接收客户端
		System.out.println("有新的客户端连接");
		DataInputStream dis = new DataInputStream(client.getInputStream());
		String msg = dis.readUTF();
		System.out.println(msg);
		dis.close();
		client.close();
	}
}
package study_04;

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

public class SocketClient {
	public static void main(String[] args) throws Exception, IOException {
		System.out.println("--------Client---------");
		Socket client = new Socket("localhost",7777);//开启客户端
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
		String msg = "测试";
		dos.writeUTF(msg);
		dos.flush();
		dos.close();
		client.close();		
	}
}
--------Server---------
有新的客户端连接
测试
--------Client---------

登录验证

package study_04;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTowWayServer {
	public static void main(String[] args) throws Exception {
		System.out.println("--------Server---------");
		ServerSocket server = new ServerSocket(7777);//开启服务端
		Socket client = server.accept();//阻塞 等待接收客户端
		System.out.println("有新的客户端连接");
		DataInputStream dis = new DataInputStream(client.getInputStream());
		String msg = dis.readUTF();
		String[] userInfo = msg.split("&");
		String username = "";
		String password = "";
 		for (String string : userInfo) {
			String[] info = string.split(":");
			if (info[0].equals("用户名")) {
				username = info[1];
			}else if (info[0].equals("密码")) {
				password = info[1];
			}
		}
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
 		if (username.equals("dan") && password.equals("123")) {
			dos.writeUTF("登录成功");
		}else {
			dos.writeUTF("登录失败");
		}
 		dos.flush();
		dis.close();
		client.close();
	}
}
package study_04;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class SocketTowWayClient {
	public static void main(String[] args) throws Exception, IOException {
		System.out.println("--------Client---------");
		Socket client = new Socket("localhost",7777);//开启客户端
		DataOutputStream dos = new DataOutputStream(client.getOutputStream());
		System.out.println("请输入用户名:");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入密码:");
		BufferedReader reader2 = new BufferedReader(new InputStreamReader(System.in));
		String username = reader.readLine();
		String password = reader2.readLine();
		System.out.println("用户名:"+ username +"\t密码:"+ password);
		dos.writeUTF("用户名:"+ username + "&" + "密码:"+ password);
		dos.flush();
		DataInputStream dis = new DataInputStream(client.getInputStream());
		System.out.println(dis.readUTF());
		dos.close();
		client.close();		
	}
}

传文件

package study_04;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

public class SocketFileServer {
	public static void main(String[] args) throws Exception {
		System.out.println("--------Server---------");
		ServerSocket server = new ServerSocket(7777);//开启服务端
		Socket client = server.accept();//阻塞 等待接收客户端
		System.out.println("有新的客户端连接");
		InputStream is = new BufferedInputStream(client.getInputStream());//输入流
		OutputStream os = new BufferedOutputStream(new FileOutputStream("2.jpg"));//
		int len = -1;
		byte[] flush = new byte[1024 * 10];
		//保存文件
		while ((len = is.read(flush)) != -1) {
			os.write(flush,0,len);
		}
		os.flush();
		os.close();
		client.close();
	}
}
package study_04;

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

public class SocketFileClient {
	public static void main(String[] args) throws Exception, IOException {
		System.out.println("--------Client---------");
		Socket client = new Socket("localhost",7777);//开启客户端
		InputStream is = new BufferedInputStream(new FileInputStream("1.jpg"));//加载到缓冲流
		OutputStream os = new BufferedOutputStream(client.getOutputStream());
		byte[] flush = new byte[1024 * 10];
		int len = -1;
		//打开文件
		while ((len = is.read(flush)) != -1) {
			os.write(flush,0,len);
		}
		os.flush();
		is.close();
		client.close();		
	}
}

 

相关标签: java