android发送udp,tcp消息
2013-11-14 10:48
197 查看
发送方创建步骤:
1. 创建一个DatagramSocket对象
DatagramSocket socket = new DatagramSocket (4567);
2. 创建一个 InetAddress , 相当于是地址
InetAddress serverAddress = InetAddress.getByName("想要发送到的那个IP地址");
3. 这是随意发送一个数据
String str = "hello";
4. 转为byte类型
byte data[] = str.getBytes();
5. 创建一个DatagramPacket 对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket package = new DatagramPacket (data , data.length , serverAddress , 4567);
6. 调用DatagramSocket对象的send方法 发送数据
socket . send(package);
udp的辅助工具类:
这在自己项目中主要用来发送频道号码,如果是1...9自然发送一次即可,如果是多余1位的,就依次发送。
发送tcp消息
1. 创建一个DatagramSocket对象
DatagramSocket socket = new DatagramSocket (4567);
2. 创建一个 InetAddress , 相当于是地址
InetAddress serverAddress = InetAddress.getByName("想要发送到的那个IP地址");
3. 这是随意发送一个数据
String str = "hello";
4. 转为byte类型
byte data[] = str.getBytes();
5. 创建一个DatagramPacket 对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket package = new DatagramPacket (data , data.length , serverAddress , 4567);
6. 调用DatagramSocket对象的send方法 发送数据
socket . send(package);
udp的辅助工具类:
这在自己项目中主要用来发送频道号码,如果是1...9自然发送一次即可,如果是多余1位的,就依次发送。
package com.wotlab.home.moneyplantairs.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; /** * Socket的助手类,用于通过TCP协议发送数据 * * @author lx * */ public class SocketHelper { private Socket s = null; BufferedReader input = null; PrintWriter output = null; public SocketHelper() { } public void connect(String ip, int port) throws Exception { s = new Socket(); SocketAddress socAddress = new InetSocketAddress(ip, port); // SocketAddress socAddress = new InetSocketAddress("192.168.1.105", // port); s.connect(socAddress, Constants.SOCKET_TIMEOUT); s.setSoTimeout(Constants.SOCKET_LINK_TIMEOUT); } public void close() throws Exception { output.close(); input.close(); s.close(); } public String sendTCP(String str) { String receive = ""; try { if (s == null) { throw new Exception(""); } output = new PrintWriter(s.getOutputStream()); output.print(str); output.flush(); input = new BufferedReader( new InputStreamReader(s.getInputStream())); receive = input.readLine(); } catch (Exception e) { e.printStackTrace(); } return receive; } }
发送tcp消息
相关文章推荐
- Socket编程 (连接,发送消息) (Tcp、Udp) ,这个是同步,跟上一篇差不多
- Socket编程 (连接,发送消息) (Tcp、Udp) - Part1
- Socket编程 (连接,发送消息) (Tcp、Udp) - Part1
- Socket编程(连接、发送消息)(Tcp、Udp)
- Android 部分机型有时UDP消息发送失败
- Qt开发:UDP、TCP发送文字消息、检测U盘插拔、SYSZUXpinyin软键盘输入法移植------[源码奉送]
- qt 下UDP消息的接收和发送和TCP发送文件
- tcp文件传输(udp发送消息确定是否接收)
- rsyslog用tcp/udp发送日志消息最大2k的限制配置
- linux socket接收、发送小工具(支持tcp、udp包(组播)的发送接收)
- android发送消息正确方式
- 在 TCP 网络上检测出有重复名称,发送消息的计算机的 IP 地址在数据中
- TCP和UDP的"保护消息边界" (经典)
- Android 中即时聊天或者后台任务需要发送消息的一种解决方案.
- Android JPush (通讯录好友,是该应用app,则发送一条提示消息,点击通知,跳进对应的页面)
- Android之发送接收服务器消息
- Android:子线程向UI主线程发送消息
- android 发送UDP广播,搜寻服务器建立socket链接
- 在android中如何使用UDP和TCP传输