C#写一个TCP通信得程序
时间: 2023-08-02 11:36:48 浏览: 58
以下是一个简单的 C# TCP 通信的程序,其中包含了客户端和服务器端的代码。
服务器端代码:
```C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TCPServer
{
class Program
{
static void Main(string[] args)
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpListener serverSocket = new TcpListener(ipAddress, port);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
while (true)
{
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " + dataFromClient);
string serverResponse = "Last Message from client - " + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
}
}
```
客户端代码:
```C#
using System;
using System.Net.Sockets;
using System.Text;
namespace TCPClient
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient clientSocket = new TcpClient();
clientSocket.Connect("127.0.0.1", 8888);
Console.WriteLine(" >> Client Started");
string message = "Hello Server!";
Byte[] sendBytes = Encoding.ASCII.GetBytes(message);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromServer = Encoding.ASCII.GetString(bytesFrom);
dataFromServer = dataFromServer.Substring(0, dataFromServer.IndexOf("$"));
Console.WriteLine(" >> Data from server - " + dataFromServer);
networkStream.Flush();
clientSocket.Close();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
}
}
}
}
```
在运行这个程序之前,需要先运行服务器端程序,然后再运行客户端程序。在成功运行之后,客户端将会发送消息“Hello Server!”给服务器端,然后服务器端会将消息原样发送回客户端。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)