From 03a7ed1b568f5eefd064cf891f9f291b0e7342f7 Mon Sep 17 00:00:00 2001 From: Jose Pino Date: Wed, 18 Jul 2018 23:44:59 -0400 Subject: [PATCH] Este cliente envia datos al servidor sincrono --- .gitignore | 3 ++ ClientSYNC/ClientSYNC.csproj | 8 +++++ ClientSYNC/Program.cs | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 ClientSYNC/ClientSYNC.csproj create mode 100644 ClientSYNC/Program.cs diff --git a/.gitignore b/.gitignore index 1403e2f..0a37270 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ ServerSYNC/.vscode ServerSYNC/bin ServerSYNC/obj +ClientSYNC/.vscode +ClientSYNC/bin +ClientSYNC/obj diff --git a/ClientSYNC/ClientSYNC.csproj b/ClientSYNC/ClientSYNC.csproj new file mode 100644 index 0000000..e5e0e16 --- /dev/null +++ b/ClientSYNC/ClientSYNC.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/ClientSYNC/Program.cs b/ClientSYNC/Program.cs new file mode 100644 index 0000000..b13636a --- /dev/null +++ b/ClientSYNC/Program.cs @@ -0,0 +1,63 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace ClientSYNC +{ + class Program + { + static void Main(string[] args) + { + StartClient(); + } + + public static void StartClient() { + // Datos recibidos desde el servidor + byte[] bytes = new byte[1024]; + + try { + // Datos de la IP y puerto del servidor + //IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); + //IPAddress ipAddress = ipHostInfo.AddressList[0]; + + IPAddress ipAddress = IPAddress.Parse("192.168.0.20"); + IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000); + + // TCP/IP socket. + Socket sender = new Socket(ipAddress.AddressFamily, + SocketType.Stream, ProtocolType.Tcp ); + + try { + sender.Connect(remoteEP); + + Console.WriteLine("Socket conectado con (ip:puerto) {0}", sender.RemoteEndPoint.ToString()); + + // El texto a enviar se pasa a un arreglo de bytes + byte[] msg = Encoding.ASCII.GetBytes("Hola Mundo, desde socket !!! "); + + // se envian los datos (msg) por medio del socket + int bytesSent = sender.Send(msg); + + // Recibe la respuesta del dispositivo remoto. + int bytesRec = sender.Receive(bytes); + Console.WriteLine("Datos recibidos = {0}", Encoding.ASCII.GetString(bytes,0,bytesRec)); + + // Libera el socket. + sender.Shutdown(SocketShutdown.Both); + sender.Close(); + + } catch (ArgumentNullException ane) { + Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); + } catch (SocketException se) { + Console.WriteLine("SocketException : {0}",se.ToString()); + } catch (Exception e) { + Console.WriteLine("Unexpected exception : {0}", e.ToString()); + } + + } catch (Exception e) { + Console.WriteLine( e.ToString()); + } + } + } +}