62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test模拟登陆客户端 {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos=null;
DataInputStream dis=null;
System.out.println("-------------开启客户端------------------------------");
try {
//创建Socket对象
Socket client=new Socket("localhost",2234);
//创建User对象
User u1=new User("755199443","12345678");
//获取输出流(对象流)
oos=new ObjectOutputStream(client.getOutputStream());//这里不能加缓冲流包装`
// ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));//加缓冲流
//User对象发到服务器
oos.writeObject(u1);
//获取反响信息(数据流)
dis=new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭
if(oos!=null) {
oos.close();
}
if(dis!=null) {
dis.close();
}
}
System.out.println("-------------结束客户端------------------------------");
}
}
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5292247610512991206L;
private String id;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public User() {
super();
}
public User(String id, String password) {
super();
this.id = id;
this.password = password;
}
}
public class Test模拟登陆服务器端 {
public static void main(String[] args) throws IOException {
System.out.println("-------------开启服务器------------------------------");
//返回信息
String message = null;
ObjectInputStream ois=null;
DataOutputStream dos=null;
try {
//创建ServerSocket对象
ServerSocket ss=new ServerSocket(2234);
//调用accept()方法
Socket server=ss.accept();
//创建输入流(对象流)
// ois=new ObjectInputStream(new BufferedInputStream(server.getInputStream()));//加缓冲流
ois=new ObjectInputStream(server.getInputStream());//不加缓冲流
//获取对象
User u1=(User) ois.readObject();
System.out.println("收到登录请求");
if(u1.getId().equals("755199443")) {
if(u1.getPassword().equals("12345678")) {
message="登录成功";
}else {
message="密码输入错误";
}
}else {
message="无此用户名";
}
//创建输出流(数据流)
dos=new DataOutputStream(server.getOutputStream());
//返回反响信息
dos.writeUTF(message);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
//关闭
if(ois!=null) {
ois.close();
}
if(dos!=null) {
dos.close();
}
}
System.out.println("-------------结束服务器------------------------------");
}
}