?? serverthread.java
字號:
//用來和各個(gè)client通信
package chatServer;
import java.net.*;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.EOFException;
public class ServerThread extends Thread {
private ChatServer server;
private Socket socket;
public ServerThread(ChatServer server,Socket socket)
{
this.server=server;
this.socket=socket;
}
public void run()
{
try {
// Create a DataInputStream for communication; the client
// is using a DataOutputStream to write to us
DataInputStream din = new DataInputStream( socket.getInputStream() );
// Over and over, forever ...
while (true) {
// ... read the next message ...
String message = din.readUTF();
// ... tell the world ...
server.revMsg.setText( server.revMsg.getText()+"\n"+"Sending "+message);
// ... and have the server send it to all clients
server.sendToAll( 2+message );
}
} catch( EOFException ie ) {
// This doesn't need an error message
} catch( IOException ie ) {
// This does; tell the world!
ie.printStackTrace();
} finally {
// The connection is closed for one reason or another,
// so have the server dealing with it
server.removeConnection( socket );
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -