C# Network Programming Tutorial40


## Introduction

Network programming in C# allows you to create applications that can communicate over a network. This can be useful for a variety of purposes, such as creating chat applications, file sharing applications, or web servers. In this tutorial, we will provide an introduction to network programming in C# by creating a simple client-server application.

## Creating a TCP Server

The first step in creating a network application is to create a server. A server is a program that listens for incoming connections from clients. To create a TCP server in C#, you can use the TcpListener class. The following code shows an example of how to create a TCP server that listens on port 13000:```c#
using System;
using ;
using ;
public class TcpServer
{
public static void Main()
{
// Create a TcpListener object to listen on port 13000
TcpListener listener = new TcpListener(, 13000);

// Start the TcpListener object
();

// Loop forever, waiting for incoming connections
while (true)
{
// Accept an incoming connection from a client
TcpClient client = ();

// Create a new thread to handle the client connection
new Thread(() => HandleClientConnection(client)).Start();
}
}
public static void HandleClientConnection(TcpClient client)
{
// Get the client's IP address
string ipAddress = ((IPEndPoint)).();

// Get the client's port number
int port = ((IPEndPoint)).Port;

// Print the client's IP address and port number
("Client connected from {0}:{1}", ipAddress, port);

// Create a stream to read and write data to the client
NetworkStream stream = ();

// Send a welcome message to the client
byte[] welcomeMessage = ("Welcome to the TCP server!");
(welcomeMessage, 0, );

// Read data from the client until the connection is closed
while (true)
{
byte[] buffer = new byte[1024];
int bytesRead = (buffer, 0, );
if (bytesRead == 0)
{
break;
}

// Process the data received from the client
string data = (buffer, 0, bytesRead);
("Received data from client: {0}", data);

// Send a response to the client
byte[] response = ("Hello, client!");
(response, 0, );
}

// Close the client connection
();
}
}
```

## Creating a TCP Client

The next step in creating a network application is to create a client. A client is a program that connects to a server and sends and receives data. To create a TCP client in C#, you can use the TcpClient class. The following code shows an example of how to create a TCP client that connects to the server created in the previous section:```c#
using System;
using ;
using ;
public class TcpClient
{
public static void Main()
{
// Create a TcpClient object to connect to the server
TcpClient client = new TcpClient();

// Connect to the server on port 13000
("127.0.0.1", 13000);

// Get the client's IP address
string ipAddress = ((IPEndPoint)).();

// Get the client's port number
int port = ((IPEndPoint)).Port;

// Print the client's IP address and port number
("Client connected to {0}:{1}", ipAddress, port);

// Create a stream to read and write data to the server
NetworkStream stream = ();

// Send a message to the server
byte[] message = ("Hello, server!");
(message, 0, );

// Read data from the server until the connection is closed
while (true)
{
byte[] buffer = new byte[1024];
int bytesRead = (buffer, 0, );
if (bytesRead == 0)
{
break;
}

// Process the data received from the server
string data = (buffer, 0, bytesRead);
("Received data from server: {0}", data);
}

// Close the client connection
();
}
}
```

## Conclusion

In this tutorial, we have provided an introduction to network programming in C# by creating a simple client-server application. We have shown how to create a TCP server using the TcpListener class and how to create a TCP client using the TcpClient class. We have also shown how to send and receive data between the client and server. This tutorial provides a foundation for creating more complex network applications in C#.

2024-11-20


Previous:Cloud Computing with IBM: A Comprehensive Guide

Next:Bluetooth Development Guide: Step-by-Step Tutorial for Beginners