6.ServerRegThread类[code=java]package userreg;
import java.io.*;
import java.net.*;
public class ServerRegThread implements Runnable {
ServerSocket r_socket=null;
public void run() {
try {
r_socket=new ServerSocket(9876);
while(true){
Socket socket = r_socket.accept();
UserRegThread user = new UserRegThread(socket);
new Thread(user).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}[/code]7.UserRegThread类[code=java]package userreg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dao.UserDAO;
import dbutil.ConnectionManager;
public class UserRegThread implements Runnable {
DataInputStream dis;
DataOutputStream dos;
Socket socket;
String msgs[]=new String[3];
public UserRegThread(Socket socket){
this.socket = socket;
}
public void run() {
try {
boolean flag = true;
while(flag){
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
int i = 0;
while(true){
if(i==3) break;
String msg = dis.readUTF();//这里有异常-************************
msgs = msg;
i++;
}
int num = UserDAO.saveUser(msgs);
if(num==1){
dos.writeUTF("OK");
break;
}else if(num!=1){
dos.writeUTF("NO");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}[/code]8.ServerChat类[code=java]package util;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import userlogin.ServerThread;
import userreg.ServerRegThread;
public class ServerChat extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static Vector<ServerThread> vec_Threads=new Vector<ServerThread>();
public static Vector<HashMap<String, String>> vec_Users = new Vector<HashMap<String, String>>();
public ServerChat(){
init();
}
public void init(){
this.setVisible(true);
this.setTitle("服务器");
this.setBounds(400,300,200,140);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
JButton exit = new JButton("退出");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
this.add(exit);
this.validate();
}
public static void main(String[] args){
new ServerChat();
ServerSocket s_socket=null;
try {
new Thread(new ServerRegThread()).start();
s_socket=new ServerSocket(8765);
} catch (IOException e) {
e.printStackTrace();
}
try{
while(true){
Socket sc=s_socket.accept();
ServerThread st=new ServerThread(sc,vec_Threads);
if(st.getSocket().isClosed()||st.getUserName().equals("")){
continue;
}
new Thread(st).start();
for(int i=0;i<vec_Threads.size();i++){
String secondName = st.getSecondName();
ServerThread serverThread=(ServerThread)vec_Threads.get(i);
serverThread.write("s"+secondName);
serverThread.write("0"+st.getUserName());
}
vec_Threads.addElement(st);
HashMap putUser = ServerThread.putUser();
vec_Users.add(putUser);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void exit(String message){
for(int i=0;i<vec_Threads.size();i++){
ServerThread st1=(ServerThread)vec_Threads.get(i);
st1.write("1"+message);
}
}
public static void say(int num,String msg,String secondName){
if(num==2){
msg = msg.substring(0,msg.indexOf("<"))+secondName+msg.substring(msg.indexOf(">")+1);
for(int i=0;i<vec_Threads.size();i++){
ServerThread st1=(ServerThread)vec_Threads.get(i);
st1.write(num+msg);
}
}
else if(num==3){
String toUserName = msg.substring(msg.lastIndexOf("<")+1,msg.lastIndexOf(">"));
for(int i=0;i<vec_Threads.size();i++){
ServerThread st1=(ServerThread)vec_Threads.get(i);
if(st1.getUserName().equals(toUserName)){
HashMap<String, String> h = (HashMap<String,String>)vec_Users.get(i);
String toUserSecondName = h.get(toUserName);
msg = msg.substring(0,msg.indexOf("<")+1)+secondName+msg.substring(msg.indexOf(">"),msg.lastIndexOf("<")+1)+toUserSecondName+msg.substring(msg.lastIndexOf(">"));
st1.write(num+msg);
break;
}
}
}
}
public static void privateSay(String msg){
for(int i=0;i<vec_Threads.size();i++){
ServerThread st1=(ServerThread)vec_Threads.get(i);
st1.write(msg);
}
}
}[/code]连接数据库为Mysql
提供数据库语句,希望感兴趣的同学拿下放进工程里去测试
create database chat;
create table chat_userinfo (username varchar(16) not null primary key,password varchar(16) not null,nickName varchar(12) not null,sex varchar(4) default '男',age varchar(4) , birthday varchar(4) ,truename varchar(12) , address varchar(30),description varchar(50),sign varchar(30),state varchar(10) default '离线');
create table chat_userfriends (username varchar(16) not null,friendname varchar(16) not null,setvisible varchar(2) not null default '否',setstate varchar(10) not null default '离线',stopful varchar(10) not null default '否');
create table chat_usermessage(username varchar(16) not null,friendname varchar(16) not null,message varchar(50) not null);
create table chat_groupinfo(groupid varchar(16) not null primary key,groupname varchar(12) not null,owner varchar(16) not null, description varchar(20));
create table chat_groupmember(groupid varchar(16) not null,groupmember varchar(16)); |