public async Task Run() { await _client.ConnectAsync("13.81.65.60", 4088); _console.WriteLine("client is connected"); try { var buffer = new byte[BufferSize]; var offset = 0; var actuallyRead = 0; do { actuallyRead = await _client.GetStream().ReadAsync(buffer, 0, BufferSize); offset += actuallyRead; _console.WriteLine(offset.ToString()); } while (actuallyRead != 0); //_track.Stop(); _console.WriteLine("Completed"); } catch (Exception exception) { _console.WriteLine(exception.Message); throw; } }
/// <summary> /// Clients the message send. /// </summary> /// <param name="s">S.</param> /// <param name="e">E.</param> public async void ClientConnection_MessageSend(Object s, EventArgs e, string anser) { var address = "127.0.0.1"; TcpSocketClient client = new TcpSocketClient(); await client.ConnectAsync(address, Port); //NetworkStreamを取得する // Uses the GetStream public method to return the NetworkStream. System.IO.Stream gs = client.GetStream(); //文字列をByte型配列に変換 System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] sendBytes = enc.GetBytes(anser + '\n'); //データを送信する gs.Write(sendBytes, 0, sendBytes.Length); await client.WriteStream.FlushAsync(); //await client.DisconnectAsync(); }
//******************************************************************************************** //********************************************** Client ************************************** /// <summary> /// Clients the connection. /// </summary> /// <param name="sender">Sender.</param> /// <param name="args">Arguments.</param> public async void ClientConnection(Object sender, EventArgs args) { var address = "127.0.0.1"; IpAddressInput(); TcpSocketClient client = new TcpSocketClient(); await client.ConnectAsync(address, Port); //NetworkStreamを取得する // Uses the GetStream public method to return the NetworkStream. clientSocket = client.GetStream(); //クライアントがサーバとのデータのやり取りに使うストリームを取得 System.IO.Stream ns = client.GetStream(); clientReader = new StreamReader(ns, Encoding.UTF8); clientWriter = new StreamWriter(ns, Encoding.UTF8); var sendMsg = "+OK to BondotConnectio!"; //サーバーにデータを送信する //文字列をByte型配列に変換 System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] sendBytes = enc.GetBytes(sendMsg + '\n'); //データを送信する************************************ clientSocket.Write(sendBytes, 0, sendBytes.Length); clientSocket.Flush(); Client_Disp.Text = sendMsg; Client_Send.IsEnabled = true; //リステナーからのデータを受信する var buf = new byte[1024]; var r = clientSocket.Read(buf, 0, buf.Length); ////受信したデータを文字列に変換 Encoding enr = System.Text.Encoding.UTF8; string resMsg = enr.GetString(buf, 0, r); ////末尾の\nを削除 resMsg = resMsg.TrimEnd('\n'); Client_Disp.Text = resMsg; if (resMsg == "+OK to BondotConnectio!listener-Anser")//Client ID Check { await DisplayAlert("For Client Message ", "Listenerと接続しました", "OK"); Client_Conect.Text = "接続中"; Client_Conect.IsEnabled = false; Client_Conect.TextColor = Color.Red; Device.BeginInvokeOnMainThread(() => { Task.Run(() => { ClientListen(clientReader);// 重たい処理のつもり*/ }); }); } //クライアントに接続応答する*********************** // System.IO.Stream ns = serverSocket.GetStream(); //serverWriter = new StreamWriter(ns, Encoding.UTF8); // sendMsg = resMsg + "listener-Anser" + '\n'; //文字列をByte型配列に変換 // byte[] sendBytes = enc.GetBytes(sendMsg + "listener-Anser" + '\n'); // SendMessage(serverWriter, sendMsg); // wait a little before sending the next bit of data //await Task.Delay(TimeSpan.FromMilliseconds(500)); //await client.DisconnectAsync(); }