?? tcpclient2.java
字號:
/*****************************************
*程序功能:基于Socket結(jié)構(gòu)的聊天程序客戶端*
*程 序 員: *
*日 期:2006年4月2日 *
*版 本:1.0 *
*修改日期: *
*****************************************/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class TcpClient2 extends WindowAdapter implements ActionListener
{
Socket client; //客戶端Socket對象
boolean flag; //標記Socket對象是否已連接
BufferedReader cin; //Socket對象的字符輸入流
PrintWriter cout; //Socket對象的字符輸出流
Frame f; //聊天程序界面框架
Button SendButton;
Button ExitButton;
Label Label1;
Label Label2;
Label Label3;
TextArea DisplayArea;
TextField InputArea;
public TcpClient2()
{
try //try_catch對可能出現(xiàn)的異常進行處理
{
client=new Socket("localhost",6789); //與服務(wù)端進行連接
this.ChatFrame("客戶端"); //顯示在聊天程序界面
DisplayArea.append("成功建立連接!"+"\n");
flag=true;
while(flag) //提供Socket服務(wù)
{
InputStream is=client.getInputStream(); //獲取Socket對象的輸入流
cin =new BufferedReader(new InputStreamReader(is)); //創(chuàng)建字符輸入流存入變量cin
OutputStream os=client.getOutputStream(); //獲取Socket對象的輸出流
cout =new PrintWriter(os,true); //創(chuàng)建字符輸出流存入變量cout
String aline;
while((aline=cin.readLine())!=null) //等待接收輸入流數(shù)據(jù)
{
DisplayArea.append(aline+"\n"); //將從輸入流中讀入的字符串添加到多行文本框
if(aline.equals("bye")) //獲得結(jié)束標記時停止服務(wù)
{
flag=false;
break;
}
}
is.close(); //關(guān)閉流
os.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void ChatFrame(String str) //構(gòu)造聊天程序窗口界面
{
f=new Frame("聊天程序"+str);
f.setSize(800,600);
BorderLayout border=new BorderLayout(5,10);
f.setLayout(border);
DisplayArea=new TextArea(); //創(chuàng)建多行文本輸入?yún)^(qū)對象
f.add(DisplayArea,border.CENTER);
DisplayArea.setEditable(false); //置DisplayArea為不可編輯狀態(tài)
Panel p=new Panel(); //創(chuàng)建面板對象
f.add(p,border.SOUTH);
InputArea=new TextField(20); //創(chuàng)建文本輸入行對象
SendButton=new Button("發(fā)送"); //創(chuàng)建按鈕對象
ExitButton=new Button("關(guān)閉");
Label1=new Label("和");
Label2=new Label("聊天");
Label3=new Label("消息");
Choice choice1=new Choice();
choice1.add("LovelyPig");
choice1.add("SharpKnife");
choice1.add("Jhon");
choice1.add("Harvey");
p.add(Label1);
p.add(choice1);
p.add(Label2);
p.add(Label3);
p.add(InputArea); //把InputArea添加到p上
p.add(SendButton); //把SendButton添加到p上
p.add(ExitButton);
SendButton.addActionListener(this); //注冊SendButton的Action事件
ExitButton.addActionListener(this);
InputArea.addActionListener(this); //注冊InputArea的Action事件
f.setVisible(true);
f.addWindowListener(this); //注冊f的Window事件
}
public void actionPerformed(ActionEvent e) //處理按鈕單擊事件
{
if(e.getSource()==SendButton)
{
DisplayArea.append(InputArea.getText()+"\n"); //把用戶當(dāng)前輸入的字符串添加到多行文本框?qū)ο驞isplayArea上
cout.println(InputArea.getText()); //向Socket對象的字符輸出流發(fā)送字符串
InputArea.setText("");
}
if(e.getSource()==ExitButton)
{
cout.println("bye"); //向客戶端發(fā)送結(jié)束標記
System.exit(0); //程序運行結(jié)束
}
}
public void windowClosing(WindowEvent e) //單擊窗口關(guān)閉按鈕時的處理
{
cout.println("bye"); //向客戶端發(fā)送結(jié)束標記
System.exit(0); //程序運行結(jié)束
}
public static void main(String args[])
{
new TcpClient2();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -