Socket编程 (连接,发送消息) (Tcp、Udp) - Part1

Socket编程 (连接,发送消息) (Tcp、Udp)


本篇文章主要实现Socket在Tcp\Udp协议下相互通讯的方式。(服务器端与客户端的通讯)

1.基于Tcp协议的Socket通讯类似于B/S架构,面向连接,但不同的是服务器端可以向客户端主动推送消息。

使用Tcp协议通讯需要具备以下几个条件:

(1).建立一个套接字(Socket)

(2).绑定服务器端IP地址及端口号--服务器端

(3).利用Listen()方法开启监听--服务器端

(4).利用Accept()方法尝试与客户端建立一个连接--服务器端

(5).利用Connect()方法与服务器建立连接--客户端

(5).利用Send()方法向建立连接的主机发送消息

(6).利用Recive()方法接受来自建立连接的主机的消息(可靠连接)

2.基于Udp协议是无连接模式通讯,占用资源少,响应速度快,延时低。至于可靠性,可通过应用层的控制来满足。(不可靠连接)

(1).建立一个套接字(Socket)

(2).绑定服务器端IP地址及端口号--服务器端

(3).通过SendTo()方法向指定主机发送消息(需提供主机IP地址及端口)

(4).通过ReciveFrom()方法接收指定主机发送的消息(需提供主机IP地址及端口)

1、服务端

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;
#region 命名空间
using System.Net;
using System.Net.Sockets;
using System.Threading;
#endregion

namespace SocketServerConsole
{
    class Program
    {
        #region 控制台主函数
        /// <summary>
        /// 控制台主函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //主机IP
            IPEndPoint serverIP = new IPEndPoint(IPAddress.Parse("192.168.1.105"), 8686);
            Console.WriteLine("请选择连接方式:");
            Console.WriteLine("A.Tcp");
            Console.WriteLine("B.Udp");
            ConsoleKey key;
            while (true)
            {
                key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.A) TcpServer(serverIP);
                else if (key == ConsoleKey.B) UdpServer(serverIP);
                else
                {
                    Console.WriteLine("输入有误,请重新输入:");
                    continue;
                }
                break;
            }
        }
        #endregion

        #region Tcp连接方式
        /// <summary>
        /// Tcp连接方式
        /// </summary>
        /// <param name="serverIP"></param>
        public static void TcpServer(IPEndPoint serverIP)
        {
            Console.WriteLine("客户端Tcp连接模式");
            Socket tcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            tcpServer.Bind(serverIP);
            tcpServer.Listen(100);
            Console.WriteLine("开启监听...");
            new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        TcpRecive(tcpServer.Accept());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
                        break;
                    }
                }
            }).Start();
            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
            tcpServer.Close();
        }

        public static void TcpRecive(Socket tcpClient)
        {
            new Thread(() =>
            {
                while (true)
                {
                    byte[] data = new byte[1024];
                    try
                    {
                        int length = tcpClient.Receive(data);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
                        break;
                    }
                    Console.WriteLine(string.Format("收到消息:{0}", Encoding.UTF8.GetString(data)));
                    string sendMsg = "收到消息!";
                    tcpClient.Send(Encoding.UTF8.GetBytes(sendMsg));
                }
            }).Start();
        }
        #endregion

        #region Udp连接方式
        /// <summary>
        /// Udp连接方式
        /// </summary>
        /// <param name="serverIP"></param>
        public static void UdpServer(IPEndPoint serverIP)
        {
            Console.WriteLine("客户端Udp模式");
            Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpServer.Bind(serverIP);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)ipep;
            new Thread(() =>
            {
                while (true)
                {
                    byte[] data = new byte[1024];
                    try
                    {
                        int length = udpServer.ReceiveFrom(data, ref Remote);//接受来自服务器的数据
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
                        break;
                    }
                    Console.WriteLine(string.Format("{0} 收到消息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Encoding.UTF8.GetString(data)));
                    string sendMsg = "收到消息!";
                    udpServer.SendTo(Encoding.UTF8.GetBytes(sendMsg), SocketFlags.None, Remote);
                }
            }).Start();
            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
            udpServer.Close();
        }
        #endregion
    }
}

2、客户端

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;
#region 命名空间
using System.Net.Sockets;
using System.Net;
using System.Threading;
#endregion

namespace SocketClientConsole
{
    class Program
    {
        #region 控制台主函数
        /// <summary>
        /// 控制台主函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //主机IP
            IPEndPoint serverIP = new IPEndPoint(IPAddress.Parse("192.168.1.77"), 8686);
            Console.WriteLine("请选择连接方式:");
            Console.WriteLine("A.Tcp");
            Console.WriteLine("B.Udp");
            ConsoleKey key;
            while (true)
            {
                key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.A) TcpServer(serverIP);
                else if (key == ConsoleKey.B) UdpClient(serverIP);
                else
                {
                    Console.WriteLine("输入有误,请重新输入:");
                    continue;
                }
                break;
            }
        }
        #endregion

        #region Tcp连接方式
        /// <summary>
        /// Tcp连接方式
        /// </summary>
        /// <param name="serverIP"></param>
        public static void TcpServer(IPEndPoint serverIP)
        {
            Console.WriteLine("客户端Tcp连接模式");
            Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                tcpClient.Connect(serverIP);
            }
            catch (SocketException e)
            {
                Console.WriteLine(string.Format("连接出错:{0}", e.Message));
                Console.WriteLine("点击任何键退出!");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("客户端:client-->server");
            string message = "我上线了...";
            tcpClient.Send(Encoding.UTF8.GetBytes(message));
            Console.WriteLine(string.Format("发送消息:{0}", message));
            new Thread(() =>
            {
                while (true)
                {
                    byte[] data = new byte[1024];
                    try
                    {
                        int length = tcpClient.Receive(data);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
                        break;
                    }
                    Console.WriteLine(string.Format("{0} 收到消息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Encoding.UTF8.GetString(data)));
                }
            }).Start();
            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
            tcpClient.Close();
        }
        #endregion

        #region Udp连接方式
        /// <summary>
        /// Udp连接方式
        /// </summary>
        /// <param name="serverIP"></param>
        public static void UdpClient(IPEndPoint serverIP)
        {
            Console.WriteLine("客户端Udp模式");
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            string message = "我上线了...";
            udpClient.SendTo(Encoding.UTF8.GetBytes(message), SocketFlags.None, serverIP);
            Console.WriteLine(string.Format("发送消息:{0}", message));
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)sender;
            new Thread(() =>
            {
                while (true)
                {
                    byte[] data = new byte[1024];
                    try
                    {
                        int length = udpClient.ReceiveFrom(data, ref Remote);//接受来自服务器的数据
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
                        break;
                    }
                    Console.WriteLine(string.Format("{0} 收到消息:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Encoding.UTF8.GetString(data)));
                }
            }).Start();
            Console.WriteLine("\n\n输入\"Q\"键退出。");
            ConsoleKey key;
            do
            {
                key = Console.ReadKey(true).Key;
            } while (key != ConsoleKey.Q);
            udpClient.Close();
        }
        #endregion
    }
}

Tcp协议下通讯效果如下图:

客户端:

服务器端:

基于Udp协议下通讯效果如下图:

客户端:

服务器端:

总结

Tcp协议相对通讯来说相对可靠,信息不易丢失,Tcp协议发送消息,发送失败时会重复发送消息等原因。所以对于要求通讯安全较高的程序来说,选择Tcp协议的通讯相对合适。Upd协议通讯个人是比较推荐的,占用资源小,低延时,响应速度快。至于可靠性是可以通过一些应用层加以封装控制得到相应的满足。

相关推荐
FPGA_Linuxer30 分钟前
xilinx hbm ip运用
网络·网络协议·tcp/ip
不悔哥2 小时前
openwrt wsdd模块介绍
linux·c语言·网络·tcp/ip·智能路由器
jyan_敬言2 小时前
虚拟机centos_7 配置教程(镜像源、配置centos、静态ip地址、Finalshell远程操控使用)
linux·运维·服务器·c语言·数据结构·tcp/ip·centos
洁洁!2 小时前
【计算机网络】数据链路层深度解析
网络·网络协议·计算机网络
Aomnitrix4 小时前
网络协议全景:Linux环境下的TCP/IP、UDP
linux·运维·网络·c++·网络协议·tcp/ip·运维开发
snow@li12 小时前
AI问答-HTTP:理解 Content-Disposition
网络·网络协议·http
OldGj_13 小时前
一次RPC调用过程是怎么样的?
网络·网络协议·rpc
宗介波妞16 小时前
BuripSuiteProfessional 抓取HTTPS配置
网络协议·http·https
Hello.Reader21 小时前
使用 Nmap 进行 SSL/TLS 加密套件枚举
网络·网络协议·ssl
快快小毛毛1 天前
CC攻击防御策略要怎么调整?使用游戏盾有效解决
运维·服务器·网络·tcp/ip·游戏·udp