資源簡(jiǎn)介
JAVA利用TCP進(jìn)行服務(wù)器與客戶(hù)端通訊

代碼片段和文件信息
//QQ客戶(hù)端
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?????????????客戶(hù)端“);
jf.setSize(500450);
jf.setResizable(false);
jf.locate(100100);
// cp1=getContentPane();
jl1=new?JLabel(“連接中....“);
ta1=new?TextArea();
ta2=new?TextArea();
jb1=new?JButton(“發(fā)送“);
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(“正在連接服務(wù)器請(qǐng)稍候...“);
??//與指定地址的服務(wù)器相連接
??svr=new?Socket(“127.0.0.1“3300);//要連接遠(yuǎn)程主機(jī)請(qǐng)?zhí)領(lǐng)P?
?//svr=new?Socket(InetAddress.getLocalHost()3300)
??jl1.setText(“與“+svr.getInetAddress()+“連接成功!請(qǐng)輸出要傳送的信息...“);
?}
}
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(“[自己]說(shuō):\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=“客戶(hù)端在[“+yc.svr.getInetAddress()+“]說(shuō):\n“+str;
???? ?yc.ta1.append(str+“\n“);
}
catch(Exception?ee)
{
;
}
? ? ?
???
???
??? }
}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????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
評(píng)論
共有 條評(píng)論