• 大小: 79KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签: socket  tcp  udp  c#  

资源简介

例子中演示了udp和tcp通讯,可以循环发送消息。 分为服务器端和客户端两个控制台。 udp比较简单,无连接状态,可以指定ip也可以广播; tcp比较复杂,不能广播,每次发消息都需要重新建立连接 手动修改一下Main方法中调用的方法udp()/tcp()即可完成两种方式的切换。

资源截图

代码片段和文件信息

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

namespace SocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
           udp();//可以手动切换tcp/udp代码
            
        }
        /// 
        /// udp不需要建立面向连接的通信(通过广播发送数据)
        /// 

        private static void udp()
        {
            Socket udp = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
            udp.SetSocketOption(SocketOptionLevel.Socket SocketOptionName.Broadcast 1);//允许 发送广播
            IPEndPoint iepA = new IPEndPoint(IPAddress.Broadcast 9095);
            //IPEndPoint iepA = new IPEndPoint(IPAddress.Parse(“192.168.2.160“) 5566);
            Console.WriteLine(“udp已经建立,请输入您要发送的内容“);
            while (true)
            {
                string sss = Console.ReadLine();
                int len = udp.SendTo(System.Text.Encoding.Unicode.GetBytes(sss) iepA);

            }
        }
     
        /// 
        /// tcp需要建立面向连接的通讯,并且需要三次握手.也就是说,每次通信后,tcp都需要关闭,下次关闭前重新建立连接。(不能通过广播发送数据)
        /// 

        private static void tcp()
        {
            Socket newclient;
            while (true)
            {

                Console.WriteLine(“请输入您要发送的内容“);
                string content = Console.ReadLine();
                using (newclient = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork System.Net.Sockets.SocketType.Stream System.Net.Sockets.ProtocolType.Tcp))
                {
                    byte[] data = new byte[1024];
                    IPEndPoint ie = new System.Net.IPEndPoint(IPAddress.Parse(“192.168.1.10“) 9050);//服务器的IP和端口

                    try
                    {
                        //因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
                        newclient.Connect(ie);//第一次交互
                        int recv = newclient.Receive(data);//第二次交互
                        //连接服务器成功
                        string stringdata = System.Text.Encoding.Unicode.GetString(data 0 recv);
                        if (stringdata == “连接服务器成功“)
                        {
                            newclient.Send(System.Text.Encoding.Unicode.GetBytes(content));//第三次交互
                            newclient.Shutdown(System.Net.Sockets.SocketShutdown.Send);
                            data = new byte[1024];
                            recv = newclient.Receive(data);
                            string result = System.Text.Encoding.Unicode.GetString(data 0 recv);
                            newclient.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
                            newclient.Close();
                            Console.WriteLine(result);

                        }
                        else
                        {
                            C

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-04-20 22:37  SocketClient\
     目录           0  2017-07-18 14:24  SocketClient\SocketClient\
     文件         187  2017-03-29 17:10  SocketClient\SocketClient\App.config
     文件        4402  2017-07-18 14:24  SocketClient\SocketClient\Program.cs
     目录           0  2017-04-20 22:37  SocketClient\SocketClient\Properties\
     文件        1348  2017-03-29 17:10  SocketClient\SocketClient\Properties\AssemblyInfo.cs
     文件        2566  2017-03-29 17:10  SocketClient\SocketClient\SocketClient.csproj
     目录           0  2017-04-20 22:37  SocketClient\SocketClient\bin\
     目录           0  2017-04-20 22:37  SocketClient\SocketClient\bin\Debug\
     文件        6656  2017-07-18 14:24  SocketClient\SocketClient\bin\Debug\SocketClient.exe
     文件         187  2017-03-29 17:10  SocketClient\SocketClient\bin\Debug\SocketClient.exe.config
     文件       15872  2017-07-18 14:24  SocketClient\SocketClient\bin\Debug\SocketClient.pdb
     文件       24224  2017-07-18 14:25  SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe
     文件         187  2017-03-29 17:10  SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe.config
     文件         490  2013-03-18 17:00  SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe.manifest
     目录           0  2017-04-20 22:37  SocketClient\SocketClient\obj\
     目录           0  2017-07-18 14:24  SocketClient\SocketClient\obj\Debug\
     文件        6613  2017-07-14 15:10  SocketClient\SocketClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         556  2017-07-18 14:25  SocketClient\SocketClient\obj\Debug\SocketClient.csproj.FileListAbsolute.txt
     文件        1755  2017-07-14 15:11  SocketClient\SocketClient\obj\Debug\SocketClient.csprojResolveAssemblyReference.cache
     文件        6656  2017-07-18 14:24  SocketClient\SocketClient\obj\Debug\SocketClient.exe
     文件       15872  2017-07-18 14:24  SocketClient\SocketClient\obj\Debug\SocketClient.pdb
     目录           0  2017-07-18 14:33  SocketClient\SocketClient\obj\Debug\TempPE\
     文件           0  2017-03-29 17:10  SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2017-03-29 17:10  SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2017-03-29 17:10  SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     文件        1005  2017-03-29 17:10  SocketClient\SocketClient.sln
     文件       30208  2017-07-18 14:25  SocketClient\SocketClient.v12.suo
     目录           0  2017-04-20 22:37  SocketServer\
     目录           0  2017-07-18 14:24  SocketServer\SocketServer\
     文件         187  2017-03-29 16:50  SocketServer\SocketServer\App.config
............此处省略25个文件信息

评论

共有 条评论