android socket怎么获得ip地址
1个回答
展开全部
TCP Socket的代码
[java] view plain copy
class TCPThread extends Thread {
@Override
public void run() {
Log.i("juetion", "TCPThread runing");
int i = 0;
while (!this.isInterrupted()) { //判断是否中断线程
ServerSocket serverSocket = null;//声明一个ServerSocket对象
Log.i("juetion", "TCPThread run times:" + (i));
i++;
try {
serverSocket = new ServerSocket(12345);//创建一个ServerSocket对象,并在12345端口监听
Socket socket = serverSocket.accept();//接受客户端发送的请求
InputStream inputStream = socket.getInputStream();//获取数据流
byte buffer[] = new byte[1024 * 4];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1) {
Log.i("juetion", new String(buffer, 0, temp)); //数据输出
}
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.i("juetion", "serverSocket close");
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP Socket 的代码:
[java] view plain copy
class UDPThread extends Thread {
@Override
public void run() {
Log.i("juetion", "UDPThread runing");
int i = 0;
while (!this.isInterrupted()) {//判断是否中断线程
DatagramSocket datagramSocket = null;//声明DatagramSocket对象
Log.i("juetion", "UDPThread run times:" + (i));
i++;
try {
datagramSocket = new DatagramSocket(12345);//创建对象,并监听12345端口
byte data[] = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(data, data.length); //获取数据包
datagramSocket.receive(datagramPacket);//接受数据
Log.i("juetion", "" + new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength()));//数据输出
} catch (Exception e) {
e.printStackTrace();
}
Log.i("juetion", "datagramSocket close");
datagramSocket.close();
}
}
}
[java] view plain copy
class TCPThread extends Thread {
@Override
public void run() {
Log.i("juetion", "TCPThread runing");
int i = 0;
while (!this.isInterrupted()) { //判断是否中断线程
ServerSocket serverSocket = null;//声明一个ServerSocket对象
Log.i("juetion", "TCPThread run times:" + (i));
i++;
try {
serverSocket = new ServerSocket(12345);//创建一个ServerSocket对象,并在12345端口监听
Socket socket = serverSocket.accept();//接受客户端发送的请求
InputStream inputStream = socket.getInputStream();//获取数据流
byte buffer[] = new byte[1024 * 4];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1) {
Log.i("juetion", new String(buffer, 0, temp)); //数据输出
}
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.i("juetion", "serverSocket close");
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP Socket 的代码:
[java] view plain copy
class UDPThread extends Thread {
@Override
public void run() {
Log.i("juetion", "UDPThread runing");
int i = 0;
while (!this.isInterrupted()) {//判断是否中断线程
DatagramSocket datagramSocket = null;//声明DatagramSocket对象
Log.i("juetion", "UDPThread run times:" + (i));
i++;
try {
datagramSocket = new DatagramSocket(12345);//创建对象,并监听12345端口
byte data[] = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(data, data.length); //获取数据包
datagramSocket.receive(datagramPacket);//接受数据
Log.i("juetion", "" + new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength()));//数据输出
} catch (Exception e) {
e.printStackTrace();
}
Log.i("juetion", "datagramSocket close");
datagramSocket.close();
}
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询