C# 移動端與PC端的資料互動
小記:針對目前功能越來越強大的智能手機來說,在PC端支援對手機中的使用者資料作同步、備份以及恢複等保護措施的應用已經急需完善。不僅要對資料作保護,而且使用者更希望自己的手機跟PC能夠一體化,以及和遠程伺服器的一體化。使用者希望在手機端的操作能夠轉移到PC端,對於PC端大螢幕的電腦來說,完成同樣的操作可以大量的節省使用者的時間。對於功能強大的手機來說,有近1/2的應用可以在PC端同步。所以對PC端應用的規劃要以系統的角度來對待。同時要保證手機端和PC端的主流互動模式應保持一致。個人觀點:資料的一體化和管理的多元化是以後發展的一個趨勢。下面進行今天的學習,今天的實驗室移動端和PC端的資料互動及解析。
1,如何?移動端和PC端的資料互動?
答:1,藍芽 2,NFC技術 3,紅外 4,Socket.
NFC和藍芽的異同點:
相同點:都是近距離傳輸。
不同點: NFC優於紅外和藍芽傳輸方式。作為一種面向消費者的交易機制,NFC比紅外更快、更可靠而且簡單得多,不用向紅外那樣必須嚴格的對齊才能傳輸資料。與藍芽相比,NFC面向近距離交易,適用於交換財務資訊或敏感的個人資訊等重要資料;藍芽能夠彌補NFC通訊距離不足的缺點,適用於較長距離資料通訊。因此,NFC和藍芽互為補充,共同存在。事實上,快捷輕型的NFC 協議可以用於引導兩台裝置之間的藍芽配對過程,促進了藍芽的使用。但是要實現遠距離的資料轉送那就只能用Socket了,
下面進行代碼的分析:
首先在PC上建立服務端Server.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpServer
{
class Program
{
public static Socket serverSocket;
static Thread threadSend;
static Thread sendDataToClient;
static int count = 1;
static void Main(string[] args)
{
//確定連接埠號碼
int port = 121;
//設定串連IP
string host = "192.168.1.100";
//將IP地址字串轉化為IP地址執行個體
IPAddress ip = IPAddress.Parse(host);
//將網路端點表示為 IP 位址和連接埠號碼
IPEndPoint ipe = new IPEndPoint(ip, port);
//建立Socket
//addressFamily 參數指定 Socket 類使用的定址方案
//socketType 參數指定 Socket 類的類型
//protocolType 參數指定 Socket 使用的協議。
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//socket與本地終結點建立關聯
socket.Bind(ipe);
while (true)
{
//開始監聽連接埠
socket.Listen(0);
Console.WriteLine("服務已開啟,請等待....." + DateTime.Now.ToString() + DateTime.Now.Millisecond.ToString());
//為建立的串連建立新的Socket目的為用戶端將要建立串連
serverSocket = socket.Accept();
Console.WriteLine("串連已建立......" + DateTime.Now.ToString() + DateTime.Now.Millisecond.ToString());
Console.WriteLine("用戶端IP:"+serverSocket.RemoteEndPoint);
string recStr =string.Empty;
//定義緩衝區用於接收用戶端的資料
byte[] recbyte = new byte[1024];
ReceiveData();
sendDataToClient = new Thread(sendData);
sendDataToClient.Start();
}
}
public static void sendData()
{
while (true)
{
Console.WriteLine("send to client\n");
//服務端給用戶端回送訊息
string strSend = "Hello Android Client!" + DateTime.Now.Second;
//string strSend = "HelloClient1HelloClient2HelloClient3HelloClient4HelloClient5HelloClient6HelloClient7HelloClient8HelloClient9HelloClient10HelloClient11HelloClien12HelloClien13HelloClien14HelloClien15HelloClient16";
byte[] sendByte = new byte[1024];
//將發送的字串轉換為byte[]
sendByte = UTF8Encoding.UTF8.GetBytes(strSend);
//服務端發送資料
serverSocket.Send(sendByte, sendByte.Length, 0);
Thread.Sleep(1000);
}
}
#region
/// <summary>
/// 非同步串連
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="clientSocket"></param>
public static void Connect(IPAddress ip, int port)
{
serverSocket.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), serverSocket);
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket handler = (Socket)ar.AsyncState;
handler.EndConnect(ar);
}
catch (SocketException ex)
{
throw ex;
}
}
/// <summary>
/// 發送資料
/// </summary>
/// <param name="data"></param>
public static void Send(string data)
{
//Send(System.Text.Encoding.UTF8.GetBytes(data));
Send(UTF8Encoding.UTF8.GetBytes(data));
}
/// <summary>
/// 發送資料
/// </summary>
/// <param name="byteData"></param>
private static void Send(byte[] byteData)
{
try
{
int length = byteData.Length;
byte[] head = BitConverter.GetBytes(length);
byte[] data = new byte[head.Length + byteData.Length];
Array.Copy(head, data, head.Length);
Array.Copy(byteData, 0, data, head.Length, byteData.Length);
serverSocket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), serverSocket);
}
catch (SocketException ex)
{ }
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket handler = (Socket)ar.AsyncState;
handler.EndSend(ar);
}
catch (SocketException ex)
{
throw ex;
}
}
static byte[] MsgBuffer = new byte[128];
/// <summary>
/// 接收訊息
/// </summary>
public static void ReceiveData()
{
if (serverSocket.ReceiveBufferSize > 0)
{
serverSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
int REnd = serverSocket.EndReceive(ar);
Console.WriteLine("長度:" + REnd);
if (REnd > 0)
{
byte[] data = new byte[REnd];
Array.Copy(MsgBuffer, 0, data, 0, REnd);
int Msglen = data.Length;
//在此次可以對data進行按需處理
serverSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallback), null);
//Console.WriteLine("接收服務端的資訊:{0}", Encoding.ASCII.GetString(MsgBuffer, 0, Msglen));
Console.WriteLine("接收服務端的資訊:{0}", UTF8Encoding.UTF8.GetString(data, 0, Msglen));
}
else
{
dispose();
}
}
catch (SocketException ex)
{ }
}
private static void dispose()
{
try
{
serverSocket.Shutdown(SocketShutdown.Both);
serverSocket.Close();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
移動端使用Unity3D寫的指令碼掛在mainCamera上,代碼如下:
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Text;
using System.IO;
public class client : MonoBehaviour
{
public GUIText text;
public GUIText path;
public static Socket clientSocket;
// Use this for initialization
void Start ()
{
//伺服器IP
string LocalIP = "192.168.1.100";
//連接埠號碼
int port = 121;
IPAddress ip =IPAddress.Parse(LocalIP);
IPEndPoint ipe = new IPEndPoint(ip,port);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立用戶端Socket
clientSocket.Connect(ipe);
StartCoroutine("sendData"); //啟動協同程式
StartCoroutine("getInfo");
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Escape)||Input.GetKey(KeyCode.Home))
{
Application.Quit();
}
}
void OnGUI()
{
if(GUI.Button(new Rect(Screen.width/2-40,30,100,60),""))
{
StartCoroutine("GetCapture");
}
}
IEnumerator GetCapture ()
{
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0, true);
byte[] imagebytes = tex.EncodeToPNG ();//轉化為png圖
tex.Compress (false);//對螢幕緩衝進行壓縮
//image.mainTexture = tex;//對螢幕緩衝進行顯示(縮圖)
string PicPath="storage";
File.WriteAllBytes (Application.persistentDataPath+"/"+Time.time+ ".png", imagebytes);//儲存png圖
path.text = Application.persistentDataPath+"/";
}
/// <summary>
/// 向伺服器發送訊息
/// </summary>
/// <returns>The data.</returns>
public IEnumerator sendData()
{
int i=0;
while(true)
{
string sendStr = i.ToString()+"你好 server,I am Android";
byte[] sendBytes = UTF8Encoding.UTF8.GetBytes(sendStr);
clientSocket.Send(sendBytes);
i++;
yield return new WaitForSeconds(1f);
}
}
/// <summary>
/// 得到伺服器回應
/// </summary>
/// <returns>The info.</returns>
public IEnumerator getInfo()
{
while(true)
{
byte[] revBytes = new byte[1024];
int bytes = clientSocket.Receive(revBytes, revBytes.Length, 0);
string revStr ="";
//revStr += Encoding.ASCII.GetString(revBytes, 0, bytes);
revStr += UTF8Encoding.UTF8.GetString(revBytes,0,bytes);
Debug.Log ("接收到伺服器訊息:"+revStr);
text.text = "From Server:"+revStr;
yield return null;
}
}
}
伺服器端運行結果:
伺服器回送訊息代碼:string strSend = "Hello Android Client!" + DateTime.Now.Second;
區域網路下測試沒有什麼問題,以後無論是做應用還是網路遊戲肯定少不了的是Socket網路資料轉送,多瞭解點網路知識還是很有必要的尤其是TCP協議,如有錯誤,歡迎指正。
本文永久更新連結地址: