首先是客户端:
package client;
import domin.User;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {//客户端
public static void main(String[] args) throws IOException {
//创建套接字:指定服务器ip和端口号
Socket s = new Socket("192.168.163.1",8080);
//向服务器发送数据
OutputStream os = s.getOutputStream();
/*DataOutputStream dos = new DataOutputStream(os);//OutputStream不能直接向外发生数据,因此我在它的外面套了一个DataOutputStream处理流
dos.writeUTF("hello,我是客户端1号....");
*/
//录入用户的账号和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的账号");
String name = sc.next();
System.out.println("请输入您的密码");
String password = sc.next();
//把name和password封住成一个对象,方便输入给服务器端
User user = new User();
user.setName(name);
user.setPassword(password);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(user);//给服务器端写回一个对象
//接收服务端的回话
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
/* String str = dis.readUTF();//读取字符串
System.out.println("服务器对我说:"+str);*/
Boolean flag = dis.readBoolean();
if(flag)
{
System.out.println("密码和账号正确");
}
else
{
System.out.println("密码和账号不正确");
}
//关闭流+关闭网络资源
s.close();
os.close();
oos.close();
is.close();
dis.close();
}
}
服务器端:
package server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException{
//创建套接字:指定服务器的端口号
ServerSocket serverSocket = new ServerSocket(8080);
//等着客户端发来的信息
Socket socket = null;
int count = 0;
try {
while (true) {
socket = serverSocket.accept();//一直阻塞获取,没有得到信息,就一直卡着
new ServerThread(socket).start();
count++;
System.out.println("这是第"+count+"客户端");
}
}catch (IOException e)
{
e.printStackTrace();
}
//关闭流
serverSocket.close();
socket.close();
}
}
因为要对一个use对象进行验证,因此写一个类
package domin;
import java.io.Serializable;
public class User implements Serializable {//进行网络和io流传送的时候,必须进行序列化
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
因为要实现多线程访问,因此肯定要写一个线程类
package server;
import domin.User;
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket = null;
public ServerThread(Socket socket ) throws IOException {
this.socket = socket;
}
InputStream is = null ;
ObjectInputStream objectInputStream =null;
OutputStream os = null;
DataOutputStream dos = null;
@Override
public void run() {
try {
is = socket.getInputStream();
objectInputStream = new ObjectInputStream(is);
//读取对象
User user = null;
try {
user = (User) objectInputStream.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Boolean flag = false;
if (user.getName().equals("GG") && user.getPassword().equals("123")) {
flag = true;//在服务器端得到了验证
}
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeBoolean(flag);//给客户端写回标记位
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if(is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(objectInputStream != null)
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(dos != null)
{
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果图如下:
服务器端因为在一直死循环获取,因此肯定不会停止运行。