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

本文介绍了使用C#进行TCP网络编程,详细解析了如何实现TCP三次握手过程,并展示了服务器和客户端的代码实现,使得双方能够进行信息的收发。
摘要由CSDN通过智能技术生成

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;
        int ServicePort = 
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值