在socket中将数据从socket发送到c#

时间:2016-07-19 12:30:55

标签: c# android sockets gps

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

driver1.findElement(By.xpath("//span[contains(.,'Work Items')]")).click();

String[] expected = {"Defect", "Task", "Story", "Epic","Design Task"};

c#代码或服务器代码是:

@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();
    }   

}

我要说的另一件事是我将此权限添加到清单 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()); }

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

1 个答案:

答案 0 :(得分:0)

<强>客户端

要加速位置更新,您必须在设置LocationListener实施时向活动指定位置更新间隔。 类似的东西:

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

(请注意,上面的代码更像是伪代码,因为我触摸了LocationListener已经有很长时间了。)

服务器

您提供的代码未提及“OnDataReceived”成员函数。所以我认为你应该这样挖掘。

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