楊彩的Java博客
          醉里挑燈看劍,夢里吹雨聽風.仗劍獨走天涯,試問誰與爭鋒!
          posts - 8,comments - 49,trackbacks - 0

          //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 jl1,jl2;
          ?public static TextArea ta1,ta2;
          ?public static JButton jb1,jb2;
          ?public static Container cp1;
          ? static Socket svr;
          ?public static PrintWriter out;
          ?
          ?
          ?public static void main(String arg[])
          ?{
          ??JFrame jf=new JFrame("與小新聊天--VIP???????????? 客戶端");
          ??jf.setSize(500,450);
          ??jf.setResizable(false);
          ??jf.locate(100,100);
          //??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)
          ???{
          ????;
          ???}?
          ??? ?
          ?? ??
          ?? ??
          ?? ??}
          ?}
          }
          ?

          ?

          ?

          ————————————————————————————————

          ?

          ?

          //QQ服務端

          import java.io.*;
          import java.net.*;
          import javax.swing.*;
          import javax.swing.event.*;
          import java.awt.*;
          import java.awt.event.*;

          public class ycserver extends JFrame
          {
          ?public static JLabel jl1;
          ?public static TextArea ta1,ta2;
          ?public static JButton jb1,jb2;
          ?public static Container cp1;
          ?public static ServerSocket svr;?
          ?public static Socket clt;
          ?public static Thread thread;
          ?public static int i=0;
          ?


          ??
          ?public static void main(String arg[])
          ?{
          ??JFrame jf=new JFrame("與楊彩聊天--VIP??????????? 服務端");
          ??jf.setSize(500,450);
          ??jf.locate(200,200);
          ??jf.setResizable(false);
          ??ta1=new TextArea();
          ??ta2=new TextArea();
          ??jb1=new JButton("發送");
          ??jb2=new JButton("清空");
          ??jl1=new JLabel("等待連接");
          ??jb1.addActionListener(new jb1Listener());
          ??jb2.addActionListener(new jb1Listener());
          ??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
          ??{
          ???target outmsg=new target();
          ???Thread outthread=new Thread(outmsg);
          ???outthread.start();
          ???
          ???wait wait1=new wait();
          ???Thread waitthread=new Thread(wait1);
          ???waitthread.start();
          ???
          ???QQ();

          ???

          ??}
          ??catch(Exception e)
          ??{
          ???
          ??}
          ??
          ??
          ?}
          ?
          ?
          static void QQ()throws Exception
          {/*
          ?//建立服務器套節字
          ? svr=new ServerSocket(3300);
          ? System.out.println("等待連接....");
          ?//等待客戶機接入
          ?? clt=svr.accept();
          ??? i++;
          ??? ta1.setText(i+"");
          ?//獲得客戶IP地址
          ? System.out.println("連接請求來自:"+clt.getInetAddress());
          ? jl1.setText("連接請求來自:"+clt.getInetAddress());
          ? //建立I/O流
          ? */

          ?? }
          ??
          ?}
          ?
          ?


          ?class jb1Listener implements ActionListener
          ?{
          ??public void actionPerformed(ActionEvent e)
          ??{
          ???if(e.getSource()==ycserver.jb2)
          ???{
          ????ycserver.ta1.setText("");
          ???}
          ???
          ???else
          ???{
          ???? try
          ??? {
          ??? ??????
          ?? ?PrintWriter out=new PrintWriter(ycserver.clt.getOutputStream());
          ?? ?out.println(ycserver.ta2.getText());
          ?? ?out.flush();
          ??? ?ycserver.ta1.append("[自己]說:\n"+ycserver.ta2.getText()+"\n");
          ??? ?ycserver.ta2.setText("");
          ?
          ???
          ??? }
          ???? catch(Exception ee)
          ???? {
          ???? ?
          ???? }
          ???}
          ??
          ??}
          ?}

          ?

          class target implements Runnable
          {
          ?public void run()
          ?{
          ???
          ??? while(true)
          ??? {
          ??? ?
          ???try
          ???{????
          ??? BufferedReader in=new BufferedReader(new InputStreamReader(ycserver.clt.getInputStream()));
          ? ??? String str=in.readLine();
          ?? ??? str="[客戶端在"+ycserver.clt.getInetAddress()+"]說:\n"+str;
          ??? ?? ycserver.ta1.append(str+"\n");
          ???}
          ???
          ???catch(Exception ee)
          ???{
          ????;
          ???}?
          ??? ?
          ?? ??
          ?? ??
          ?? ??}
          ?}
          }

          ?

          class wait implements Runnable
          {
          ?public void run()
          ?{
          ???
          ??? while(true)
          ??? {
          ??? ?
          ???try
          ???{????
          ??? //建立服務器套節字
          ? ycserver.svr=new ServerSocket(3300);
          ? System.out.println("等待連接....");
          ?//等待客戶機接入
          ?? ycserver.clt=ycserver.svr.accept();
          ??? ycserver.i+=2;
          ??? ycserver.ta1.setText(ycserver.i+"");
          ?//獲得客戶IP地址
          ? System.out.println("連接請求來自:"+ycserver.clt.getInetAddress());
          ? ycserver.jl1.setText("連接請求來自:"+ycserver.clt.getInetAddress());
          ? //建立I/O流
          ???}
          ???
          ???catch(Exception ee)
          ???{
          ????;
          ???}?
          ??? ?
          ?? ??
          ?? ??
          ?? ??}
          ?}
          }

          ?

          ?

          ?

          ?
          ?
          ?
          ?
          ?
          ?
          源碼下載:
          http://www.aygfsteel.com/Files/yangcai/qq.rar
          posted on 2006-12-08 07:28 楊彩 閱讀(2709) 評論(7)  編輯  收藏 所屬分類: 我的Java程序

          FeedBack:
          # re: Tcp通信實現兩機互相通信
          2006-12-08 09:22 | itVincent
          不錯的練習,java實現socket挺方便的  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信
          2006-12-08 20:50 |
          寫得不錯 前不久我也用socket在寫聊天室,不過你這樣的寫法與我的寫法

          都只能是實現聊天室功能,如果要實現單對單可能還要加上一些判斷條件. 后來

          我用UDP去完成單對單功能..哎 太慚愧了,線程沒學好,一直到現在都還沒完成.

          繼續努力同志~!  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信
          2006-12-09 08:30 | lix
          you can use broadcasting mode to write, so that you dont have to specify a server for your chating "network" ;)  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信
          2006-12-11 13:16 | joy[匿名]
          不支持nat把~  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信
          2007-01-08 13:53 | while
          mark!  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信[未登錄]
          2007-12-19 16:15 | 哈哈
          都不好意思說自己是新手了,不過還是要是說聲 謝謝!  回復  更多評論
            
          # re: Tcp通信實現兩機互相通信
          2008-12-17 11:03 | 提拉米蘇
          你很榜 我來支持你了  回復  更多評論
            
          主站蜘蛛池模板: 南汇区| 永泰县| 海阳市| 广州市| 道真| 洛浦县| 营山县| 车致| 兰溪市| 甘泉县| 延长县| 额济纳旗| 兰考县| 北碚区| 龙州县| 金昌市| 榆中县| 巧家县| 元谋县| 永州市| 沁阳市| 阿鲁科尔沁旗| 大竹县| 伊春市| 和田县| 桐城市| 紫阳县| 扶绥县| 滨海县| 云南省| 教育| 泸定县| 油尖旺区| 南阳市| 尼玛县| 女性| 西乌珠穆沁旗| 得荣县| 来宾市| 黄龙县| 吴忠市|