0% found this document useful (0 votes)
52 views6 pages

C# Client-Server App with System.Net

This is my client server application network in c sharp and WCF Documentation.

Uploaded by

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

C# Client-Server App with System.Net

This is my client server application network in c sharp and WCF Documentation.

Uploaded by

Kashaf Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A Simple Example of a Client-Server

Application Using the `[Link]`


Library In C#
This is a basic example to demonstrate how to create a simple client-server application using the
`[Link]` library in C#.
This example includes a server that listens for incoming connections and a client that connects to the
server and sends a message.

Server Code

First, let's create the server that listens for incoming connections:

using System;
using [Link];
using [Link];
using [Link];
using [Link];

public class Server


{
public static void Main()
{
[Link](() => StartServer());
[Link]("Press Enter to quit...");
[Link]();
}

public static void StartServer()


{
var listener = new TcpListener([Link], 5000);
[Link]();
[Link]("Server started, waiting for connections...");

while (true)
{
var client = [Link]();
[Link](() => HandleClient(client));
}
}

public static void HandleClient(TcpClient client)


{
var buffer = new byte[1024];
var stream = [Link]();

int bytesRead = [Link](buffer, 0, [Link]);


string message = [Link](buffer, 0, bytesRead);
[Link]($"Received: {message}");

string response = "Hello from server!";


byte[] responseBytes = [Link](response);
[Link](responseBytes, 0, [Link]);

[Link]();
}
}

Client Code

Next, let's create the client that connects to the server and sends a message:

using System;
using [Link];
using [Link];

public class Client


{
public static void Main()
{
try
{
var client = new TcpClient("[Link]", 5000);
var stream = [Link]();

string message = "Hello from client!";


byte[] messageBytes = [Link](message);
[Link](messageBytes, 0, [Link]);

var buffer = new byte[1024];


int bytesRead = [Link](buffer, 0, [Link]);
string response = [Link](buffer, 0, bytesRead);
[Link]($"Received: {response}");

[Link]();
}
catch (Exception ex)
{
[Link]($"Exception: {[Link]}");
}
}
}

Explanation

Server Code:
TcpListener: Listens for incoming connections on port 5000.
HandleClient: Handles each client connection in a separate task. Reads the incoming message and sends
a response back to the client.

Client Code:
TcpClient: Connects to the server at `[Link]` on port 5000.
Send Message: Sends a message to the server and reads the response.

Running the Example

1. Start the Server: Run the server application first. It will start listening for incoming connections.
2. Run the Client: Run the client application. It will connect to the server, send a message, and print the
server's response.
Windows Communication Foundation
(WCF).
WCF provides a unified programming model for building service-oriented applications and supports a
variety of communication protocols, including HTTP, TCP, and MSMQ.

Example: Simple WCF Service

This example demonstrates how to create a simple WCF service and client. WCF is a powerful
framework for building distributed applications and is the recommended approach for replacing .NET
Remoting
Here's a basic example of a WCF service and a client:

Step 1: Define the Service Contract

Create a new class library project and define the service contract:

using [Link];

namespace WcfService
{
[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
double Add(double a, double b);
}
}

Step 2: Implement the Service

Implement the service contract in a class:


using [Link];
using [Link];
namespace WcfService
{
public class CalculatorService : ICalculatorService
{
public double Add(double a, double b)
{
return a + b;
}
}
}

Step 3: Configure the Service

Create a configuration file (`[Link]` or `[Link]`) to configure the service:


<[Link]>
<services>
<service name="[Link]">
<endpoint address="" binding="basicHttpBinding" contract="[Link]"/>
<host>
<baseAddresses>
<add baseAddress="[Link]
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</[Link]>

Step 4: Create the Client

Create a new console application project for the client:

using System;
using [Link];
namespace WcfClient
{
class Program
{
static void Main(string[] args)
{
var address = new Uri("[Link]
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress(address);
var channelFactory = new ChannelFactory<ICalculatorService>(binding, endpoint);
var client = [Link]();

double result = [Link](10, 5);


[Link]($"Result: {result}");
}
}
}

Explanation

1. Service Contract: Defines the operations the service will provide.


2. Service Implementation: Implements the service contract.
3. Service Configuration: Configures the service to use HTTP binding and sets the base address.
4. Client: Creates a client to consume the service.

You might also like