那位有socket双向透传的例子! 如题,分我另开帖加! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 P2P啊? 用DatagramSocket打孔。 ChatClientimport 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("文件"); JLabel jl2_1=new JLabel("消息"); JButton jb; final JButton btn_2=new JButton("发送"); String outmessage=""; //构造方法 public ChatClient() { super("Client"); ImageIcon jbicon = new ImageIcon("images/send.jpg", ""); jb=new JButton(jbicon); jp1.add(jl2_1); jp1.add(j2); jp2.add(jp1); jp2.add(jb); this.add(jl1_1,"North"); this.add(j,"Center"); this.add(jp2,"South"); 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 + "\n"); j2.setText(""); String outString = "★变态狂:" + 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 + "\n"); }//while }catch(UnknownHostException e){System.err.println("Can't open socket");System.exit(1);} catch(SocketException e){System.err.println("Can't open socket");System.exit(1);} catch(IOException e){System.err.println("Communication error");e.printStackTrace();System.exit(1);} System.out.println("ChatClient over"); } public static void main(String args[]) { ChatClient cc= new ChatClient(); Thread t=new Thread(cc); t.start(); }}-----------------------ChatServer.javaimport 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("文件"); JLabel jl2=new JLabel("消息"); JButton btn_1=new JButton("发送"); JButton jb; String outmessage=""; //方法说明 public ChatServer() { super("Server"); ImageIcon jbicon = new ImageIcon("images/send.jpg", ""); jb=new JButton(jbicon); jp.add(jl2); jp.add(jt2); jp1.add(jp); jp1.add(jb); this.add(jl1,"North"); this.add(jt,"Center"); this.add(jp1,"South"); 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 + "\n"); jt2.setText(""); String outString = "→八神★酷: " + 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("Communication error");System.exit(1);} } public void run() { try{ sk = new DatagramSocket(PORT); sk1 = new DatagramSocket(); System.out.println("服务器开始工作"); while (true) { sk.receive(dpg); //获取信息 String rcvd=new String(dpg.getData(),0,dpg.getLength()); jt.append(rcvd+"\n"); }//while }catch(SocketException e){System.err.println("Can't open socket");System.exit(1);} catch(IOException e){System.err.println("Communication error");;System.exit(1);} } public static void main(String args[]) { ChatServer cs=new ChatServer(); Thread t=new Thread(cs); t.start(); }} 同时 启动这两个一个是Client端一个是Server端 谁有oracle 下载地址啊!!?急用 一道定时执行的题目 通过窗口输入数据后,如何提取这些数据进行处理? 请教大家一个关于TextArea的问题,希望大家多多看看~ 各位兄弟来看看哈.关于继承的. jpg输出问题 菜人问题:问一个jdk-doc的问提 编译不通过,你做的,帮我看看啊! URL问题(100分) 谁知道怎么卸载resin? 新手java白痴问题!高手赐教! 在jsp页面如何给javascript中的变量付值
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("文件");
JLabel jl2_1=new JLabel("消息");
JButton jb;
final JButton btn_2=new JButton("发送");
String outmessage=""; //构造方法
public ChatClient()
{
super("Client");
ImageIcon jbicon = new ImageIcon("images/send.jpg", "");
jb=new JButton(jbicon);
jp1.add(jl2_1);
jp1.add(j2);
jp2.add(jp1);
jp2.add(jb);
this.add(jl1_1,"North");
this.add(j,"Center");
this.add(jp2,"South");
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 + "\n");
j2.setText("");
String outString = "★变态狂:" + 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 + "\n");
}//while
}catch(UnknownHostException e){System.err.println("Can't open socket");System.exit(1);}
catch(SocketException e){System.err.println("Can't open socket");System.exit(1);}
catch(IOException e){System.err.println("Communication error");e.printStackTrace();System.exit(1);}
System.out.println("ChatClient over"); }
public static void main(String args[])
{
ChatClient cc= new ChatClient();
Thread t=new Thread(cc);
t.start();
}}-----------------------
ChatServer.javaimport 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("文件");
JLabel jl2=new JLabel("消息");
JButton btn_1=new JButton("发送");
JButton jb;
String outmessage="";
//方法说明
public ChatServer()
{
super("Server");
ImageIcon jbicon = new ImageIcon("images/send.jpg", "");
jb=new JButton(jbicon);
jp.add(jl2);
jp.add(jt2);
jp1.add(jp);
jp1.add(jb);
this.add(jl1,"North");
this.add(jt,"Center");
this.add(jp1,"South");
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 + "\n");
jt2.setText("");
String outString = "→八神★酷: " + 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("Communication error");System.exit(1);}
}
public void run()
{
try{
sk = new DatagramSocket(PORT);
sk1 = new DatagramSocket();
System.out.println("服务器开始工作");
while (true)
{
sk.receive(dpg);
//获取信息
String rcvd=new String(dpg.getData(),0,dpg.getLength());
jt.append(rcvd+"\n");
}//while
}catch(SocketException e){System.err.println("Can't open socket");System.exit(1);}
catch(IOException e){System.err.println("Communication error");;System.exit(1);} }
public static void main(String args[])
{ ChatServer cs=new ChatServer();
Thread t=new Thread(cs);
t.start();
}
}
一个是Server端