//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
杨彩 阅读(2691)
评论(7) 编辑 收藏 所属分类:
我的Java程序