参考 https://blog.csdn.net/baidu_38591365/article/details/74276249
服务器
public class Service extends Thread{
Socket socket;
public Service(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
while (true) {
String choose = socketReader.readLine();
System.out.println(choose+"服务...");
switch (choose){
case "login" :
login(socketReader, printWriter);
break;
case "register" :
register(socketReader, printWriter);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void login(BufferedReader socketReader, PrintWriter pw) throws IOException {
String userid = socketReader.readLine();
String pass = socketReader.readLine();
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserDao dao = sqlSession.getMapper(UserDao.class);
User user = dao.selectByIdPass(userid,pass);
sqlSession.close();
if (user != null) {
pw.println(user.getName()+"");
} else {
pw.println("登录失败。"+"账号为"+userid+"且密码为"+pass+"的用户不存在,请检查账号密码信息重新登录");
}
}
public static void register(BufferedReader socketReader, PrintWriter pw) throws IOException {
String userid=socketReader.readLine();
String name=socketReader.readLine();
String pass=socketReader.readLine();
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserDao dao = sqlSession.getMapper(UserDao.class);
User user = dao.selectById(userid);
if(user == null) {
dao.insertUser(new User(userid,name,pass));
sqlSession.commit();
pw.println("注册成功!");
}else {
pw.println("注册失败!");
}
sqlSession.close();
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9090);
while (true) {
new Service(serverSocket.accept()).start();
}
}
}
客户端
public class LoginCli {
public String login(String inetAddress, String userid, String pass) throws IOException {
Socket s = new Socket(InetAddress.getByName(inetAddress),9090);
String choose = "login";
PrintWriter pw =new PrintWriter(s.getOutputStream(),true);
pw.println(choose);
pw.println(userid);
pw.println(pass);
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br2.readLine();
s.close();
return str;
}
}
public class RegisterCli {
public String register(String inetAddress, String userid, String name, String pass) throws IOException {
Socket s = new Socket(InetAddress.getByName(inetAddress),9090);
PrintWriter pw =new PrintWriter(s.getOutputStream(),true);
pw.println("register");
pw.println(userid);
pw.println(name);
pw.println(pass);
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br2.readLine();
s.close();
return str+"你注册的信息是,账号:"+userid+",姓名:"+name+",密码:"+pass;
}
}
写进swing
//登录窗口的登录按钮
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String reminder = null; //提示语
String ip = ipField.getText(); //获取就是127.0.0.1这个东西
String userid = textField.getText(); //账号
String pass = String.valueOf(passwordField.getPassword()); //密码
try {
reminder = new LoginCli().login(ip, userid, pass); //登录客户端
} catch (IOException ioException) {
ioException.printStackTrace();
}
if (reminder.length() < 20){ //这个判断的有点草率,因为成功返回的是用户名,不成功返回的是一大长串提示语,不想写了,就先这么草率的搞完得了(看上面的LoginCli的return)
//登录成功
new ClientUI().ui(ip, userid, reminder); //进入客户端窗口
} else {
//登录失败
new ReminderUI().ui(reminder); //进入提示错误窗口
}
jf.dispose();
}
});
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String reminder = null;
try {
reminder = new RegisterCli().register(textField3.getText(), textField1.getText(), textField2.getText(), String.valueOf(passwordField.getPassword()));
} catch (IOException ioException) {
ioException.printStackTrace();
}
new ReminderUI().ui(reminder);
jf.dispose();
}
});
越写重复代码越多,应该是可以更加精简代码(废话),我不会
|