📄 chatserver.java
字号:
/**
* <p>application name: ListMerge</p>
* <p>application describing: 用于List正排序</p>
* <p>copyright: Copyrigtht 2006 东软 成都Java定制班版权所有</p>
* <p>company: neusoft</p>
* <p>time: 2006.09.19</P>
*
* @author YiLiu
* @version ver 1.0
* @email:2031120624@smail.ccniit.com
*
* */
/**
* <p><code>ListMerge</code>
*
* @see java.util.List
* @see java.util.conllections
* @author YiLiu
* @version 1.0,2006-9-19
* @deprecated
* */
package Chat_9_23.bak;
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ChatServer extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea outputArea;
private ServerSocket server;
private static Socket socket = null;
@SuppressWarnings("unused")
private ServerSocket fileserver;
private String Message;
//private static List<ChatPlay> ChatPlaysList;
static Vector ChatPlaysList= new Vector(10);
/**
* <p>
* <code>Method</code> 构造函数
*
* @author YiLiu
* @version 2.1,2006-9-24
* @since 1.0
*/
public ChatServer() {
super("Chat Server");
outputArea = new JTextArea();
getContentPane().add(outputArea, BorderLayout.CENTER);
outputArea.setAutoscrolls(true);
outputArea.setText("Server awaiting connections\n");
setSize(500, 300);
setVisible(true);
}
@SuppressWarnings("unchecked")
public void execute() {
try {
server = new ServerSocket(12345, 2);
fileserver = new ServerSocket(4001, 2);
} catch (IOException ioException) {
ioException.printStackTrace();
System.exit(1);
}// end of try/catch
int i = 0;
while( true )
{
//初始化一个ChatPlay的类数组
//ChatPlaysList = new ArrayList <ChatPlay> ();
if(ChatPlaysList.size() < 3)
{
try
{
socket=server.accept(); //用来存储连接上的客户socket
if(socket!=null)
{
System.out.println(socket+"连接"); //在控制台打印客户连接信息
}
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
do
{
ChatPlay ChatPlays = new ChatPlay(socket,i);
ChatPlays.start();
ChatPlaysList.addElement(ChatPlays);
i++;
break;
}while( i < ChatPlaysList.size());
}
else
{
try{Thread.sleep(200);}
catch(InterruptedException e)
{
e.printStackTrace();
}
}// end of if / else
}//end of while
}// end of execute
// 对文件对象进行监听初始化
public void fileexecute() {
}
private void displayMessage(final String messageToDisplay) {
// display message from event-dispatch thread of execution
SwingUtilities.invokeLater(new Runnable() { // inner class to ensure GUI
// updates properly
public void run() // updates outputArea
{
outputArea.append(messageToDisplay);
outputArea.setCaretPosition(outputArea.getText()
.length());
}
} // end inner class
); // end call to SwingUtilities.invokeLater
}// end of displayMessage
public boolean isChatOver() {
return false;
}
public synchronized void validate(String Mesasge) {
String msg = null;
String usermsg = null;
// 使用标记符读取指定信息
StringTokenizer msginfo = new StringTokenizer(Mesasge, ":");
if (msginfo.hasMoreTokens())
msg = msginfo.nextToken();
if (msginfo.hasMoreTokens())
usermsg = msginfo.nextToken();
// 验证指定用户发送信息
if (usermsg.equals("所有用户")) {
for (int i = 0; i < ChatPlaysList.size(); i++) {
// 从容器中取出对象.
ChatPlay temp =(ChatPlay)ChatPlaysList.elementAt(i);
temp.SendMessage(msg);
}
} else if (usermsg.equals("用户一")) {
ChatPlay temp = (ChatPlay)ChatPlaysList.elementAt(0);
temp.SendMessage(msg);
} else if (usermsg.equals("用户二")) {
ChatPlay temp = (ChatPlay)ChatPlaysList.elementAt(1);
temp.SendMessage(msg);
} else if (usermsg.equals("用户三")) {
ChatPlay temp = (ChatPlay)ChatPlaysList.elementAt(2);
temp.SendMessage(msg);
}
}
@SuppressWarnings("deprecation")
public void closeSokect() {
for (int i = 0; i < ChatPlaysList.size(); i++) {
// 从容器中取出对象的Sokect
ChatPlay temp = (ChatPlay)ChatPlaysList.elementAt(i);
try {
temp.connection.close();
temp.input.close();
temp.output.close();
temp.stop();
} catch (IOException e) {
e.printStackTrace();
} finally {
temp = null;
ChatPlaysList.clear();
}
}
}
public void saveFile(String message) {
String filepath = "c:\\ChatServerMsg.txt";
File filename = new File(filepath);
PrintWriter log;
// 获取系统时间
Date now = new Date();
DateFormat mediumformat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM);
String mediumDate = mediumformat.format(now);
try {
log = new PrintWriter(new FileWriter(filename, true), true);
log.println();
log.println();
log.println("-------登录时间: " + mediumDate + "------");
log.println();
log.println(message);
log.println();
log.println("-----------------------------------------");
log.println();
log.println();
log.close();
} catch (IOException e) {
e.printStackTrace();
System.err.println("无法打开文件: " + filename);
}
}
public static void main(String[] args) {
ChatServer application = new ChatServer();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.execute();
}
private class ChatPlay extends Thread {
private Socket connection;
private DataInputStream input;
private DataOutputStream output;
private int UserID;
private String HostName;
public ChatPlay(Socket socket, int userid) {
UserID = userid;
connection = socket;
try {
// 注册处理流
input = new DataInputStream(connection.getInputStream());
output = new DataOutputStream(connection.getOutputStream());
} catch (IOException ioException) {
ioException.printStackTrace();
System.exit(0);
}// end of try
}// end of custuors
public void SendMessage(String Message) {
try {
// 服务器转发信息
output.writeUTF(Message);
output.flush();
}
// process problems sending message
catch (IOException ioException) {
ioException.printStackTrace();
closeSokect();
}
}
public void run() {
displayMessage("有新游客进入! ");
try {
HostName = input.readUTF();
displayMessage(HostName + "\n");
// 发送用户所分配的ID号
output.writeInt(UserID);
// 将登录日志刻录到文件中
saveFile(HostName);
while (!isChatOver()) {
Message = input.readUTF();
if (Message != null) {
// displayMessage(Message+"\n");
validate(Message);
}
}
// 关闭连接
connection.close();
} catch (IOException IoException) {
IoException.printStackTrace();
closeSokect();
System.exit(0);
}
}// end of run
}// end of ChatPlayer
@SuppressWarnings("unused")
private class ChatFile {
private Socket connection;
private DataInputStream input;
private DataOutputStream output;
private String IPAddrees;
public ChatFile(Socket socket, String IPAddrees) {
connection = socket;
try {
// 注册处理流
input = new DataInputStream(connection.getInputStream());
output = new DataOutputStream(connection.getOutputStream());
} catch (IOException ioException) {
ioException.printStackTrace();
closeSokect();
System.exit(0);
}// end of try
}// end of custuors
/**
* @param filename
*/
public void SendFile(File filename) {
try {
// 服务器转发文件信息
byte[] file_buf = new byte[1024];
FileInputStream fis = new FileInputStream(filename);
int f_read = 1;
while (f_read > 0) {
f_read = fis.read(file_buf, 0, 1024);
if (f_read > 0) {
output.writeByte(f_read);
output.flush();
}
}// end of while
}
catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -