Creado un server sincrono, crea un socket tcp en el puerto 11000
This commit is contained in:
parent
8fa955a937
commit
cb611a6536
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,7 +1,5 @@
|
|||||||
# ---> VisualStudioCode
|
# ---> VisualStudioCode
|
||||||
.vscode/*
|
ServerSYNC/.vscode
|
||||||
!.vscode/settings.json
|
ServerSYNC/bin
|
||||||
!.vscode/tasks.json
|
ServerSYNC/obj
|
||||||
!.vscode/launch.json
|
|
||||||
!.vscode/extensions.json
|
|
||||||
|
|
||||||
|
75
ServerSYNC/Program.cs
Normal file
75
ServerSYNC/Program.cs
Normal 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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
ServerSYNC/ServerSYNC.csproj
Normal file
8
ServerSYNC/ServerSYNC.csproj
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user