问题描述
我正在尝试通过 wifi direct 连接两个设备,然后通过套接字在它们之间传输数据。
ArrayList<SendReceive> threadList = new ArrayList<>();
public class ServerClass extends Thread{
ServerSocket server;
{
try {
server = new ServerSocket(8888,5);
} catch (IOException e) {
e.printstacktrace();
}
}
public void run() {
while (true) {
try {
Socket client =server.accept();
runOnUiThread(new Runnable() {
@Override
public void run() {
appendStatus("Connection from " + client.getRemoteSocketAddress());
}
});
SendReceive serverThread=new SendReceive(client);
threadList.add(serverThread);
serverThread.start();
} catch (Exception e) {
System.out.println("Error occured in main: " + e.getStackTrace());
}
}
}
}
public class ClientClass extends Thread{
Socket socket;
String hostAdd;
public ClientClass(InetAddress hostAddress){
hostAdd=hostAddress.getHostAddress();
socket=new Socket();
}
@Override
public void run() {
try {
socket.connect(new InetSocketAddress(hostAdd,8888),500);
runOnUiThread(new Runnable() {
@Override
public void run() {
CONSt.setText("Now the client connected to server.");
}
});
sendReceive=new SendReceive(socket);
sendReceive.start();
} catch (IOException e) {
e.printstacktrace();
}
}
}
注意:SendReceive的代码是
private class SendReceive extends Thread{
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public SendReceive(Socket skt){
socket=skt;
try {
inputStream=socket.getInputStream();
outputStream=socket.getoutputStream();
} catch (IOException e) {
e.printstacktrace();
}
}
@Override
public void run() {
byte[] buffer =new byte[1024];
int bytes;
while (socket!=null){
try {
bytes=inputStream.read(buffer);
if(bytes>0){
String tempMsg=new String(buffer,bytes);
runOnUiThread(new Runnable() {
@Override
public void run() {
appendStatus(tempMsg);
}
});
}
} catch (IOException e) {
e.printstacktrace();
}
}
}
public void write(byte[] bytes){
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printstacktrace();
}
}
}
这个工作与 ipv4 地址一样
ClientClass clientClass= new ClientClass( InetAddress.getByName("192.168.49.1"));
clientClass.start();
但在 ipv6 地址上不起作用
ClientClass clientClass= new ClientClass( InetAddress.getByName("fe80::58a2:b5ff:fe7f:ea51%p2p0"));
clientClass.start();
我需要使用 ipv6 ,有什么问题!!为什么此代码不适用于 ipv6 和 ipv4 ,我如何使它与 ipv6 一起工作。
我将不胜感激
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)