//Connect to the client
public void connect()
{
if (!clientConnected)
{
IPAddress ipAddress = IPAddress.Any;
TcpListener listener = new TcpListener(ipAddress, portSend);
listener.Start();
Console.WriteLine("Server is running");
Console.WriteLine("Listening on port " + portSend);
Console.WriteLine("Waiting for connections...");
while (!clientConnected)
{
s = listener.AcceptSocket();
s.SendBufferSize = 256000;
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[65535];
int k = s.Receive(b);
ASCIIEncoding enc = new ASCIIEncoding();
Console.WriteLine("Received:" + enc.GetString(b, 0, k) + "..");
//Ensure the client is who we want
if (enc.GetString(b, 0, k) == "hello" || enc.GetString(b, 0, k) == "hellorcomplete")
{
clientConnected = true;
Console.WriteLine(enc.GetString(b, 0, k));
}
}
}
}