如何使用C#smat设备应用程序从智能设备将GPS数据存储在服务器中 [英] How stores GPS Data in server from smart device using C# smat device application

查看:96
本文介绍了如何使用C#smat设备应用程序从智能设备将GPS数据存储在服务器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友
我开发了智能设备中的应用程序,用于从串行端口读取GPS数据(纬度和经度),我可以获取经度和纬度,我的下一个任务是将GPS dat保存在服务器中,并使用纬度和经度显示位置

hi friends
i developed application in smart device for reading GPS Data (latitude and longitude) from serial port i can get latitued and longitude my next task is SAVE the GPS dat in server and show postion using latitude and longititude

推荐答案

您的问题并未真正概述如何传输数据的详细信息,但是如果您想寻求一个简单的解决方案,则可以在服务器上打开一个套接字来侦听传入的连接您喜欢的任何格式的数据.

因此,桌面应用程序看起来像这样:

Your question does not really outline the details of how you want to transmit the data, but if you want to go for a dead simple solution you could have a socket open on the server listening for incoming connections sending the data in any format you like.

So a desktop application looking something like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;

namespace Server {
    class Program  {
        private Thread thread;
        private Socket serverSocket;
        private bool active;

        public Program() {
            IsActive = true;
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint( IPAddress.Any, 4242));
            serverSocket.Listen(4);
            thread = new Thread(AcceptMethod);
            thread.Start();
        }

        private void AcceptMethod() {
            try {
                while (IsActive) {
                    Console.Write("Waiting for client connection...");
                    Socket clientSocket = serverSocket.Accept();
                    Console.WriteLine("{0} connected!", clientSocket);
                    StringBuilder builder = new StringBuilder();
                    while (clientSocket.Connected && clientSocket.Available > 0) {
                        byte[] buffer = new byte[32];
                        int read = clientSocket.Receive(buffer);
                        builder.Append(Encoding.UTF8.GetString(buffer, 0, read));
                    }
                    Console.WriteLine("Read message: {0}", builder);
                    clientSocket.Close();
                }
            }
            catch (Exception e) {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }

        public bool IsActive {
            get { return active; }
            set {
                active = value;
                if (!IsActive)
                {
                    serverSocket.Close();
                }
            }
        }

        static void Main(string[] args) {
            Program program = new Program();
            Console.ReadLine();

            program.IsActive = false;
        }
    }
}




运行在设备上的客户端应用程序看起来像:




And a client application running on the device looking something like:

private void sendButton_Click(object sender, EventArgs e) {
    string host = serverAddress.Text.Split(':')[0];
    int port = Convert.ToInt32(serverAddress.Text.Split(':')[1]);
    TcpClient client = new TcpClient(host, port);

    byte[] buffer = Encoding.UTF8.GetBytes(String.Format("ClientId={0};Latitude={1};Longitude={2}", clientId.Text, latitude.Text, longitude.Text));
    client.GetStream().Write(buffer, 0, buffer.Length);
    client.Close();
}




然后,在服务器端,您可以解析从客户端发送到服务器的消息,并将其存储在文件或DB中或任何您想要的位置.

请注意,这只是一个示例,对于实际的应用程序,您可能还希望考虑安全性之类的东西,并且更有可能使用基于http的方法.

至于在地图上显示它,我会研究Google Maps api或类似的东西.

希望这会有所帮助,
弗雷德里克·博纳德(Fredrik Bornander)




Then, on the server side you could parse the message sent from the client to the server and store that in a file or in a DB or where ever you want.

Note that this is just an example to get you going, for an actual application you''d probably want to consider things like security as well, and more than likely use a http based approach instead.

As for showing it on a map, I''d look into the Google maps api or something similar.

Hope this helps,
Fredrik Bornander


这篇关于如何使用C#smat设备应用程序从智能设备将GPS数据存储在服务器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆