0% found this document useful (0 votes)
29 views

Client Server - 201014

This document contains code for a client-server application in C# that allows them to communicate over TCP. The client code connects to a server listening on port 5555, sends the message "Hello To World !", receives a response from the server, and prints it to the console. The server code listens on port 5555 for incoming connections, receives data from clients, sends back the response "Hello, World!", and closes the socket.

Uploaded by

201014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Client Server - 201014

This document contains code for a client-server application in C# that allows them to communicate over TCP. The client code connects to a server listening on port 5555, sends the message "Hello To World !", receives a response from the server, and prints it to the console. The server code listens on port 5555 for incoming connections, receives data from clients, sends back the response "Hello, World!", and closes the socket.

Uploaded by

201014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Client:

using System;

using System.Net;

using System.Net.Sockets;

using System.Text;

class Program

static void Main()

// Define the server host and port

string serverAddress = "127.0.0.1";

int serverPort = 5555;

// Create a TCP/IP socket and bind it to the server address and port

TcpListener serverSocket = new TcpListener(IPAddress.Parse(serverAddress), serverPort);

serverSocket.Start();

Console.WriteLine("Server is listening on {0}:{1}", serverAddress, serverPort);

// Accept incoming connections and handle them

while (true)

TcpClient clientSocket = serverSocket.AcceptTcpClient();

Console.WriteLine("Accepted connection from {0}:{1}",


((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address, ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Port);

NetworkStream networkStream = clientSocket.GetStream();

byte[] receiveBuffer = new byte[1024];

int bytesRead = networkStream.Read(receiveBuffer, 0, receiveBuffer.Length);

string dataReceived = Encoding.UTF8.GetString(receiveBuffer, 0, bytesRead);

Console.WriteLine(dataReceived);
// Send a response back to the client

byte[] responseBuffer = Encoding.UTF8.GetBytes("Hello, World!");

networkStream.Write(responseBuffer, 0, responseBuffer.Length);

// Close the client socket

clientSocket.Close();

Server:
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

class Program
{
static void Main()
{
// Define the server host and port
string serverHost = "127.0.0.1";
int serverPort = 5555;

try
{
// Create a TCP/IP socket
using (TcpClient client = new TcpClient(serverHost, serverPort))
using (NetworkStream stream = client.GetStream())
{
// Send data to the server
byte[] dataToSend = Encoding.UTF8.GetBytes("Hello To World !");
stream.Write(dataToSend, 0, dataToSend.Length);

// Receive data from the server


byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine(response);
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception occurred: {ex.Message}");
}
}
}

CLIENT:

SERVER:

You might also like