加载中…
个人资料
无聊的承诺故事
无聊的承诺故事
  • 博客等级:
  • 博客积分:0
  • 博客访问:88,804
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

使用udp实现qq消息传输基础版本(未保证消息可靠性传输)

(2014-05-11 21:11:56)
模拟qq
   客户端A-->server-->客户端B
   消息格式  源qq号:目的qq号:传输内容;   -----列子10298,12345,nihao
   服务端代码
   public class UDPServer {
public static final Integer PORT = 10001;
public static DatagramSocket datagramSocket = null;
public static void main(String[] args){
byte[] buf = new byte[1000];
try {
datagramSocket = new DatagramSocket(PORT);
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
while(true){
datagramSocket.receive(datagramPacket);
String info = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
String sourceQQ = info.split(",")[0];//原始qq
String sourceIp = datagramPacket.getAddress().getHostAddress();
Integer sourcePort = datagramPacket.getPort();
// String targetIp = info.split(",")[0]; //目标ip
// Integer targetPort = Integer.valueOf(info.split(",")[1]); //目标端口
String targetQQ = info.split(",")[1];//目标qq
String message = info.split(",")[2]; //发送内容
UDPInfo.clinetInfoMap.put(sourceQQ, sourceIp+":"+sourcePort); //存储到map中以便下次查询
System.out.println("发送客户端A"+sourceIp+":"+sourcePort);

System.out.println("内容"+message);

if(UDPInfo.clinetInfoMap.get(targetQQ) == null){
message =targetQQ+"未登录";
DatagramPacket serverToSourcePacket = new DatagramPacket(message.getBytes("utf-8"), message.getBytes("utf-8").length, InetAddress.getByName(sourceIp), sourcePort);
datagramSocket.send(serverToSourcePacket);
}else{
String[] targetArr = UDPInfo.clinetInfoMap.get(targetQQ).split(":");
String targetIp = targetArr[0];
Integer targetPort = Integer.valueOf(targetArr[1]);
System.out.println("targetIp"+targetIp+"targetPort"+targetPort);
DatagramPacket serverToTargetPacket = new DatagramPacket(message.getBytes("utf-8"), message.getBytes("utf-8").length, InetAddress.getByName(targetIp), targetPort);
datagramSocket.send(serverToTargetPacket);
message = "消息"+message+"已经传送至server";
DatagramPacket serverToSourcePacket = new DatagramPacket(message.getBytes("utf-8"), message.getBytes("utf-8").length, InetAddress.getByName(sourceIp), sourcePort);
datagramSocket.send(serverToSourcePacket);
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static Map getIpAndPort(SocketAddress socketAddress){
Map map = new HashMap();
return map;
}

}
客户端代码
public class UDPClinetListenAndSend {

public static final Integer PORT = 10001;
public static DatagramSocket datagramSocket = null;
public static void main(String[] args){
DatagramPacket datePacket = null;
try {
datagramSocket = new DatagramSocket();
int port = datagramSocket.getLocalPort();
String address = datagramSocket.getLocalAddress().getHostAddress();
System.out.println("打印地址端口"+port+":"+address);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String message = "";
UDPServerToClientThread thread = new UDPServerToClientThread();
thread.start();
while (true) {
try {
message = br.readLine();
datePacket = new DatagramPacket(message.getBytes("UTF-8"), message.getBytes("UTF-8").length, InetAddress.getByName("127.0.0.1"), PORT);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
datagramSocket.send(datePacket);
System.out.println("从客户端发出的的内容是"+message);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
监听线程
public class UDPServerToClientThread extends Thread {
@Override
public void run() {
try {
while(true){
byte[] buf = new byte[1000];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
UDPClinetListenAndSend.datagramSocket.receive(datagramPacket);
String info = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println("接收自远端传来的信息"+info);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有