import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class Server
extends JFrame
implements WindowListener
{
/*the default port number
*/
final int iDefaultPort = 8448;
/*the Only instance that can be got
*/
static Server Instance = null;
/*The serverSocket
*/
static ServerSocket sssocServer = null;
/*Hashmap used to store the online users' answering sockets,and keep the
*message' pool for every user
*/
static Map hmMessagePool = null;
/*store the users' infomation,System's status infomation here
*/
static int iUserNumber = 0;
static int iNumberOfOnlineUsers = 0;
static int iServerPort = 8448;
static boolean IsRunning = false;
/*buttons gives server controls
*/
static JButton bStartServer = null;
static JButton bEndServer = null;
static JButton bConfigureServer = null;
static JButton bExitServer = null;
/*Panels that holds items
*/
static JPanel pButtons = null;
static JPanel pLabels = null;
static JPanel pLabelsAndScrollPane = null;
/*labels that shows system's message
*/
static JLabel lIpOfServer = null;
static JLabel lPortOfServer = null;
static JLabel lStartTimeOfServer = null;
static JLabel lAllUserNumber = null;
static JLabel lOnlineUserNumber = null;
/*shows all users' status
*/
static JScrollPane spAllUserStatus = null;
static JTable tStatusShow = null;
static Vector vTableHeader = null;
static Vector vTable = null;
/*the database interface
*
static Connection cUserInfo = null;
static Statement sQuery = null;*/
/*the getClient (main)loop thread */
StartNetServices startS = null;
private Server()
{
JPanel tempPanel = null;
BorderLayout tempLayout = null;
this.setTitle("Server Of ZX QQ");
this.setSize(600,400);
this.setResizable(true);
this.setLocation(200,160);
this.addWindowListener(this);
((BorderLayout)this.getLayout()).setVgap(10);
//add Control Buttons to the Panel
bStartServer = new JButton("Start Server");
bStartServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(IsRunning) return ;
StartServer();
}
});
bEndServer = new JButton("Stop Server");
bEndServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(false == IsRunning)
{
return ;
}
EndServer();
}
});
bConfigureServer = new JButton("Configure Server Port");
bConfigureServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(true == IsRunning)
{
return ;
}
new ConfigureServer();
}
});
bExitServer = new JButton("Exit");
bExitServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BeforeExit();
dispose();
System.exit(-1);
}
});
pButtons = new JPanel(new GridLayout(9,1));
pButtons.add(bConfigureServer);
pButtons.add(bStartServer);
pButtons.add(bEndServer);
pButtons.add(bExitServer);
this.getContentPane().add(pButtons,BorderLayout.WEST);
//add Message Labels to the Panel
lIpOfServer = new JLabel(" Server IP :");
lPortOfServer = new JLabel(" Server Port :");
lStartTimeOfServer = new JLabel("Server Start Time :");
lAllUserNumber = new JLabel(" Total User :");
lOnlineUserNumber = new JLabel("Total Online Users :");
tempPanel = new JPanel(new GridLayout(1,2));
tempPanel.add(lAllUserNumber);
tempPanel.add(lOnlineUserNumber);
pLabels = new JPanel(new GridLayout(4,1));
pLabels.add(lIpOfServer);
pLabels.add(lPortOfServer);
pLabels.add(lStartTimeOfServer);
pLabels.add(tempPanel);
//add Users's status panel to the MainFrame
spAllUserStatus = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tempLayout = new BorderLayout();
tempLayout.setVgap(10);
pLabelsAndScrollPane = new JPanel(tempLayout);
pLabelsAndScrollPane.add(pLabels,BorderLayout.NORTH);
pLabelsAndScrollPane.add(spAllUserStatus,BorderLayout.CENTER);
this.getContentPane().add(pLabelsAndScrollPane,BorderLayout.CENTER);
this.setVisible(true);
return ;
}
void BeforeExit()
{
JOptionPane.showMessageDialog(this,
"void BeforeExit() : Not written yet.",
"***888***",JOptionPane.PLAIN_MESSAGE);
}
void StartServer()
{
/* retrive user infomation from database and display it */
if(true == IsRunning)
{
JOptionPane.showMessageDialog(this,
"Server already started.",
"ZX QQ",JOptionPane.PLAIN_MESSAGE);
return ;
}
RetriveUserInfoAndDisplay();
try
{
startS = new StartNetServices();
startS.start();
}
catch(IOException e)
{
sssocServer = null;
e.printStackTrace();
JOptionPane.showMessageDialog(this,
"Create server socket meet exception here,\nserver has not started yet.",
"Error!",JOptionPane.ERROR_MESSAGE);
sssocServer = null;
return ;
}
lIpOfServer.setText(" Server IP :"+sssocServer.getInetAddress());
lPortOfServer.setText(" Server Port :"+iServerPort);
lStartTimeOfServer.setText("Server Start Time :"+(new java.util.Date()).toGMTString());
lAllUserNumber.setText(" Total User :"+iUserNumber);
lOnlineUserNumber.setText("Total Online Users :"+(iNumberOfOnlineUsers=0));
this.getContentPane().validate();
}
/*inner class which */
private class StartNetServices
extends Thread
{
Socket tempSocket = null;
StartNetServices() throws IOException
{
this.setName("getClient Loop");
sssocServer = new ServerSocket(iServerPort);
IsRunning = true;
}
public void run()
{
hmMessagePool = Collections.synchronizedMap(new HashMap(256));
while(true)
{
try
{
tempSocket = sssocServer.accept();
(new ConnectionToClient(tempSocket)).start();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
}
/*inner class that build the connection between client and the server
*do all the infomation handling when needed.
*/
private class ConnectionToClient
extends Thread
{
/*wether User logined */
private Boolean bLoginAlready = Boolean.FALSE;
/*Streams for the connection*/
private InputStream isFromClient = null;
private DataInputStream disFromClient = null;
private OutputStream osToClient = null;
private DataOutputStream dosToClient = null;
/*the socket connect to Client*/
private Socket sConnectionToClient = null;
/*User Info*/
private int iUserId;
private String sUserPasswd = null;
private String sUserName = null;
/*Interface to database*/
private Connection cUserPasswd = null;
private Statement sQuery = null;
private ResultSet rsUserPasswd = null;
ConnectionToClient(Socket sToClient)
{
this.sConnectionToClient = sToClient;
try
{
/*get the InputStream from the socket*/
this.isFromClient = this.sConnectionToClient.getInputStream();
this.disFromClient = new DataInputStream(new BufferedInputStream(this.isFromClient));
/*get the OutputStream from the socket*/
this.osToClient = this.sConnectionToClient.getOutputStream();
this.dosToClient = new DataOutputStream(new BufferedOutputStream(this.osToClient));
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,
"Exception thrown when get InputStream from the socket.",
"Connection error",JOptionPane.PLAIN_MESSAGE);
e.printStackTrace();
return ;
}
}
private void UserLogin()
{
int iMsglen;
byte[] baMsg = new byte[2048];
try
{
this.iUserId = th