Ciao a tutti!
Sto cercando di imparare a scrivere applicazioni C# che comunichino tra loro, attraverso la classe Socket. Sono riuscito, seguendo un esempio, a fare un client e un server che comunicano tra loro scrivendosi a vicenda "Ping" e "PONG". Ma come posso fare per far comunicare il server con tutti i client che si connettono anche in momenti diversi? Se ho capito bene, il momento in cui il server "vede" il client è quando, dopo aver creato un TcpListener, creo un Socket dandogli come valore il metodo AcceptSocket dell'oggetto tcpListener. Ma come faccio a fare in modo che ogni volta che un client nuovo prova a connettersi, il server crei un Socket per lui? E inoltre, per far sì che il server mandi un output a tutti i client connessi, mi conviene forse creare un array di Socket?
Riporto qui il codice che ho scritto:
Server:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace ChatServer
{
public partial class Form1 : Form
{
StreamWriter sw = null;
StreamReader sr = null;

public Form1()
{
InitializeComponent();
}

private void btnConnect_Click(object sender, EventArgs e)
{
if (!startServer())
Console.WriteLine("Unable to start server");

while (true)
{
Console.WriteLine("CLIENT: " + sr.ReadLine());
sw.WriteLine("Ping!");
sw.Flush();
}
}

private bool startServer()
{
TcpListener tcpListener = new TcpListener(8086);
tcpListener.Start();
Console.WriteLine("Server started");
btnConnect.Enabled = false;
Socket socket = tcpListener.AcceptSocket();

try
{
Console.WriteLine("Client connected");
NetworkStream ns = new NetworkStream(socket);
sw = new StreamWriter(ns);
sr = new StreamReader(ns);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return false;
}

return true;
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;

namespace ChatClient
{
public partial class Form1 : Form
{
StreamWriter sw = null;
StreamReader sr = null;
public Form1()
{
InitializeComponent();
}

private bool connectToServer()
{
try
{
//IPAddress[] ip = Dns.GetHostAddresses("http://www.google.it/");
TcpClient tcpClient = new TcpClient("localhost", 8086);
//Console.WriteLine("Connected to server");
textBox1.AppendText("Connected to server\n");
NetworkStream ns = tcpClient.GetStream();
sw = new StreamWriter(ns);
sr = new StreamReader(ns);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return false;
}

return true;
}

private void btnConnect_Click(object sender, EventArgs e)
{
if (!connectToServer())
Console.WriteLine("Unable to start server");

}

private void btnInvia_Click(object sender, EventArgs e)
{
try
{
sw.WriteLine("PONG!");
sw.Flush();
//Console.WriteLine("SERVER: " + sr.ReadLine());
textBox1.AppendText("SERVER: " + sr.ReadLine() + "\n");
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
}
Grazie in anticipo per il vostro aiuto!