📄 chatserver.java
字号:
//------------------
//Handle "recv"
//------------------
else if (line.startsWith("/recv "))
{
String someone;
//Don't care the first one
st.nextToken();
someone = st.nextToken();
for (int i=0; i<max; i++)
{
boolean flag = false;
if (t[i] != null && someone.equals(t[i].name)) {
while (true) {
if (t[i].postCnt == true) {
os.println(t[i].name + "'s message is: " + t[i].postMsg);
t[i].postCnt = false;
break;
}
else {
if (!flag) {
os.println("[busy]");
flag = true;
}
}
}
os.println("[nonbusy]");
}
}
}
//------------------
//Handle "who"
//------------------
else if (line.startsWith("/who"))
{
for (int i=0; i<max; i++)
{
if (t[i] != null)
{
if (t[i] == this)
os.println(t[i].name + ": " + t[i].clientSocket.getInetAddress() +
"/" + t[i].clientSocket.getPort() + " <-- myself");
else
os.println(t[i].name + ": " + t[i].clientSocket.getInetAddress() +
"/" + t[i].clientSocket.getPort());
}
}
}
//------------------
//Handle "kick"
//------------------
else if (line.startsWith("/kick "))
{
String someone;
//Don't care the first one
st.nextToken();
someone = st.nextToken();
for (int i=0; i<max; i++)
{
if (t[i] != null) {
if (someone.equals(t[i].name)) {
t[i].os.println("kick by "+name+", bye!");
t[i].kick = true;
}
else
t[i].os.println(someone + " is leaving the chat server.");
}
}
}
//------------------
//Handle "save"
//------------------
else if (line.startsWith("/save "))
{
String filename;
File _file;
FileWriter fw;
String cmd[] = new String[5];
int current = cnt;
//Don't care the first one
st.nextToken();
filename = st.nextToken();
_file = new File(filename);
if (!_file.exists()) {
try {
_file.createNewFile();
}
catch (IOException e) {
os.println("can't open the file: " + filename);
os.println(name+"> ");
continue;
}
}
fw = new FileWriter(filename);
for (int i=0; i < 5; i++)
{
if (--current < 0)
current = 4;
cmd[i] = command[current];
}
for (int i=4; i >= 0; i--)
{
String s1 = "\r\n";
String s2;
if (cmd[i] != null)
{
s2 = cmd[i];
s2 = s2.concat(s1);
fw.write(s2);
}
}
fw.close();
}
//------------------
//Handle "savea"
//------------------
else if (line.startsWith("/savea "))
{
String filename;
File _file;
FileWriter fw;
int i, j;
int current;
String cmd[] = new String[5];
//Don't care the first one
st.nextToken();
filename = st.nextToken();
_file = new File(filename);
if (!_file.exists()) {
try {
_file.createNewFile();
}
catch (IOException e) {
os.println("can't open the file: " + filename);
os.println(name+"> ");
continue;
}
}
fw = new FileWriter(filename);
for (i=0; i < max; i++)
{
if (t[i] != null)
{
String sectHead = "\r\n===============\r\nThe therad name: ";
if (t[i].name != null)
sectHead = sectHead.concat(t[i].name);
sectHead = sectHead.concat("\r\n");
fw.write(sectHead);
current = t[i].cnt;
for (j=0; j < 5; j++)
{
if (--current < 0)
current = 4;
cmd[j] = t[i].command[current];
}
for (j=4; j >= 0; j--)
{
String s1 = "\r\n";
String s2;
if (cmd[j] != null)
{
s2 = cmd[j];
s2 = s2.concat(s1);
fw.write(s2);
}
}
}
}
fw.close();
}
//------------------
//Handle "show"
//------------------
else if (line.startsWith("/show "))
{
String filename;
File _file;
FileReader fr;
//Don't care the first one
st.nextToken();
filename = st.nextToken();
_file = new File(filename);
if (_file.exists())
{
char data[] = new char[1024];
String s1;
int cnt;
fr = new FileReader(filename);
cnt = fr.read(data);
s1 = new String(data, 0, cnt);
os.println(s1);
fr.close();
}
else
os.println(filename + " is not available.");
}
else if (line.startsWith("/"))
{
os.println("**** Your message command '" + line + "' is incorrect");
}
os.println(name+"> ");
}
System.out.println(this+" --> Closed");
// Clean up:
for(int i=0; i<max; i++)
if (t[i]==this)
t[i]=null;
is.close();
os.close();
clientSocket.close();
}
catch(IOException e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -