java中UDP简单聊天程序(四)

2014-11-24 07:25:45 · 作者: · 浏览: 2
*/ this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if(JOptionPane.showConfirmDialog(null, "确定退出吗?","系统提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE)==0) { System.exit(0); ctsT.closeSocket(); } else { return; } } } ); } //写一个方法,让它显示消息 public void showMessage(String message) { String info= message; this.jta.append(info); } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(arg0.getSource()==jb) { byte buffer[] = jtf.getText().trim().getBytes(); ctsT.sendData(buffer); } } }

package com.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Properties;

import javax.swing.JTextArea;

import com.common.User;

/**
 * @author lenovo
 *
 */
public class ClientToServerThread extends Thread{

	private String sendIP = "127.0.0.1";	
	private int sendPORT = 6666;
	private int receivePORT = 8888;
	//声明发送信息的数据报套结字
    private DatagramSocket sendSocket = null;
    //声明发送信息的数据包
    private DatagramPacket sendPacket = null;
    //声明接受信息的数据报套结字
    private DatagramSocket receiveSocket = null;
    //声明接受信息的数据报
    private DatagramPacket receivePacket = null;
    //收发数据的端口
    private int myPort = 0;
    //接收数据主机的IP地址
    private String friendIP = null;
    private int friendPort = 0;

    //缓冲数组的大小
  
public static final int BUFFER_SIZE = 5120; private byte inBuf[] = null; //接收数据的缓冲数组 private byte outBuf[] = null; //发送数据的缓冲数组 JTextArea jta; // 构造函数 public ClientToServerThread(JTextArea jta) { this.jta = jta; getPropertiesInfo(); } public void run() { String receiveInfo = ""; try{ inBuf = new byte[BUFFER_SIZE]; receivePacket = new DatagramPacket(inBuf,inBuf.length); receiveSocket = new DatagramSocket(receivePORT); }catch (Exception e) { e.printStackTrace(); // TODO: handle exception } while (true) { if(receiveSocket == null){ break; } else { try { receiveSocket.receive(receivePacket); String message = new String(receivePacket.getData(),0,receivePacket.getLength()); jta.append("收到数据"+message+"\n"); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } } } /** * 该方法用来获得服务器属性文件中的IP、PORT */ private void getPropertiesInfo(){ Properties prop = new Properties(); InputStream inStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream("ServerInfo.properties"); try{ //获得相应的键值对 prop.load(inStream); }catch(IOException e){ e.printStackTrace(); } //根据相应的键获得对应的值 receivePORT = Integer.parseInt(prop.getProperty("serverudp.port")); } public void sendData(byte buffer[]){ try{ InetAddress address = InetAddress.getByName(sendIP); // outBuf = new byte[BUFFER_SIZE]; sendPacket = new DatagramPacket(buffer,buffer.length,address,sendPORT); sendSocket = new DatagramSocket(); sendSocket.send(sendPacket); }catch (Exception e) { e.printStackTrace(); // TODO: han