Simples TCP SERVER em C#
Publicado por Roberson Carlos 16/07/2007
[ Hits: 8.321 ]
Homepage: http://carlrobers.wordpress.com
Neste script mostro com implementar um funcional servidor TCP fazendo uso dos namespaces System.Net, System.Net.Sockets, e claro... em CSharp.
/*
// Roberson Carlos Fox AKA robersonfox
// robersonfox@genovaengine.com
// www.genovaengine.com
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SimpleTcpSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
IPAddress ia = IPAddress.Parse("192.168.0.39");
IPEndPoint ipep = new IPEndPoint(ia, 8000);
Socket newsock = new
Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine("Aguardando cliente...");
Socket client = newsock.Accept();
IPEndPoint clientep =
(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Conectado com {0} na porta {1}",
clientep.Address, clientep.Port);
string welcome = "Welcome to the Genova Engine Server";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length, SocketFlags.None);
try
{ while(true)
{
data = new byte[1024];
recv = client.Receive(data);
if (recv == 0)
break;
Console.WriteLine(
Encoding.ASCII.GetString(data, 0, recv));
client.Send(data, recv, SocketFlags.None);
}}catch (Exception e) {
}
// Console.WriteLine("Disconnected from {0}",
// clientep.Address);
//client.Close();
//newsock.Close();
}
}
Nenhum comentário foi encontrado.
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
O que houve com slackware ??? (12)
Alterar conteúdo de dica [RESOLVIDO] (3)
Vou destruir sua infância:) (5)









