客户端
public class TalkClient {
public static void main(String[] args) {
// Create application frame.
ClientSend cs = new ClientSend();
TalkClientFrame frame = new TalkClientFrame(cs);
// Show frame
frame.setVisible(true);
}
}
//----------------------------------------------------
import java.net.*;
public class ClientSend {
private DatagramSocket ds_CSend;
private DatagramPacket dp_CSend;
public ClientSend(){
try{
ds_CSend = new DatagramSocket(8080);
}
catch(Exception e){
e.printStackTrace();
}
}
public void Sended(byte[] buf,String sip){
try{
dp_CSend = new DatagramPacket(buf,buf.length,InetAddress.getByName(sip),8888);
ds_CSend.send(dp_CSend);
}
catch(Exception e){
e.printStackTrace();
}
}
}
///----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class TalkClientFrame extends Frame {
private List lst = new List(6);//存放对话内容
private TextField tf_Data = new TextField(20);//存放发送内容
private TextField tf_ip = new TextField(15);//存放IP和PORT;
private ClientSend cs;
public TalkClientFrame(ClientSend cs) {
this.cs = cs;
tf_ip.setText("127.0.0.1");
Panel p_Data = new Panel();//放置tf_Data,tf_ip的容器
//-----------------------布局-----------------------------------
this.add(lst,"Center");
this.add(p_Data,"South");
p_Data.add(tf_ip,"West");
p_Data.add(tf_Data,"East");
tf_Data.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//
tf_Dataactionperformed(evt);
}
});
//----------------------------------------------------------------
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("文件(F)");
menuFileExit.setLabel("退出(X)");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tf_Data.getText().length()>0)
TalkClientFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("TalkClient");
setMenuBar(menuBar);
setSize(new Dimension(350, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TalkClientFrame.this.windowClosed();
}
}
);
}
void tf_Dataactionperformed(ActionEvent e){
byte[] buf = tf_Data.getText().getBytes();
String strIp = tf_ip.getText();
try{
cs.Sended(buf,strIp);
}
catch(Exception ex){
ex.printStackTrace();
}
tf_Data.setText("");
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}
服务端
public class talkServer {
public static void main(String[] args) {
// TODO: Add your code here
talkRecv tR = new talkRecv();
tR.Recved();
}
}
//------------------------------------------------
import java.net.*;
public class talkRecv {
private DatagramSocket ds_Recv;
private DatagramPacket dp_Recv;
public boolean b_stop=true;
public talkRecv(){
try{
ds_Recv = new DatagramSocket(8888);//将8888端口作为接受端口
byte[] buf = new byte[1024];
dp_Recv = new DatagramPacket(buf,1024);
}
catch(Exception e){
e.printStackTrace();
}
}
public void Recved(){
while(b_stop){
try{
ds_Recv.receive(dp_Recv);
String str_info = new String(dp_Recv.getData(),0,dp_Recv.getLength());
System.out.println(" ip:"+dp_Recv.getAddress().getHostAddress());
System.out.println("port:"+dp_Recv.getPort());
System.out.println("data:"+str_info);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
//------------------------------------------------
这几个类都没怎么考虑对象的释放问题,这个问题希望那位大大能够关注一下,给小弟一点指点,小弟感激不禁