Created
January 9, 2017 20:28
-
-
Save georgepaoli/eccff1e1901cbd1c10767f27e5aa5725 to your computer and use it in GitHub Desktop.
TCP/IP - Socker Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net.Sockets; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace Client | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Console.Title = "TCP Echo Client"; | |
| var client = new TcpClient("127.0.0.1", 9802); | |
| while (true) | |
| { | |
| var msg = Console.ReadLine(); | |
| var bytesToSend = Encoding.ASCII.GetBytes(msg); | |
| var stream = client.GetStream(); | |
| stream.Write(bytesToSend, 0, bytesToSend.Length); | |
| Console.WriteLine("Sent {0} bytes to server", bytesToSend.Length); | |
| int totalBytesReceived = 0; | |
| var responseBuffer = new byte[bytesToSend.Length]; | |
| while (totalBytesReceived < bytesToSend.Length) | |
| { | |
| var bytesReceived = stream.Read(responseBuffer, totalBytesReceived, bytesToSend.Length - totalBytesReceived); | |
| totalBytesReceived = bytesReceived; | |
| } | |
| Console.WriteLine("Received {0} bytes from server: {1}", totalBytesReceived, Encoding.ASCII.GetString(responseBuffer, 0, totalBytesReceived)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment