Creado un server sincrono, crea un socket tcp en el puerto 11000

This commit is contained in:
Jose Pino 2018-07-18 23:23:42 -04:00
parent 8fa955a937
commit cb611a6536
3 changed files with 86 additions and 5 deletions

8
.gitignore vendored
View File

@ -1,7 +1,5 @@
# ---> VisualStudioCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
ServerSYNC/.vscode
ServerSYNC/bin
ServerSYNC/obj

75
ServerSYNC/Program.cs Normal file
View File

@ -0,0 +1,75 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ServerSYNC
{
class Program
{
// Data recibida desde el cliente
public static string data = null;
static void Main(string[] args)
{
StartListening();
}
public static void StartListening() {
// Arreglo como buffer de la data recibida del cliente
byte[] bytes = new Byte[1024];
// Se definen las variables del Host ip, puerto
// Al usar esto la IP es 127.0.0.1
//IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
//IPAddress ipAddress = ipHostInfo.AddressList[0];
// Se define la IP explicitamente
IPAddress ipAddress = IPAddress.Parse("192.168.0.20");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Se crea el socket de tipo TCP/IP sobre el puerto 11000.
Socket listener = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp );
// Se inicia el socket y queda a la escucha
try {
listener.Bind(localEndPoint);
listener.Listen(10);
while (true) {
Console.WriteLine("Esperando datos del Cliente...");
// El programa se suspende mientras espera una conexión entrante.
Socket handler = listener.Accept();
data = null;
// Una conexión entrante necesita ser procesada.
while (true) {
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1) {
break;
}
}
Console.WriteLine( "Data Recivida desde el Cliente : {0}", data);
// Se devuelven datos al cliente.
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine("\n Fin...");
Console.Read();
}
}
}

View File

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