C#控制台之有手就行之使用Socket创建两个控制台应用程一个做服务器Service一个做客户端Client,客户端发送服务器当前时间服务器接收

具体就是探究 服务器和客户端的通信形式

服务器代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace ServerConsole
{
    class Program
    {
        static void Main(string[] args)
        {


            Console.WriteLine("Hello World");

            //Socket


            Socket listenfd = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            //Bind 

            IPAddress ipAr = IPAddress.Parse("127.0.0.1");

            IPEndPoint ipEp = new IPEndPoint(ipAr,1234);


            listenfd.Bind(ipEp);


            //Listen

            listenfd.Listen(0);

            Console.WriteLine("[服务器]启动成功");

            while (true)
            {
                //卡 接收链接
                Socket connfd = listenfd.Accept();

                Console.WriteLine("[服务器]Accept");


               //Recv
                byte[] readBuff = new byte[1024];

                //卡 接收数据
                int count = connfd.Receive(readBuff);

                string str = System.Text.Encoding.UTF8.GetString(readBuff,0,count);

                Console.WriteLine("[服务器接收]"+str);


                //Send 发送客户端 数据

                byte[] bytes = System.Text.Encoding.Default.GetBytes("Serv echo" +str);

                connfd.Send(bytes);




            }


           

           

           

        }
    }
}

理清思路

创建一个 socket

            Socket listenfd = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

-----分割线-------------
创建本地ip地址和端口
bind方法绑定

    IPAddress ipAr = IPAddress.Parse("127.0.0.1");

            IPEndPoint ipEp = new IPEndPoint(ipAr,1234);


            listenfd.Bind(ipEp);

监听功能 不会 先放着


            listenfd.Listen(0);

为新建链接创建socket

Socket connfd = listenfd.Accept();

接收数据并且解析(重要)

	byte[] readBuff = new byte[1024];
			int count = connfd.Receive(readBuff);
			string str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);
			Console.WriteLine("[服务器接收]" + str);

Send 发送客户端 数据 这里服务器已经接受了数据了 只是对客户端格外发送请求

    //Send 发送客户端 数据

                byte[] bytes = System.Text.Encoding.Default.GetBytes("Serv echo" +str);

                connfd.Send(bytes);

客户端代码

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

namespace ClientConsole
{
    class Program
    {

      

        


        public static void Connetion()
        {
            const int BUFFER_SIZE = 1024;
            byte[] readBuff = new byte[BUFFER_SIZE];
            Socket socket;
            //Socket
            socket = new Socket(AddressFamily.InterNetwork,
                         SocketType.Stream, ProtocolType.Tcp);
            //Connect
            string host = "127.0.0.1";

            int port = 1234;

            socket.Connect(host, port);

            string  clientText = "客户端地址 " + socket.LocalEndPoint.ToString();

            //Send

            string str = System.DateTime.Now.ToString();
            byte[] bytes = System.Text.Encoding.Default.GetBytes(str);

            //
      
            socket.Send(bytes);

            int count = socket.Receive(readBuff);
            str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);


            Console.WriteLine(clientText+str);

            Console.ReadLine();

        }




        static void Main(string[] args)

        {

            Connetion();





        }
    }
}

定义好socket后进行连接

    socket.Connect(host, port);

发送

  string str = System.DateTime.Now.ToString();
            byte[] bytes = System.Text.Encoding.Default.GetBytes(str);

//这里是获取服务器端返回的信息 一样的Receive方法 和 GetString解析

    int count = socket.Receive(readBuff);
            str = System.Text.Encoding.UTF8.GetString(readBuff, 0, count);


            Console.WriteLine(clientText+str);

            Console.ReadLine();

在这里插入图片描述在这里插入图片描述完毕

发布了251 篇原创文章 · 获赞 42 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_38992403/article/details/105048002