Android:与两个用于套接字编程的仿真器相关的难度

时间:2012-08-20 10:41:26

标签: android eclipse sockets networking emulation

我遇到了与两个用于套接字编程的仿真器有关的问题...我为该服务器端和其他客户端创建了两个应用程序

服务器端

server.java

 package com.server.example;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;



  public class Server implements Runnable 
 {
private String address ="10.0.2.15";
private String add="10.0.2.2";
Context context;
@Override

public void run()

{
    try{

    InetAddress serveradd = InetAddress.getByName(address);
    InetAddress clientadd = InetAddress.getByName(add);
    DatagramSocket socket=new DatagramSocket(6000,serveradd);
    byte[] buf = new byte[5];
    byte[] bufsend =("hi").getBytes();
    DatagramPacket packet=new DatagramPacket(buf,buf.length);
    socket.receive(packet);
    Toast.makeText(context.getApplicationContext(),"received"+new String(packet.getData()),Toast.LENGTH_LONG).show();
    DatagramPacket packetsend=new DatagramPacket(bufsend,bufsend.length,clientadd,5000);
    socket.send(packetsend);
    }
    catch (Exception e)
    {
        //Toast.makeText(context.getApplicationContext(),"received"+e,Toast.LENGTH_LONG).show();    
    }

}

}

serverActivity.java

package com.server.example;



 import android.app.Activity;
 import android.os.Bundle;

 public class ServerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new Thread(new Server()).start();


}
}

client.java

package com.client1.example;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;



 public class Client implements Runnable 
{
private String address ="10.0.2.15";
private String add="10.0.2.2";
Context context;
@Override

public void run()

{
    try{

        InetAddress serveradd = InetAddress.getByName(address);
        InetAddress clientadd = InetAddress.getByName(add);
        DatagramSocket socket=new DatagramSocket(5000,clientadd);
        byte[] buf = new byte[5];
        byte[] bufsend =("hello").getBytes();
        DatagramPacket packetsend=new DatagramPacket(bufsend,bufsend.length,serveradd,6000);
        DatagramPacket packet=new DatagramPacket(buf,buf.length);

        socket.send(packetsend);
        Toast.makeText(context.getApplicationContext(),"received"+new String(packet.getData()),Toast.LENGTH_LONG).show();

        socket.receive(packet);
        }
        catch (Exception e)
        {
            //Toast.makeText(context.getApplicationContext(),"received"+e,Toast.LENGTH_LONG).show();    
        }

    }

    }

clientActivity.java

package com.client1.example;



import android.app.Activity;
 import android.os.Bundle;

public class Client1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new Thread(new Client()).start();
}
 }

logcat的

08-20 18:05:53.375: D/gralloc_goldfish(1003): Emulator without GPU emulation detected.
08-20 18:05:53.474: I/ActivityManager(88): Displayed com.server.example/.ServerActivity: +1s654ms
08-20 18:05:53.744: W/NetworkManagementSocketTagger(88): setKernelCountSet(10039, 0) failed with errno -2
08-20 18:08:06.773: W/ThrottleService(88): unable to find stats for iface rmnet0
08-20 18:10:12.865: A/NetworkStats(88): problem reading network stats
08-20 18:10:12.865: A/NetworkStats(88): java.lang.IllegalStateException: problem parsing idx 1
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.performPoll(NetworkStatsService.java:799)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.access$100(NetworkStatsService.java:128)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService$3.onReceive(NetworkStatsService.java:633)
08-20 18:10:12.865: A/NetworkStats(88):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
08-20 18:10:12.865: A/NetworkStats(88):     at android.os.Handler.handleCallback(Handler.java:605)
08-20 18:10:12.865: A/NetworkStats(88):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 18:10:12.865: A/NetworkStats(88):     at android.os.Looper.loop(Looper.java:137)
08-20 18:10:12.865: A/NetworkStats(88):     at android.os.HandlerThread.run(HandlerThread.java:60)
08-20 18:10:12.865: A/NetworkStats(88): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
08-20 18:10:12.865: A/NetworkStats(88):     at libcore.io.IoBridge.open(IoBridge.java:406)
08-20 18:10:12.865: A/NetworkStats(88):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
08-20 18:10:12.865: A/NetworkStats(88):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
08-20 18:10:12.865: A/NetworkStats(88):     ... 10 more
08-20 18:10:12.865: A/NetworkStats(88): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
08-20 18:10:12.865: A/NetworkStats(88):     at libcore.io.Posix.open(Native Method)
08-20 18:10:12.865: A/NetworkStats(88):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-20 18:10:12.865: A/NetworkStats(88):     at libcore.io.IoBridge.open(IoBridge.java:390)
08-20 18:10:12.865: A/NetworkStats(88):     ... 12 more

服务器活动正在模拟器5554上运行,我已使用

重定向它
redir add udp:5000:6000
清单文件中的

我添加了互联网权限...程序运行没有错误,但启动时没有任何反应

任何类型的帮助将不胜感激

谢谢

0 个答案:

没有答案