站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题那位有socket双向透传的例子!

那位有socket双向透传的例子!

摘要: 那位有socket双向透传的例子!


如题,分我另开帖加!


P2P啊? 用DatagramSocket打孔。



楼上的,说得详细些好不!


ChatClient

import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatClient extends JFrame implements Runnable,ActionListener
{
private DatagramSocket s;
private DatagramSocket s1;
private InetAddress hostAddress;
private byte[] buf=new byte[1024];
private DatagramPacket dp=new DatagramPacket(buf,buf.length);
JPanel jp1=new JPanel(new GridLayout(2,1));
JPanel jp2=new JPanel(new FlowLayout());
JTextArea j=new JTextArea(20,8);
JTextArea j2=new JTextArea(2,26);
JLabel jl1_1=new JLabel(-#34;文件-#34;);
JLabel jl2_1=new JLabel(-#34;消息-#34;);
JButton jb;
final JButton btn_2=new JButton(-#34;发送-#34;);
String outmessage=-#34;-#34;;

//构造方法
public ChatClient()
{
super(-#34;Client-#34;);
ImageIcon jbicon = new ImageIcon(-#34;images/send.jpg-#34;, -#34;-#34;);
jb=new JButton(jbicon);
jp1.add(jl2_1);
jp1.add(j2);
jp2.add(jp1);
jp2.add(jb);
this.add(jl1_1,-#34;North-#34;);
this.add(j,-#34;Center-#34;);
this.add(jp2,-#34;South-#34;);
jb.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setSize(400,400);
setLocation(100,100);
this.show();


}
public void actionPerformed(ActionEvent e)
{
try{
hostAddress=InetAddress.getByName(null);
outmessage = j2.getText();
j.append(outmessage + -#34;/n-#34;);
j2.setText(-#34;-#34;);
String outString = -#34;★变态狂:-#34; + outmessage;
byte[] buf = outString.getBytes();
DatagramPacket out = new DatagramPacket(buf, buf.length,
hostAddress, 4444);
s1.send(out);
}catch(IOException ex){ex.printStackTrace();}
}
public void run()
{
try{
s=new DatagramSocket(4400);
s1=new DatagramSocket();
while(true)
{
s.receive(dp);
String rcvd = new String(dp.getData(), 0, dp.getLength());
j.append(rcvd + -#34;/n-#34;);
}//while
}catch(UnknownHostException e){System.err.println(-#34;Can-#39;t open socket-#34;);System.exit(1);}
catch(SocketException e){System.err.println(-#34;Can-#39;t open socket-#34;);System.exit(1);}
catch(IOException e){System.err.println(-#34;Communication error-#34;);e.printStackTrace();System.exit(1);}
System.out.println(-#34;ChatClient over-#34;);

}
public static void main(String args[])
{
ChatClient cc= new ChatClient();
Thread t=new Thread(cc);
t.start();
}

}

-----------------------
ChatServer.java

import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatServer extends JFrame implements Runnable,ActionListener
{
static final int PORT=4444;
InetAddress hostAddress;
private byte[] buf=new byte[1024];
private DatagramPacket dpg=new DatagramPacket(buf,buf.length);
private DatagramSocket sk;
private DatagramSocket sk1;
JPanel jp=new JPanel(new GridLayout(2,1));
JPanel jp1=new JPanel(new FlowLayout());
JTextArea jt=new JTextArea(20,8);
JTextArea jt2=new JTextArea(2,26);
JLabel jl1=new JLabel(-#34;文件-#34;);
JLabel jl2=new JLabel(-#34;消息-#34;);
JButton btn_1=new JButton(-#34;发送-#34;);
JButton jb;
String outmessage=-#34;-#34;;
//方法说明
public ChatServer()
{
super(-#34;Server-#34;);
ImageIcon jbicon = new ImageIcon(-#34;images/send.jpg-#34;, -#34;-#34;);
jb=new JButton(jbicon);
jp.add(jl2);
jp.add(jt2);
jp1.add(jp);
jp1.add(jb);
this.add(jl1,-#34;North-#34;);
this.add(jt,-#34;Center-#34;);
this.add(jp1,-#34;South-#34;);
jb.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setSize(400,400);
setLocation(100,100);
this.show();

}
public void actionPerformed(ActionEvent e)
{
try{
hostAddress=InetAddress.getByName(null);
outmessage = jt2.getText();
jt.append(outmessage + -#34;/n-#34;);
jt2.setText(-#34;-#34;);
String outString = -#34;→八神★酷: -#34; + outmessage;
byte[] buf = outString.getBytes();
DatagramPacket out = new DatagramPacket(buf, buf.length,
hostAddress, 4400);
// DatagramPacket out = new DatagramPacket(buf, buf.length,
//dpg.getAddress(), 4400);
sk1.send(out);
}catch(IOException ex){System.err.println(-#34;Communication error-#34;);System.exit(1);}
}
public void run()
{
try{
sk = new DatagramSocket(PORT);
sk1 = new DatagramSocket();
System.out.println(-#34;服务器开始工作-#34;);
while (true)
{
sk.receive(dpg);
//获取信息
String rcvd=new String(dpg.getData(),0,dpg.getLength());
jt.append(rcvd+-#34;/n-#34;);
}//while
}catch(SocketException e){System.err.println(-#34;Can-#39;t open socket-#34;);System.exit(1);}
catch(IOException e){System.err.println(-#34;Communication error-#34;);;System.exit(1);}

}
public static void main(String args[])
{

ChatServer cs=new ChatServer();
Thread t=new Thread(cs);
t.start();
}
}




同时 启动这两个

一个是Client端
一个是Server端


唉,忘了说明必须是tcp的!





mark


mark



↑返回目录
前一篇: 求Jsp日期控件
后一篇: 建立中文文件建名出現亂碼?