import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class UDPClient extends Frame {
DatagramPacket dp = null;
TextArea ta = null;
Button b1 = null;
String str = null;
byte[] buff = new byte[1000];
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
DatagramSocket ds = null;
public void Laundframe() {
setTitle("b");
setLocation(200, 300);
setSize(400, 400);
setVisible(true);
this.setLayout(new GridLayout(2, 1));
ta = new TextArea("", 8, 30, TextArea.SCROLLBARS_NONE);
b1 = new Button("发送给a");
b1.addActionListener(new MonArea(this));
this.add(ta);
this.add(b1);
this.setResizable(false);
pack();
this.addWindowListener(new Windowclosed());
}
// ////////////////窗口关闭////////////////////
class Windowclosed extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
// ///////TextArea监听类//////////////////////////
class MonArea implements ActionListener {
UDPClient udp = null;
MonArea(UDPClient udp) {
this.udp = udp;
}
public void actionPerformed(ActionEvent e) {
str = udp.ta.getText();
udp.ta.setText("");
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
try {
dos.writeUTF(str);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void sconnect() {
try {
dp = new DatagramPacket(buff, buff.length, new InetSocketAddress(
"127.0.0.1", 3333));
} catch (SocketException e1) {
e1.printStackTrace();
}
try {
ds = new DatagramSocket(4567);
ds.send(dp);
ds.close();
} catch (SocketException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
UDPClient ff = new UDPClient();
ff.Laundframe();
ff.sconnect();
}
}
另一端
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class UDPServer extends Frame {
TextArea ta = null;
Button b1 = null;
String str1 = null;
DatagramPacket dp = null;
DatagramSocket ds = null;
ByteArrayInputStream bais = null;
DataInputStream dis = null;
byte[] buff = new byte[1000];
public void Laundframe() {
setTitle("A");
setLocation(200, 300);
setSize(400, 400);
setVisible(true);
this.setLayout(new GridLayout(2, 1));
ta = new TextArea("", 8, 30, TextArea.SCROLLBARS_NONE);
b1 = new Button("发送给B");
b1.addActionListener(new MonArea(this));
this.add(ta);
this.add(b1);
this.setResizable(false);
pack();
this.addWindowListener(new Windowclosed());
}
// ////////////////窗口关闭////////////////////
class Windowclosed extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
// ///////TextArea监听类//////////////////////////
class MonArea implements ActionListener {
UDPServer udp = null;
MonArea(UDPServer udp) {
this.udp = udp;
}
public void actionPerformed(ActionEvent e) {
String str = null;
str = udp.ta.getText();
System.out.println(str);
udp.ta.setText("");
}
}
// //网络connect
public void connect() {
dp = new DatagramPacket(buff, buff.length);
try {
ds = new DatagramSocket(3333);
} catch (SocketException e) {
e.printStackTrace();
}
while (true) {
try {
ds.receive(dp);
System.out.println("a connect ...");
bais = new ByteArrayInputStream(buff);
dis = new DataInputStream(bais);
ta.setText(dis.readUTF());
System.out.println(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ta.setText(str1);
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// /////////////////////////////////////////////
// main
public static void main(String[] args) {
UDPServer us = new UDPServer();
us.Laundframe();
us.connect();
}
}