📄 connection_net.java
字号:
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.JOptionPane;
import java.util.Date;
public class Connection_net extends Thread {
private Socket netClient;
private Administration admin;
private Vector<User> userOnLine;
private Vector<Chat> userChat;
private ObjectInputStream fromClient;
private PrintStream toClient;
private Object obj;//设置的临时对象
@SuppressWarnings("unchecked")
public Connection_net(Administration ad, Socket client,Vector u, Vector c) {
admin = ad;
netClient = client;
userOnLine = u;
userChat = c;
try {
// 发生双向通信
fromClient = new ObjectInputStream(netClient.getInputStream());// 检索客户输入
toClient = new PrintStream(netClient.getOutputStream());// 服务器写到客户
} catch (IOException e) {
try {
netClient.close();
} catch (IOException e1) {
System.out.println("不能建立流" + e1);
return;
}
}
this.start();//启动线程
}
//处理与客户端的通讯线程
public void run() {
try {
obj = (Object) fromClient.readObject();
if (obj.getClass().getName().equals("User")) {
serverLogin();
}
if (obj.getClass().getName().equals("Register_User")) {
serverRegiste();
}
if (obj.getClass().getName().equals("Message")) {
serverMessage();
}
if (obj.getClass().getName().equals("Chat")) {
serverChat();
}
if (obj.getClass().getName().equals("Exit")) {
serverExit();
}
if (obj.getClass().getName().equals("Password")){
serverPassword();
}
if (obj.getClass().getName().equals("Information")){
serverQueryInf();
}
} catch (IOException e) {
System.out.println(e);
} catch (NullPointerException e) {
System.out.println(e);
} catch (ClassNotFoundException e1) {
System.out.println("读对象发生错误!" + e1);
} finally {
try {
netClient.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
private void serverQueryInf() {
// TODO Auto-generated method stub
try{
Information inf = (Information) obj;
String jdbcDriverName="org.postgresql.Driver";
Driver driver = (Driver) Class.forName(jdbcDriverName).newInstance();
DriverManager.registerDriver(driver);
Connection dbcon = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","123");
Statement st = dbcon.createStatement();
ResultSet rt = st.executeQuery("select password,sex,age,email from user_o where name ='"+inf.getUserPwd()+"';");
String userPwd = rt.getString("password");
String sex = rt.getString("sex");
String age = rt.getString("age");
String email = rt.getString("email");
inf.setUserPwd(userPwd);
inf.setUserGender(sex);
inf.setUserAge(age);
inf.setUserEmail(email);
toClient.println("查询成功");
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "database error!");
}catch(InstantiationException e){
JOptionPane.showMessageDialog(null, "instantiation exception!");
}
catch(IllegalAccessException e){
JOptionPane.showMessageDialog(null, "illegal access exceptino!");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "class not found exception!");
}
}
private void serverPassword() throws IOException{
// TODO Auto-generated method stub
try{
Password pwd = (Password) obj;
String jdbcDriverName="org.postgresql.Driver";
Driver driver = (Driver) Class.forName(jdbcDriverName).newInstance();
DriverManager.registerDriver(driver);
Connection dbcon = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","123");
Statement st = dbcon.createStatement();
if(pwd.getUserPwd().length()==0)
{
toClient.println("密码不能为空!");
}else if(pwd.getUserPwd().equals(pwd.getConfirmPwd())){
st.executeUpdate("update user_o set password ="+pwd.getUserPwd()+" where name = '"+ pwd.getUserName()+"';");
toClient.println("修改成功");
}else
{
toClient.println("两次输入的密码不一致!");
}
st.close();
dbcon.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "database error!");
}catch(InstantiationException e){
JOptionPane.showMessageDialog(null, "instantiation exception!");
}
catch(IllegalAccessException e){
JOptionPane.showMessageDialog(null, "illegal access exceptino!");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "class not found exception!");
}
}
//登录处理
public void serverLogin() throws IOException {
try{
User user = (User) obj;
int find = 0;
int flag = 0;
String jdbcDriverName="org.postgresql.Driver";
Driver driver = (Driver) Class.forName(jdbcDriverName).newInstance();
DriverManager.registerDriver(driver);
Connection dbcon = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","123");
Statement st = dbcon.createStatement();
ResultSet rt = st.executeQuery("select name ,password from user_o");
while(rt.next())
{
String name_user = rt.getString("name");
String password_user = rt.getString("password");
if(name_user.equals(user.getUserName()))
{
if(password_user.equals(user.getUserPwd()))
{
for(int i =0;i <userOnLine.size();i++)
{
if(userOnLine.elementAt(i).getUserName().equals(user.getUserName()))
{
flag = 1;
break;
}
}
if(flag == 1)
{
toClient.println("该户已经登录了!");
}
else
{
toClient.println("登陆成功");
Date date = new Date();
log("用户【" + name_user + "】在" + date.toString()+"登陆成功!");
find = 1;
userOnLine.add(user);
freshServer();
}
}
else
{
toClient.println("密码错误,请重新输入");
}
}
}
if(find == 0)
{
toClient.println("用户名错误,请重新输入");
}
rt.close();
st.close();
dbcon.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "database error!");
}
catch(InstantiationException e){
JOptionPane.showMessageDialog(null, "instantiation exception!");
}
catch(IllegalAccessException e){
JOptionPane.showMessageDialog(null, "illegal access exceptino!");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "class not found exception!");
}
}
//注册处理
public void serverRegiste() throws IOException{
try{
Register_User ruser = (Register_User) obj;
int flag = 0 ;
String jdbcDriverName = "org.postgresql.Driver";
Driver driver = (Driver)Class.forName(jdbcDriverName).newInstance();
DriverManager.registerDriver(driver);
Connection dbcon = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","123");
Statement st = dbcon.createStatement();
ResultSet rs = st.executeQuery("select name from user_o");
while (rs.next())
{
String strname = rs.getString("name");
if(strname.equals(ruser.getCustName()))
{
toClient.println("用户名已经存在,请重新输入!");
flag =1;
}
}
if(flag == 0 )
{
st.executeUpdate("insert into user_o values('"+ruser.getCustName()+"',"+
"'"+ruser.getCustPassword()+"',"+
"'"+ruser.getSex()+"',"+
"'"+ruser.getAge()+"',"+
"'"+ruser.getEmail()+"',"+
"'0',"+
"'2',"+
"''"+
");");
toClient.println("注册成功");
Date date = new Date();
log("用户【" + ruser.custName + "】在" + date.toString()+"注册成功!");
}
rs.close();
st.close();
dbcon.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "database error!");
}
catch(InstantiationException e){
JOptionPane.showMessageDialog(null, "instantiation exception!");
}
catch(IllegalAccessException e){
JOptionPane.showMessageDialog(null, "illegal access exceptino!");
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "class not found exception!");
}
}
//发送信息处理
public void serverMessage() {
try {
Message mess = new Message();
mess.userOnLine = userOnLine;
mess.chat = userChat;
mess.serverMessage = admin.serverMessage;
ObjectOutputStream outputstream = new ObjectOutputStream(netClient
.getOutputStream());
outputstream.writeObject((Message) mess);
netClient.close();
outputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 增加信息处理
public void serverChat() {
Chat cObj = new Chat();
cObj = (Chat) obj;
userChat.addElement((Chat) cObj);
String newLog = admin.taMessage.getText();
newLog = newLog +"\n"+ "【"+cObj.getChatUserName()+"】说:" + cObj.getChatMessage();
admin.taMessage.setText(newLog);
}
// 用户退出处理
public void serverExit() {
Exit exit = new Exit();
exit = (Exit) obj;
removeUser(exit);
Date date = new Date();
log("用户【" + exit.exitname + "】在" + date.toString()+"退出!");
freshServer();
}
private void removeUser(Exit exit) {
Vector<User> vec = new Vector<User>();
User cus = null;
for (int j = 0; j < userOnLine.size(); j++) {
cus = (User) userOnLine.get(j);
if (!exit.exitname.equals(cus.getUserName())) {
vec.add(cus);
}
}
userOnLine.removeAllElements();
for (int j = 0; j < vec.size(); j++) {
cus = (User) vec.get(j);
userOnLine.add(cus);
}
}
public void log(String log){
String newLog = admin.taLog.getText() + "\n" +log;
admin.taLog.setText(newLog);
}
public void freshServer(){
String[] userList = new String[1000];
User cus = null;
for (int j = 0; j < userOnLine.size(); j++) {
cus = (User) userOnLine.get(j);
userList[j] = cus.getUserName();
}
admin.list.setListData(userList);
admin.txtNumber.setText("" + userOnLine.size());
admin.lblUserCount.setText("当前在线人数:" + userOnLine.size());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -