Socket-tcp协议客户端与服务器端互联

本文详细介绍了一个基于TCP协议的客户端与服务器之间的通信过程。包括客户端如何连接服务器、收发消息,以及服务器如何接受连接、处理并响应客户端请求的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 客户端
/// </summary>
namespace tcp协议客户端
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Socket
            Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //发起建立连接的请求
            IPAddress iPAddress = IPAddress.Parse("192.168.3.35");//可以字符串转换成ipaddress的对象
            EndPoint point = new IPEndPoint(iPAddress,7788);
            tcpClient.Connect(point);//通过ip和端口号定位一个服务器端

            //连接成功 接收消息
            byte[] data = new byte[1024];
            int length = tcpClient.Receive(data);//length表示接收了多少数据
            string message = Encoding.UTF8.GetString(data, 0, length);//只把接收到的数据转化
            Console.WriteLine(message);

            //--------向服务器端发送消息------------
            string message2 = Console.ReadLine();
            tcpClient.Send(Encoding.UTF8.GetBytes(message2));//把字符串转换为字节数组发送到服务器端
             

            Console.ReadKey();

        }
    }
}

服务器端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;

/// <summary>
/// 服务器端
/// </summary>
namespace tcp协议
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建Socket
            Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //绑定ip和端口号 192.168.3.35
            IPAddress iPAddress = new IPAddress(new byte[] { 192,168,3,35 });//ipaddress是对ip和端口进行封装的类
            EndPoint point = new IPEndPoint(iPAddress,7788);// 向系统申请一个可用的ip端口号
            tcpServer.Bind(point);
            //开始监听(等待客户端链接)
            tcpServer.Listen(100);//最大连接数
            Console.WriteLine("开始监听");

            //接收 返回一个clientSocket
            Socket clientScocket= tcpServer.Accept();//暂停当前线程,直到一个客户端连接过来之后进行下面的操作
            Console.WriteLine("一个客户端连接过来");
            
            //向客户端发送一个返回消息
            string message = "hallo 你好";
            byte[] data= Encoding.UTF8.GetBytes(message);//对字符串做编码,等到一个字节数组
            clientScocket.Send(data);
            Console.WriteLine("向客户端发送了一条数据");

            //-----------------接收服务器端消息-------------------
            byte[] data2 = new byte[1024];//传建一个字节数组接收客户端发来的消息
            int length = clientScocket.Receive(data2);
            string message2 = Encoding.UTF8.GetString(data2, 0, length);//把字节数组转换为字符串
            Console.WriteLine("接收了一条消息" + message2);

            Console.ReadKey();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值