I have created a simple basic program that uses sockets but it can only handle one connection, how do I make it so it handles multiple connections?
Server code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string input = "";
string message;
byte[] buffer=new byte[250];
IPHostEntry local = Dns.GetHostByName(Dns.GetHostName());
IPEndPoint iep = new IPEndPoint(local.AddressList[0],8000);
Socket newServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newServer.Bind(iep);
newServer.Listen(10);
Socket newClient = newServer.Accept();
if (newClient.Connected)
{
newClient.Receive(buffer);
message = Encoding.ASCII.GetString(buffer);
Console.WriteLine(message);
newClient.Send(Encoding.ASCII.GetBytes("Hello client"));
}
newClient.Close();
}
}
}
client code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int number=1;
string messageClient;
byte[] bufferClient=new byte[200];
IPAddress ip = IPAddress.Parse("192.168.30.127");
IPEndPoint ie = new IPEndPoint(ip, 8000);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ie);
if (client.Connected)
{
client.Send(Encoding.ASCII.GetBytes("hello server"));
client.Receive(bufferClient);
messageClient = Encoding.ASCII.GetString(bufferClient);
Console.WriteLine(messageClient);
}
client.Close();
}
}
}