java UDP的问题

evanfei 2010-01-17 08:44:11
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;


public class nchat {

Vector<String> vectorIP = null;
Vector<Information> online = null;
Vector<String> messages = null;

private static int PORT_MESSAGES = 8002;//端口 聊天信息
private static int PORT_ATTACHMENT = 8003;//端口 传输
private String HostAddress = null;
private String HostName = null;
private volatile boolean ReceiveBool;//用于结束接收信息的线程
DatagramSocket socket;//聊天信息
DatagramSocket FileSocket;//附件传输

public nchat(String name)
{
vectorIP = new Vector<String>();
online = new Vector<Information>();
messages = new Vector<String>();
ReceiveBool = true;
HostName = name;//用户名
try
{
HostAddress = InetAddress.getLocalHost().getHostAddress().toString();//获取本机IP
socket = new DatagramSocket(PORT_MESSAGES);
FileSocket = new DatagramSocket(PORT_ATTACHMENT);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void connect()
{
getIP();
presenceSend();
Thread thread = new Thread(new PresenceReceive());
thread.start();
}

class UserOnline implements Runnable
{
public UserOnline()
{

}

public void run()
{

}
}

class PresenceReceive implements Runnable
{
public void run()
{
byte[] data = new byte[65507];
while(ReceiveBool)
{
try
{
DatagramPacket dp = new DatagramPacket(data, data.length);
socket.receive(dp);
String t = new String(dp.getData(), 0, dp.getLength());
System.out.println(t);//////////////////////////////////////////////
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

private void presenceSend()
{
ListIterator<String> it = vectorIP.listIterator();
InetAddress ia = null;
DatagramSocket socket = null;
int i = vectorIP.size();
/* try
{
ia = InetAddress.getByName("169.254.10.95");
String mes = "///上线 " + HostAddress + " " + HostName;
byte [] data = mes.getBytes();
DatagramPacket output = new DatagramPacket(data, data.length, ia, PORT_MESSAGES);
socket.send(output);
}
catch(Exception e)
{

}*/
String mes = "///上线 " + HostAddress + " " + HostName;
byte [] data = mes.getBytes();
while(it.hasNext())
{
String ip = it.next();
try
{
ia = InetAddress.getByName(ip);
DatagramPacket output = new DatagramPacket(data, data.length, ia, PORT_MESSAGES);
socket.send(output);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

//获取局域网内全部IP 默认子网掩码为255.255.255.0
public void getIP()
{
String ip = null;
try
{
ip = InetAddress.getLocalHost().getHostAddress().toString();
System.out.println(ip);
}
catch(Exception e)
{

}
int i = ip.indexOf(".");
i = ip.indexOf(".", i+1);
i = ip.indexOf(".", i+1);

String ip1 = ip.substring(0, i);
String ip2 = ip.substring(i+1);
Integer t = new Integer(ip2);
i = t.intValue();

for(int i1 = 1;i1 <= 254;++i1)
{
if(i1 != i)
{
vectorIP.add(ip1 + "." + i1);
System.out.println(ip1 + "." + i1);
}
}

// ListIterator<String> it = vectorIP.listIterator();
// while(it.hasNext())
// {
// System.out.println(it.next());
// }

}

private int getHostid(String ip)
{
int i = ip.indexOf(".");
i = ip.indexOf(".", i+1);
i = ip.indexOf(".", i+1);
String t = ip.substring(i+1);
Integer tt = new Integer(t);
return tt.intValue();
}

public static void main(String [] args)
{

new nchat("fdssdf").connect();




}

}
麻烦谁能给看一下?
socket.send(output); socket.receive(dp);
时总是抛出异常,不明白啊
...全文
58 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
evanfei 2010-01-18
  • 打赏
  • 举报
回复
其实就是收不到发送的信息
sotom 2010-01-17
  • 打赏
  • 举报
回复
你的具体异常呢??? UDP 只是一个协议 Java遵守就可以了。
相关推荐
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2010-01-17 08:44
社区公告
暂无公告