?? 例13-10.txt
字號:
【例13-10】
① 客戶端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ChatClient extends Applet implements Runnable,ActionListener
{ Button send;
TextField inputName,inputContent;
TextArea chatResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
String name="";
public void init()
{ setLayout(new BorderLayout());
Panel pNorth,pSouth;
pNorth=new Panel();
pSouth=new Panel();
inputName=new TextField(6);
inputContent=new TextField(22);
send=new Button("發送");
send.setEnabled(false);
chatResult=new TextArea();
pNorth.add(new Label("輸入妮稱(回車):"));
pNorth.add(inputName);
pSouth.add(new Label("輸入聊天內容:"));
pSouth.add(inputContent);
pSouth.add(send);
send.addActionListener(this);
inputName.addActionListener(this);
thread=new Thread(this);
add(pNorth,BorderLayout.NORTH);
add(pSouth,BorderLayout.SOUTH);
add(chatResult,BorderLayout.CENTER);
}
public void start(){
try{ socket=new Socket(this.getCodeBase().getHost(), 4331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
if(!(thread.isAlive())){
thread=new Thread(this);
thread.start();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==inputName){
name=inputName.getText();
send.setEnabled(true);
try{ out.writeUTF("姓名:"+name);
}
catch(IOException exp){}
}
if(e.getSource()==send)
{ String s=inputContent.getText();
if(s!=null)
{ try { out.writeUTF("聊天內容:"+name+":"+s);
}
catch(IOException e1){}
}
}
}
public void run(){
String s=null;
while(true){
try{ s=in.readUTF();
chatResult.append("\n"+s);
}
catch(IOException e){
chatResult.setText("和服務器的連接關閉");
break;
}
}
}
}
② 服務器端
import java.io.*;
import java.net.*;
import java.util.*;
public class Server{
public static void main(String args[]){
ServerSocket server=null;
Socket you=null;
Hashtable peopleList;
peopleList=new Hashtable();
while(true){
try { server=new ServerSocket(4331);
}
catch(IOException e1){
System.out.println("正在監聽");
}
try { you=server.accept();
InetAddress address=you.getInetAddress();
System.out.println("客戶的IP:"+address);
}
catch (IOException e) {}
if(you!=null){
Server_thread peopleThread=new Server_thread(you,peopleList);
peopleThread.start();
}
else continue;
}
}
}
class Server_thread extends Thread{
String name=null;
Socket socket=null;
File file=null;
DataOutputStream out=null;
DataInputStream in=null;
Hashtable peopleList=null;
Server_thread(Socket t,Hashtable list){
peopleList=list;
socket=t;
try { in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e) {}
}
public void run(){
while(true){
String s=null;
try{ s=in.readUTF();
if(s.startsWith("姓名:")){
name=s;
boolean boo=peopleList.containsKey(name);
if(boo==false)
peopleList.put(name,this);
else{
out.writeUTF("請換妮稱:");
socket.close();
break;
}
}
else if(s.startsWith("聊天內容")){
String message=s.substring(s.indexOf(":")+1);
Enumeration chatPersonList=peopleList.elements();
while(chatPersonList.hasMoreElements()){
((Server_thread)chatPersonList.nextElement()).out.writeUTF (message);
}
}
}
catch(IOException ee){
Enumeration chatPersonList=peopleList.elements();
while(chatPersonList.hasMoreElements()){
try { Server_thread th=(Server_thread)chatPersonList.nextElement();
if(th!=this&&th.isAlive())
th.out.writeUTF("客戶離線:"+name);
}
catch(IOException eee){}
}
peopleList.remove(name);
try { socket.close();
}
catch(IOException eee){}
System.out.println(name+"客戶離開了");
break;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -