csharp网络编程实验
时间: 2023-06-13 09:05:27 浏览: 97
网络编程是现代计算机应用程序开发中不可或缺的一部分。C#作为一种高级编程语言,提供了许多用于网络编程的类和API,如System.Net和System.Net.Sockets命名空间。
下面是一个简单的C#网络编程实验,用于建立一个TCP/IP客户端和服务器之间的连接,并在客户端和服务器之间发送消息。
1. 创建一个新的C#控制台应用程序项目。
2. 在Program.cs文件中添加以下代码,用于创建一个TCP/IP服务器:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}
```
3. 添加以下代码,用于创建TCP/IP客户端:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
string server = "127.0.0.1";
int port = 13000;
try
{
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
string message = "Hello, server!";
Byte[] data = Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}
```
4. 运行程序,将启动服务器并等待客户端连接。然后启动客户端,将向服务器发送一条消息并显示响应。
这个实验只是一个简单的例子,但它演示了如何使用C#编写基本的网络应用程序。你可以使用这些基础知识来构建更复杂的应用程序,如聊天程序、文件传输应用程序等。
阅读全文