라즈베리파이에서 Visual Studio를 이용하여 C#을 올릴 경우 UWP(Universal Windows Platform)을 사용하게 됩니다. 아쉬운 점은 많은 기존 API들을 아직 지원하지 않는 점입니다. 이에 간단한 Client Sample을 하나 공유합니다.
아래는 동작 코드가 아니며, 프로그램 코드의 일부로 Socket 및 Class를 닫거나, 수신을 정지하는 상태 등은 임의로 만드시면 될 것 같습니다.
감사합니다.
This code is a sample code of the UWP for Windows IOT which does not support many previous APIs.
The sample code is a part of the whole-code and cannot be compliled thus please refer it and write your own code for closing class and sockets, and receiving conditions.
Thank you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using Windows.Networking.Sockets; using Windows.Networking; using Windows.Storage.Streams; using System.Text.RegularExpressions; using Windows.Storage; using Windows.Devices.Gpio; namespace Sample { class ClientSample { private static Windows.Networking.Sockets.StreamSocket gsocket = null ; private static StreamWriter writer = null ; private static StreamReader reader = null ; private static Stream streamOut = null ; private static Stream streamIn = null ; private string CRLF = "\x0d\x0a" ; private async Task MakeConnection() { MainPage.StaticLogBox.Text += ( "\nNEW CONNECTION" ); gsocket = new Windows.Networking.Sockets.StreamSocket(); string strIP = "192.168.0.5" stinrg iLLZGP_Port = 3306; Windows.Networking.HostName serverHost = new Windows.Networking.HostName(strIP); await gsocket.ConnectAsync(serverHost, Convert.ToString(iLLZGP_Port)); streamOut = gsocket.OutputStream.AsStreamForWrite(); writer = new StreamWriter(streamOut); streamIn = gsocket.InputStream.AsStreamForRead(); reader = new StreamReader(streamIn); } public async Task LoginToServer() { string strID = "ID" ; string strPassword = "Password" ; string strReq = "<Request mode='INIT' target='Login'><ID>" + strID + "</ID><PW>" + strPassword + "</PW><ClientType>2</ClientType></Request>\n" + CRLF; try { string RecvText = "" ; RecvText = await SendandReceive(strReq, RecvText); MainPage.StaticLogBox.Text += ( "\nRequest:" + strReq); MainPage.StaticLogBox.Text += ( "\nResponse:" + RecvText); } } catch (Exception e) { MainPage.StaticLogBox.Text += ( "LOGIN SVR Receive failed with error: " + e.Message); } } private async Task<String> SendandReceive( string SendText, string received) { try { await writer.WriteAsync(SendText); await writer.FlushAsync(); string readText = "" ; while ((readText = await reader.ReadLineAsync()) != null ) { received += readText; if (readText.Contains( "</RESPONSE" )) // EOF or Condition of the last sentance { break ; } } IsActive = true ; } catch (Exception e) { //Handle exception here. MainPage.StaticLogBox.Text += ( "Send & Receive failed with error: " + SendText + " / " + e.Message); if (streamIn != null ) { streamIn.Dispose(); streamIn = null ; } if (streamOut != null ) { streamOut.Dispose(); streamOut = null ; } if (writer != null ) { writer.Dispose(); writer = null ; } if (reader != null ) { reader.Dispose(); reader = null ; } gsocket.Dispose(); gsocket = null ; IsActive = false ; return "" ; } return received; } } } |