diff --git a/TCP/Client.cs b/TCP/Client.cs new file mode 100644 index 0000000..44eeb80 --- /dev/null +++ b/TCP/Client.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net; +using System.Net.Sockets; +using System.IO; + +namespace tcpclientexample +{ + class Program + { + static void Main(string[] args) + { + try + { + TcpClient tcpClient = new TcpClient(); + Console.WriteLine("Connecting...."); + + tcpClient.Connect("127.0.0.1", 8888); + + Console.WriteLine("Connected"); + Console.Write("Enter the string to be transmitted: "); + + String str = Console.ReadLine(); + Stream stm = tcpClient.GetStream(); + + ASCIIEncoding asen = new ASCIIEncoding(); + byte[] ba = asen.GetBytes(str); + + Console.WriteLine("Transmitting...."); + stm.Write(ba, 0, ba.Length); + + byte[] bb = new byte[100]; + int k = stm.Read(bb, 0, 100); + + for (int i = 0; i < k; i++) + { + Console.Write(Convert.ToChar(bb[i])); + } + + tcpClient.Close(); + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.StackTrace); + } + } + } +} diff --git a/TCP/Server.cs b/TCP/Server.cs new file mode 100644 index 0000000..e5f2340 --- /dev/null +++ b/TCP/Server.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net; +using System.Net.Sockets; + +namespace tcpserverexample +{ + class Program + { + static void Main(string[] args) + { + try + { + TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8888); + tcpListener.Start(); + + Console.WriteLine("The server is running at port 8888..."); + Console.WriteLine("The local End point is :" + tcpListener.LocalEndpoint); + Console.WriteLine("Waiting for a connection..."); + + Socket client = tcpListener.AcceptSocket(); + Console.WriteLine("Connection accepted from " + client.RemoteEndPoint); + + byte[] b = new byte[100]; + int k = client.Receive(b); + + Console.WriteLine("Received..."); + for (int i = 0; i < k; i++) + { + Console.Write(Convert.ToChar(b[i])); + } + + ASCIIEncoding asen = new ASCIIEncoding(); + client.Send(asen.GetBytes("The string was recieved by the server")); + Console.WriteLine("Sent Acknolwedgement"); + client.Close(); + tcpListener.Stop(); + + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.StackTrace); + } + } + } +}