64 lines
2.4 KiB
C#
64 lines
2.4 KiB
C#
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());
|
|
}
|
|
}
|
|
}
|
|
}
|