C# 网络编程 实现TCP三次握手(代码+解析)(也能接收到信息)

C# 网络编程 实现TCP三次握手(也能接收到信息)

解析都在代码里了

实现:

服务器可接收客户端发来的信息和发送信息
客户端也可以发送信息 和 接收服务器的信息

实现的结果在这里:

实现结果

服务器前端代码

MainWindow.xaml:

<Window x:Class="ServerTcpClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Content="服务器" Height="25" HorizontalAlignment="Left" Margin="198,12,0,0" Name="label1" VerticalAlignment="Top" Width="108" />
        <ScrollViewer Height="199" HorizontalAlignment="Left" Margin="32,45,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="443">
            <TextBlock Height="auto" Name="textBlock1" Text="" Width="auto" TextWrapping="Wrap"/>
        </ScrollViewer>
        <Button Content="开始监听" Height="33" HorizontalAlignment="Left" Margin="68,262,0,0" Name="button1" VerticalAlignment="Top" Width="113" Click="button1_Click" />
        <Button Content="停止监听" Height="33" HorizontalAlignment="Right" Margin="0,262,113,0" Name="button2" VerticalAlignment="Top" Width="113" Click="button2_Click" />
    </Grid>
</Window>

标题后端代码:

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace ServerTcpClient
{
   
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
   
        IPAddress ip;
        
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您解答关于C#网络编程中Socket Tcp的异步循环接收客户端连接和信息的问题。 在Socket Tcp编程中,异步循环接收客户端连接和信息是一种常见的编程模式,可以有效地提高程序的并发性能和可靠性。下面是一个简单的实现方式: 首先,我们需要创建一个TcpListener对象,并绑定监听的IP地址和端口号: ```csharp TcpListener listener = new TcpListener(IPAddress.Any, 8888); listener.Start(); ``` 然后,我们可以使用异步方式接收客户端的连接请求: ```csharp listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener); ``` 其中,AcceptCallback是一个回调函数,用于处理客户端连接的请求,可以在该函数中创建一个新的TcpClient对象,并使用异步方式接收客户端发送的数据。 在AcceptCallback函数中,我们可以使用异步方式接收客户端发送的数据: ```csharp TcpClient client = listener.EndAcceptTcpClient(ar); NetworkStream stream = client.GetStream(); byte[] buffer = new byte[4096]; stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), buffer); ``` 其中,ReadCallback是一个回调函数,用于处理客户端发送的数据,可以在该函数中解析并处理客户端发送的数据。 最后,在ReadCallback函数中,我们可以使用异步方式持续接收客户端发送的数据: ```csharp stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(ReadCallback), buffer); ``` 以上就是使用异步循环接收客户端连接和信息的基本实现方式。需要注意的是,在实现过程中,我们需要考虑多线程安全和异常处理等问题,以确保程序的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值