1.创建客户端和服务器
客户端输入正确的用户名和密码,服务端会显示
public class loginClient {
public static void main(String[] args) throws IOException {
while(true){
Socket socket = new Socket("DESKTOP-IU5NLRI",10086);
OutputStream outputStream = socket.getOutputStream();
User user =getUser();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(user);
socket.shutdownOutput();
//接受服务器端相响应的结果
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
System.out.println("服务器相应的消息:" + new String(buffer,0,length));
socket.shutdownInput();
//关闭资源
inputStream.close();
objectOutputStream.close();
objectOutputStream.close();
}
}
public static User getUser(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String pwd = scanner.next();
User user = new User(username,pwd);
return user;
}
}
public class loginServer {
public static void main(String[] args) throws IOException,ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(10086);
System.out.println("*****************服务器已开启... ...*********************");
while(true){
Socket server = serverSocket.accept();
InputStream inputStream = server.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user = (User)objectInputStream.readObject();
String str = "";
if("wzry".equals(user.getUsername()) && "123456".equals(user.getPassword())){
str = "登录成功!";
System.out.println("欢迎用户 【" + user.getUsername() + "】 登录系统!");
}else{
str = "登录失败!";
}
server.shutdownInput();
OutputStream outputStream = server.getOutputStream();
outputStream.write(str.getBytes());
server.shutdownOutput();
//关闭资源
outputStream.close();
objectInputStream.close();
inputStream.close();
server.close();
}
}
}
运行图
?
注:这时程序并没有停止?,需手动停止
优化一下服务端
客户端
public class loginClient2 {
public static void main(String[] args) throws IOException {
while(true){
Socket socket = new Socket("DESKTOP-IU5NLRI",10086);
OutputStream outputStream = socket.getOutputStream();
User user =getUser();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(user);
socket.shutdownOutput();
//接受服务器端相响应的结果
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int length = inputStream.read(buffer);
System.out.println("服务器相应的消息:" + new String(buffer,0,length));
socket.shutdownInput();
//关闭资源
inputStream.close();
objectOutputStream.close();
objectOutputStream.close();
}
}
public static User getUser(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String pwd = scanner.next();
User user = new User(username,pwd);
return user;
}
}
服务端
public class loginServer2 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10086);
System.out.println("*****************服务器已开启... ...*********************");
while(true){
Socket socket = serverSocket.accept();
loginThread2 loginThread = new loginThread2(socket);
new Thread(loginThread).start();
}
}
}
public class loginThread2 implements Runnable{
private Socket socket;
public loginThread2(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream = null;
ObjectInputStream objectInputStream = null;
OutputStream outputStream = null;
try {
inputStream = socket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
User user = (User)objectInputStream.readObject();
String str = "";
if("wzry".equals(user.getUsername()) && "123456".equals(user.getPassword())){
str = "登录成功!";
System.out.println("欢迎用户 【" + user.getUsername() + "】 登录系统!");
}else{
str = "登录失败!";
}
socket.shutdownInput();
outputStream = socket.getOutputStream();
outputStream.write(str.getBytes());
socket.shutdownOutput();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
//关闭资源
outputStream.close();
objectInputStream.close();
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
现在让他通信后结束运行
public class udpClient {
public static void main(String[] args) throws IOException {
//创建DatagramSocket对象
DatagramSocket datagramSocket = new DatagramSocket();
//发送数据
System.out.println("请输入要发送的信息:");
//定义数据包
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
//getBytes 使用平台的默认字符集将这个(@code String)编码为字节序列,并将结果存储到一个新的字节数组中。
DatagramPacket datagramPacket = new DatagramPacket(s.getBytes(),s.getBytes().length,InetAddress.getByName("DESKTOP-IU5NLRI"),10086);
//发送数据包给服务器
datagramSocket.send(datagramPacket);
//关闭资源
datagramSocket.close();
}
}
public class udpClient {
public static void main(String[] args) throws IOException {
//创建DatagramSocket对象
DatagramSocket datagramSocket = new DatagramSocket();
//发送数据
System.out.println("请输入要发送的信息:");
//定义数据包
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
//getBytes 使用平台的默认字符集将这个(@code String)编码为字节序列,并将结果存储到一个新的字节数组中。
DatagramPacket datagramPacket = new DatagramPacket(s.getBytes(),s.getBytes().length,InetAddress.getByName("DESKTOP-IU5NLRI"),10086);
//发送数据包给服务器
datagramSocket.send(datagramPacket);
//关闭资源
datagramSocket.close();
}
}
?
实现图片传输
import java.io.*;
import java.net.Socket;
public class picClient {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("1.jpg");
//创建Socket套接字对象
Socket client = new Socket("DESKTOP-IU5NLRI",10001);
//从socket中获取输入流
OutputStream outputStream = client.getOutputStream();
//读数据,向服务器写数据
int read = 0;
while((read = fileInputStream.read()) != -1){
outputStream.write(read);
}
//截断socket中的输出流
client.shutdownOutput();
//读取服务器响应的数据
InputStream inputStream = client.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
String a = dataInputStream.readUTF();
System.out.println("服务器响应的消息:" + a);
client.shutdownOutput();
//关资源
dataInputStream.close();
inputStream.close();
fileInputStream.close();
outputStream.close();client.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class picServer {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("bajie.jpg");
//创建ServerSocket对象
ServerSocket serverSocket = new ServerSocket(10001);
System.out.println("*****************服务器已开启... ...*********************");
//开启监听,获取socket对象
Socket server = serverSocket.accept();
//从socket中读取客户端上传的图片数据
InputStream inputStream = server.getInputStream();
//读取数据,写入服务器端
int length = 0;
byte[] buffer = new byte[1024];
while((length = inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer,0,length);
}
server.shutdownInput();
//响应客户端一条消息
OutputStream outputStream = server.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF("上传成功!");
server.shutdownOutput();
//关资源
dataOutputStream.close();
outputStream.close();
inputStream.close();
fileOutputStream.close();
server.close();
serverSocket.close();
}
}
|