读书人

做一个类似QQ的聊天程序遇到的UDP有关

发布时间: 2012-01-16 23:36:51 作者: rapoo

做一个类似QQ的聊天程序遇到的UDP问题
我是一个才学JAVA不久的新手,现在在学做一个类似QQ的聊天程序,遇到个消息发送问题,我在客户端用UDP接收和发送信息,发送消息没问题,可是接收消息时有点问题,就是我在单机上调试,登陆两个号码,当登陆第二个号码是提示。ADDRESS IS USERD,好像是这个,就是说地址在使用中,我想是在创建接收消息数据报时用了同一个端口造成的,我的代码如下:

//以下创建数据报
public void CreatUDP()
{
try
{
sendSocket=new DatagramSocket();
receiveSocket=new DatagramSocket(8899);
// System.out.println( "udp ok ");
}catch(SocketException se)
{
se.printStackTrace();
System.out.println( "false udp ");
}
}
public void run()
{
while(true)//无限监听好友消息
{
try
{
for(int i=0;i <512;i++)
{
array1[i]= ' ';
}
receivePacket=new DatagramPacket(array1,array1.length);
receiveSocket.receive(receivePacket);
byte[] data=receivePacket.getData();
String infofromip=receivePacket.getAddress().getHostAddress().toString().trim();
received=new String(data,0,data.length);
received.trim();
int temp_qqno;
int temp_int;
index2=0;


if(received.substring(0,6).equals( "online ")) ///如果是通知上线
{
temp_qqno=Integer.parseInt(received.substring(6).trim());
do
{
temp_int=Integer.parseInt(f_qqno.get(index2).toString());
if(temp_qqno==temp_int)
{
break;
}
index2++;
}while(index2 <f_qqno.size());
f_userip.setElementAt(infofromip,index2);
DefaultListModel mm3=(DefaultListModel)list1.getModel();
int temp_picid=Integer.parseInt(f_userpic.get(index2).toString());
mm3.setElementAt(new Object[]{f_qqname.get(index2),new ImageIcon(picOnline[temp_picid+1])},index2);
}
else if(received.substring(0,7).equals( "offline ")) ///如果是通知下线
{
temp_qqno=Integer.parseInt(received.substring(7).trim());
do
{
temp_int=Integer.parseInt(f_qqno.get(index2).toString());
if(temp_qqno==temp_int)
{
break;
}
index2++;
}while(index2 <f_qqno.size());
infofromip= "null ";
f_userip.setElementAt(infofromip,index2);
DefaultListModel mm3=(DefaultListModel)list1.getModel();
int temp_picid=Integer.parseInt(f_userpic.get(index2).toString());
mm3.setElementAt(new Object[]{f_qqname.get(index2),new ImageIcon(picOffline[temp_picid+1])},index2);
}
else if(received.substring(0,8).equals( "oneaddme ")) //如果是有人发消息过来要加我
{


}
else //最后就是收到消息
{
index4=0;
do
{
String temp_ip=f_userip.get(index4).toString().trim();
if(infofromip.equals(temp_ip))
{
String temp_name=f_qqname.get(index4).toString().trim();
JOptionPane.showMessageDialog(this, "收到 "+temp_name+ "的消息 ", "ok ",JOptionPane.INFORMATION_MESSAGE);
fromunknow=false;
break;
}
}while(index4 <f_qqname.size());
if(index4> f_qqname.size())
{
JOptionPane.showMessageDialog(this, "收到陌生人的消息 ", "ok ",JOptionPane.INFORMATION_MESSAGE);
fromunknow=true;
}
}
}catch(IOException e4)
{
e4.printStackTrace();
}
}
}

//发送消息按扭监听
void send_mouseClicked(MouseEvent e)
{
try
{
String s_text=send_text.getText();
if(s_text.length()==0)
{
JOptionPane.showMessageDialog(this, "消息不能为空 ", "ok ",JOptionPane.INFORMATION_MESSAGE);
}
else


{
byte[] data=s_text.getBytes();
System.out.println(theip);
theip.trim();
if(theip.equals( "null ")||theip.equals( " ")||theip.equals( "0 "))
{
JOptionPane.showMessageDialog(this, "对不起,对方不在线,现在还不支持离线留言功能 ", "ok ",JOptionPane.INFORMATION_MESSAGE);
}
else
{
sendPacket=new DatagramPacket(data,s_text.length(),InetAddress.getByName(theip),sendPort);
sendSocket.send(sendPacket);
senddata.dispose();
JOptionPane.showMessageDialog(this, "消息发送成功 ", "ok ",JOptionPane.INFORMATION_MESSAGE);

}
}
}catch(IOException e2)
{
send_text.append(send_text.getText());
e2.printStackTrace();
senddata.dispose();
}
}

高手们,这个问题怎么解决啊 ???

[解决办法]
在本机调试登陆第二个号码,因为使用的是同一个端口,所以会抛address in used的错误.
解决的思路是:
判断当前使用的端口是否已经被占用,如果是,则在错误处理内,给当前的端口号加1.


类似这样:
public void init() {
try {
server = new ServerSocket(currentPort);
System.out.println( "开始监听,端口号为: " + currentPort);
} catch (IOException ex) {
currentPort++;
if (currentPort <= CommonUtil.MAXPORT) {
init();
} else {
System.out.println( "无可用端口提供! ");
}
// ex.printStackTrace();
}
}

另,QQ发送信息应该是经过服务器的,使用的是非UDP协议吧.

读书人网 >J2SE开发

热点推荐