/** */
/**
*文字聊天对话框
*采用DatagramSocket 和DatagramPacket信息
*接收聊天信息
*/
package chatsocket;
import java.awt. * ;
import java.awt.event. * ;
import java.util. * ;
import javax.swing. * ;
import java.net. * ;
import java.io. * ;
public class ChatReceiveSocket extends Thread ... {
private DatagramSocket receiveSocket;
private DatagramPacket receivePacket;
private String receiveMessage;
/** *//**
* 初始化
*/
public ChatReceiveSocket()...{
try...{
jbInit();
}
catch(Exception e)...{
e.printStackTrace();
}
}
public void jbInit()...{
try...{
receiveSocket=new DatagramSocket(8000);
}
catch(SocketException e)...{
e.printStackTrace();
System.exit(1);
}
}
/** *//**
* 处理接收信息
*/
public void run()...{
try...{
byte[] data1=new byte[1024];
receivePacket=new DatagramPacket(data1,data1.length);
receiveSocket.receive(receivePacket);
Toolkit.getDefaultToolkit().beep();
Thread.yield();
byte[] data2=receivePacket.getData();
String rece=new String(data2);
receiveMessage=rece.trim();
}
catch(IOException e)...{
e.printStackTrace();
}
}
public String getReceiveMessage()...{
return receiveMessage;
}
}
*文字聊天对话框
*采用DatagramSocket 和DatagramPacket信息
*接收聊天信息
*/
package chatsocket;
import java.awt. * ;
import java.awt.event. * ;
import java.util. * ;
import javax.swing. * ;
import java.net. * ;
import java.io. * ;
public class ChatReceiveSocket extends Thread ... {
private DatagramSocket receiveSocket;
private DatagramPacket receivePacket;
private String receiveMessage;
/** *//**
* 初始化
*/
public ChatReceiveSocket()...{
try...{
jbInit();
}
catch(Exception e)...{
e.printStackTrace();
}
}
public void jbInit()...{
try...{
receiveSocket=new DatagramSocket(8000);
}
catch(SocketException e)...{
e.printStackTrace();
System.exit(1);
}
}
/** *//**
* 处理接收信息
*/
public void run()...{
try...{
byte[] data1=new byte[1024];
receivePacket=new DatagramPacket(data1,data1.length);
receiveSocket.receive(receivePacket);
Toolkit.getDefaultToolkit().beep();
Thread.yield();
byte[] data2=receivePacket.getData();
String rece=new String(data2);
receiveMessage=rece.trim();
}
catch(IOException e)...{
e.printStackTrace();
}
}
public String getReceiveMessage()...{
return receiveMessage;
}
}