?? chatserver.java
字號:
import java.net.*;
import java.util.*;
import java.io.*;
public class ChatServer
{
ServerSocket server = null;
Collection cClient = new ArrayList();
public ChatServer(int port) throws Exception
{
server = new ServerSocket(port);
}
public void startServer() throws Exception
{
while(true)
{
Socket s = server.accept();
cClient.add( new ClientConn(s) );
}
}
class ClientConn implements Runnable
{
Socket s = null;
public ClientConn(Socket s)
{
this.s = s;
(new Thread(this)).start();
}
public void run()
{
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while(str != null && str.length() !=0)
{
System.out.println(str);
str = dis.readUTF();
}
s.close();
cClient.remove(this);
} catch (IOException e) {
System.out.println("client quit");
try {
if(s != null)
s.close();
cClient.remove(this);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception
{
ChatServer cs = new ChatServer(8888);
cs.startServer();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -