SimpleTcpClient.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SimpleTcpClient
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse(“127.0.0.1″), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
} catch (SocketException e)
{
Console.WriteLine(“Unable to connect to server.”);
Console.WriteLine(e.ToString());
return;
}
int recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
while (true)
{
try
{
input = Console.ReadLine();
if (input == “exit”)
break;
server.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
catch (Exception e)
{
}
}
Console.WriteLine(“Disconnecting from server…”);
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SimpleTcpClient
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse(“127.0.0.1″), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ipep);
} catch (SocketException e)
{
Console.WriteLine(“Unable to connect to server.”);
Console.WriteLine(e.ToString());
return;
}
int recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
while (true)
{
try
{
input = Console.ReadLine();
if (input == “exit”)
break;
server.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
catch (Exception e)
{
}
}
Console.WriteLine(“Disconnecting from server…”);
server.Shutdown(SocketShutdown.Both);
server.Close();
}
}
BadTcpSrvr.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class BadTcpSrvr
{
public static void Main()
{
int recv1;
string input, stringData;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine(“Waiting for a client…”);
Socket client = newsock.Accept();
string welcome = “Welcome to my test server”;
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length, SocketFlags.None);
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine(“Connected with {0} at port {1}”,
newclient.Address, newclient.Port);
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class BadTcpSrvr
{
public static void Main()
{
int recv1;
string input, stringData;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine(“Waiting for a client…”);
Socket client = newsock.Accept();
string welcome = “Welcome to my test server”;
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length, SocketFlags.None);
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine(“Connected with {0} at port {1}”,
newclient.Address, newclient.Port);
int recv = client.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
while (true)
{
try
{
input = Console.ReadLine();
if (input == “exit”)
break;
client.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = client.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
catch (Exception e)
{
}
}
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
while (true)
{
try
{
input = Console.ReadLine();
if (input == “exit”)
break;
client.Send(Encoding.ASCII.GetBytes(input));
data = new byte[1024];
recv = client.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
catch (Exception e)
{
}
}
for (int i = 0; i < 5; i++)
{
recv1 = client.Receive(data);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv1));
}
Console.WriteLine(“Disconnecting from {0}”, newclient.Address);
client.Close();
newsock.Close();
}
}
{
recv1 = client.Receive(data);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv1));
}
Console.WriteLine(“Disconnecting from {0}”, newclient.Address);
client.Close();
newsock.Close();
}
}