Este cliente envia datos al servidor sincrono

This commit is contained in:
Jose Pino 2018-07-18 23:44:59 -04:00
parent cb611a6536
commit 03a7ed1b56
3 changed files with 74 additions and 0 deletions

3
.gitignore vendored
View File

@ -3,3 +3,6 @@ ServerSYNC/.vscode
ServerSYNC/bin
ServerSYNC/obj
ClientSYNC/.vscode
ClientSYNC/bin
ClientSYNC/obj

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>

63
ClientSYNC/Program.cs Normal file
View File

@ -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 !!! <EOF>");
// 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());
}
}
}
}