Accepts a pending connection request
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class TcpListenerSample
{
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Any, 13000);
server.Start();
while (true)
{
byte[] bytes = new byte[1024];
string data;
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i = stream.Read(bytes, 0, bytes.Length);
while (i != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine(String.Format("Sent: {0}", data));
i = stream.Read(bytes, 0, bytes.Length);
}
client.Close();
}
Console.WriteLine("Hit enter to continue...");
Console.Read();
}
}
Related examples in the same category