資源簡介
JAVA利用TCP進行服務器與客戶端通訊

代碼片段和文件信息
//QQ客戶端
import?java.io.*;
import?java.net.*;
import?javax.swing.*;
import?javax.swing.event.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?yc?extends?Jframe
{
public?static?JLabel?jl1jl2;
public?static?TextArea?ta1ta2;
public?static?JButton?jb1jb2;
public?static?Container?cp1;
?static?Socket?svr;
public?static?PrintWriter?out;
public?static?void?main(String?arg[])
{
Jframe?jf=new?Jframe(“與小新聊天--VIP?????????????客戶端“);
jf.setSize(500450);
jf.setResizable(false);
jf.locate(100100);
// cp1=getContentPane();
jl1=new?JLabel(“連接中....“);
ta1=new?TextArea();
ta2=new?TextArea();
jb1=new?JButton(“發送“);
jb1.addActionListener(new?Listener());
jb2=new?JButton(“清空“);
jb2.addActionListener(new?Listener());
jf.getContentPane().setLayout(new?FlowLayout());
jf.getContentPane().add(jl1);
? jf.getContentPane().add(ta1);
? jf.getContentPane().add(ta2);
? jf.getContentPane().add(jb1);
? jf.getContentPane().add(jb2);
?
jf.setVisible(true);
try
{
target2?outmsg2=new?target2();
Thread?outthread2=new?Thread(outmsg2);
outthread2.start();
QQ();
}
catch(Exception?e)
{
}
}
static?void?QQ()throws?Exception
{
??System.out.println(“正在連接服務器請稍候...“);
??//與指定地址的服務器相連接
??svr=new?Socket(“127.0.0.1“3300);//要連接遠程主機請填IP?
?//svr=new?Socket(InetAddress.getLocalHost()3300)
??jl1.setText(“與“+svr.getInetAddress()+“連接成功!請輸出要傳送的信息...“);
?}
}
class?Listener?implements?ActionListener{
public?void?actionPerformed(ActionEvent?e)
{
??if(e.getSource()==yc.jb2)
???{
??? ??yc.ta1.setText(““);
???}
???
??else
??{
?? try
??{
??
?? yc.out=new?PrintWriter(yc.svr.getOutputStream());
?? yc.out.println(yc.ta2.getText());
?? yc.out.flush();
?? yc.ta1.append(“[自己]說:\n“+yc.ta2.getText()+“\n“);
?? yc.ta2.setText(““);
??
??
??}
???catch(Exception?ee)
???{
???
???}
??
??}
??
???
???
}
}
class?target2?implements?Runnable
{
public?void?run()
{
?
? ?while(true)
? ?{
? ?
try
{
?BufferedReader?in=new?BufferedReader(new?InputStreamReader(yc.svr.getInputStream()));
?? ?String?str=in.readLine();
??? ?str=“客戶端在[“+yc.svr.getInetAddress()+“]說:\n“+str;
???? ?yc.ta1.append(str+“\n“);
}
catch(Exception?ee)
{
;
}
? ? ?
???
???
??? }
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2560??2006-12-05?15:55??yc.java
?????文件???????2627??2006-12-05?15:42??ycserver.java
?????文件?????????30??2006-12-05?15:57??readme.txt
?????文件??????30413??2006-12-05?15:52??YC.JPG
?????文件??????30565??2006-12-05?15:52??YC2.JPG
-----------?---------??----------?-----??----
????????????????66195????????????????????5
- 上一篇:IP地址的合法性及子網判斷
- 下一篇:用java編程將txt文件數據導入oracle
評論
共有 條評論