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

2014-11-24 07:25:45 · 作者: · 浏览: 1
vate 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(); } //根据相应的键获得对应的值 serverIP = prop.getProperty("serverip"); serverPORT = Integer.parseInt(prop.getProperty("serverudp.port")); receivePORT = Integer.parseInt(prop.getProperty("receiveudp.port")); } public void sendData(byte buffer[]){ try{ InetAddress address = InetAddress.getByName(serverIP); // outBuf = new byte[BUFFER_SIZE]; sendPacket = new DatagramPacket(buffer,buffer.length,address,serverPORT); sendSocket = new DatagramSocket(); sendSocket.send(sendPacket); }catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } public void closeSocket(){ receiveSocket.close(); } }

服务器:

package com.server.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ObjectOutputStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.tools.ClientToServerThread;

/**
 * @author lenovo
 *
 */
public class JChatFrm extends JFrame implements ActionListener{

	
	JTextArea jta;
	JTextField jtf;
	JButton jb;
	JPanel jp;
	String ownerId;
	String friendId;
	
	ClientToServerThread ctsT;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new JChatFrm();
	}
	
	public JChatFrm()
	{
		setTitle("服务器");
		jta=new JTextArea();
		jtf=new JTextField(15);
		jb=new JButton("发送");
		jb.addActionListener(this);
		jp=new JPanel();
		jp.add(jtf);
		jp.add(jb);
		
		this.add(jta,"Center");
		this.add(jp,"South");
	//	this.setTitle(ownerId+" 正在和 "+friend+" 聊天");
		this.setIconImage((new ImageIcon("image/qq.gif").getImage()));
	//	this.setSize(300, 200);
		this.setBounds(300, 200, 300, 200);
		this.setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		ctsT = new ClientToServerThread(jta);
		ctsT.start();
		
		/**窗体关闭按钮事件