将数据从socket发送到android中的c

本文关键字:android 中的 数据 socket | 更新日期: 2023-09-27 17:57:43

我正在尝试在android中开发一个应用程序,将gps数据发送到我的电脑。android部分是:

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    float latitude = (float) (location.getLatitude());
    float longitude = (float) (location.getLongitude());
    showMessage("Student Details", "Latitude: " + latitude + ", Longitude: " + longitude);
    Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);

    try {
        Socket socket = new Socket("192.168.1.5",5000);
        DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
        DOS.writeUTF("HELLO_WORLD");
        socket.close();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

c代码或服务器代码为:

 public AsyncCallback pfnWorkerCallBack;
    private Socket m_mainSocket;
    private Socket[] m_workerSocket = new Socket[25];
    private int m_clientCount = 0;
    private string ipaddress;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        startfun();
    }
    public void startfun()
    {
        try
        {
            // DrawMapPersian();
            ipaddress = "192.168.1.5";
            // Check the port value
            string portStr = "5000";
            int port = System.Convert.ToInt32(portStr);
            // Create the listening socket...
            m_mainSocket = new Socket(AddressFamily.InterNetwork,
                                      SocketType.Stream,
                                      ProtocolType.Tcp);
            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
            // Bind to local IP Address...
            m_mainSocket.Bind(ipLocal);
            listBox1.Items.Add("Server Started...");
            // Start listening...
            m_mainSocket.Listen(20);
            listBox1.Items.Add("Server Listening for ...");
            // Create the call back for any client connections...
            m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
        }
        catch (Exception qqq)
        {
            using (StreamWriter writer =
    new StreamWriter(@"e:'a.txt"))
            {
                writer.Write(qqq.Message);
            }
        }
    }

   public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                SocketPacket socketData = (SocketPacket)asyn.AsyncState;
                int iRx = 0;
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream 
                // by the client
                iRx = socketData.m_currentSocket.EndReceive(asyn);
                string res = GetParameters(socketData.dataBuffer);
                Console.WriteLine(res.ToString());
}

我应该说的另一件事是,我将此权限添加到清单<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />

我把手机连接到了无线网络,我的意思是手机可以访问我的ip。问题是gps数据生成得太慢了,我不知道为什么?另一个问题是android应用程序没有向服务器发送任何数据,

将数据从socket发送到android中的c

客户端

要加速位置更新,您必须在设置LocationListener实现时为"活动"指定位置更新间隔。类似于:

LocationListener impl = new MyLocationListenerImpl();
long interval = 4000;//change your code here
setLocationListener(impl, interval);

(请注意,上面的代码更像是一个伪代码,因为我已经很久没有接触LocationListener了)。

服务器

您提供的代码中没有提到"OnDataReceived"成员函数。所以我认为你应该这样挖掘。

除非套接字是故意使用的,否则请考虑使用Http协议和ASP等框架。NET和loopj-Async-Http将数据传递到服务器,这更容易!