This
in continuation to my previous post. In previous post, I explained how to send
some information from client to server. In this post, I am going to give an
example, where client send a string to server, server converts the string to
upper case and send back to client. Client prints the response to console.
Server.cs
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace Server { public class ServerSocket { private int port; private Socket serverSocket; public ServerSocket(int port) { this.port = port; serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); /* Associates a Socket with a local endpoint. */ serverSocket.Bind(serverEndPoint); /*Places a Socket in a listening state. * The maximum length of the pending connections queue is 100 */ serverSocket.Listen(100); } public void start() { Console.WriteLine("Starting the Server"); /* Accept Connection Requests */ Socket accepted = serverSocket.Accept(); /* Get the size of the send buffer of the Socket. */ int bufferSize = accepted.SendBufferSize; byte[] buffer = new byte[bufferSize]; /* Receives data from a bound Socket into a receive buffer. It return the number of bytes received. */ int bytesRead = accepted.Receive(buffer); byte[] formatted = new byte[bytesRead]; for (int i = 0; i < bytesRead; i++) { formatted[i] = buffer[i]; } String receivedData = Encoding.UTF8.GetString(formatted); Console.WriteLine("Received Data " + receivedData); String response = receivedData.ToUpper(); byte[] resp = Encoding.UTF8.GetBytes(response); accepted.Send(resp, 0, resp.Length, 0); Console.WriteLine("Press some key to close"); Console.Read(); } } class Server { static void Main(string[] args) { ServerSocket server = new ServerSocket(1234); server.start(); } } }
Client.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; namespace client { public class ClientSocket { private Socket clientSocket; private int port; public ClientSocket(int port) { this.port = port; clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public void start() { Console.WriteLine("Starting client socket"); try { IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); clientSocket.Connect(serverEndPoint); Console.WriteLine("Enter some data to send to server"); String data = Console.ReadLine(); byte[] bytes = Encoding.UTF8.GetBytes(data); clientSocket.Send(bytes); int receiveBufferSize = clientSocket.ReceiveBufferSize; byte[] buffer = new byte[receiveBufferSize]; int receivedBytes = clientSocket.Receive(buffer); byte[] receivedData = new byte[receivedBytes]; for (int i = 0; i < receivedBytes; i++) { receivedData[i] = buffer[i]; } String received = Encoding.UTF8.GetString(receivedData); Console.WriteLine("Response : {0}", received); Console.WriteLine("Press Enter to close"); Console.WriteLine("Closing connection"); Console.Read(); clientSocket.Close(); } catch (Exception e) { Console.WriteLine("Error while connectiong to server {0}", e.Message); } } } class Client { static void Main(string[] args) { ClientSocket clientSocket = new ClientSocket(1234); clientSocket.start(); } } }
Run
Server.cs, server starts listening on port.
Run
Client.cs and enter some data, it get the response in uppercase.
Sample Output
Starting
client socket
Enter
some data to send to server
Hello
World
Response
: HELLO WORLD
Press
Enter to close
Closing
connection
No comments:
Post a Comment