using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace MyClient
{
class client
{
static void Main(string[] args)
{
Console.WriteLine("This is the client");
Socket master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
master.Connect(ipEnd)
string sendData = "";
do
{
Console.WriteLine("Data to send: ");
sendData = Console.ReadLine();
master.Send(Encoding.UTF8.GetBytes(sendData));
//getting the reply
byte[] rdt = new byte[4];
master.Receive(rdt);
Console.WriteLine("our reply is: " + Encoding.UTF8.GetString(rdt));
} while (sendData.Length>0);
master.Close();
//Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MyServer
{
class Server
{
static void Main(string[] args)
{
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
listenerSocket.Bind(ipEnd);
while(true)
{
listenerSocket.Listen(0);
Socket clientSocket = listenerSocket.Accept();
// runs the clients as threads
Thread clientThead = new Thread((c)=> ClientConnection(clientSocket));
clientThead.Start();
}
}
private static void ClientConnection(Socket clientSocket)
{
byte[] Buffer = new byte[clientSocket.SendBufferSize];
int readByte;
do
{
// received data
readByte = clientSocket.Receive(Buffer);
// do stuff
byte[] rData = new byte[readByte];
Array.Copy(Buffer, rData, readByte);
Console.WriteLine("We got " + Encoding.UTF8.GetString(rData));
//replay
clientSocket.Send(new byte[4] { 65, 66, 67, 68 });
} while (readByte > 0);
Console.WriteLine("Client disconnected");
Console.ReadKey();
}
}
}