//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)
???{
????;
???}?
??? ?
?? ??
?? ??
?? ??}
?}
}
?
?
?
?
?
?
?
?
?
源碼下載:
posted on 2006-12-08 07:28
楊彩 閱讀(2702)
評論(7) 編輯 收藏 所屬分類:
我的Java程序