byte[] bytes = new Byte[1024]; // Buffer per contenere i dati in arrivo dal client
// Ottengo l'IP di rete LAN della macchina ed uso la porta 11000
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipLocale = ipHostInfo.AddressList[0];
IPEndPoint EPLocale = new IPEndPoint(ipLocale, 11000);
textBox2.Text = ipLocale.ToString();
// Configuro il socket TCP
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
// Imposto il server sull'endpoint locale ed attendo la richiesta del client
server.Bind(EPLocale);
server.Listen(10);
// Controllo se ci sono connessioni
while (true)
{
Socket handler = server.Accept();
data = null;
// Una nuova connessione deve essere processata
while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
break;
}
}
// Stampo i dati ricevuti dal client
data = data.Replace("<EOF>", "");
textBox1.Text = data;
// Invio la risposta al client
data = "Dati ricevuti da " + ipLocale.ToString();
byte[] risposta = Encoding.ASCII.GetBytes(data);
handler.Send(risposta);
// Chiudo il socket
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Si è verificato un errore", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Trying to push the limits of App Inventor!
Snippets,
Tutorials and
Extensions from
Pura Vida Apps by
Taifun.