• 大小: 0.24M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-04-29
  • 语言: C#
  • 标签: Socket  

资源简介

一个服务端监听程序源码,一个客户端发消息程序源码,入门级

资源截图

代码片段和文件信息

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 6000;
            string host = “127.0.0.1“;

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip port);

            Socket sSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            sSocket.Bind(ipe);
            sSocket.Listen(0);
            Console.WriteLine(“监听已经打开,请等待“);

            //receive message
            Socket serverSocket = sSocket.Accept();
            Console.WriteLine(“连接已经建立“);
            string recStr = ““;
            byte[] recByte = new byte[4096];
            int bytes = serverSocket.Receive(recByte recByte.Length 0);
            recStr += Encoding.ASCII.GetString(recByte 0 bytes);

            //send message
            Console.WriteLine(“服务器端获得信息:{0}“ recStr);
            string sendStr = “send to client :hello“;
            byte[] sendByte = Encoding.ASCII.GetBytes(sendStr);
            serverSocket.Send(sendByte sendByte.Length 0);
            serverSocket.Close();
            sSocket.Close();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     31744  2020-09-03 16:40  socket\ConsoleApp2\.vs\ConsoleApp2\v15\.suo

     文件       4096  2020-05-04 13:16  socket\ConsoleApp2\.vs\ConsoleApp2\v15\Server\sqlite3\storage.ide

     文件      32768  2020-09-03 16:38  socket\ConsoleApp2\.vs\ConsoleApp2\v15\Server\sqlite3\storage.ide-shm

     文件     622152  2020-05-04 13:17  socket\ConsoleApp2\.vs\ConsoleApp2\v15\Server\sqlite3\storage.ide-wal

     文件        189  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\App.config

     文件       5632  2020-09-03 16:38  socket\ConsoleApp2\ConsoleApp2\bin\Debug\ConsoleApp2.exe

     文件        189  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\bin\Debug\ConsoleApp2.exe.config

     文件      15872  2020-09-03 16:38  socket\ConsoleApp2\ConsoleApp2\bin\Debug\ConsoleApp2.pdb

     文件       2372  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\ConsoleApp2.csproj

     文件         42  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\ConsoleApp2.csproj.CoreCompileInputs.cache

     文件        669  2020-05-04 13:17  socket\ConsoleApp2\ConsoleApp2\obj\Debug\ConsoleApp2.csproj.FileListAbsolute.txt

     文件       8077  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\ConsoleApp2.csprojAssemblyReference.cache

     文件       5632  2020-09-03 16:38  socket\ConsoleApp2\ConsoleApp2\obj\Debug\ConsoleApp2.exe

     文件      15872  2020-09-03 16:38  socket\ConsoleApp2\ConsoleApp2\obj\Debug\ConsoleApp2.pdb

     文件       6806  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       1426  2020-05-04 13:17  socket\ConsoleApp2\ConsoleApp2\Program.cs

     文件       1320  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2\Properties\AssemblyInfo.cs

     文件       1132  2020-05-04 13:16  socket\ConsoleApp2\ConsoleApp2.sln

    ..A..H.     36352  2020-09-03 16:40  socket\ConsoleApp3\.vs\ConsoleApp3\v15\.suo

     文件       4096  2020-05-04 13:19  socket\ConsoleApp3\.vs\ConsoleApp3\v15\Server\sqlite3\storage.ide

     文件      32768  2020-09-03 16:39  socket\ConsoleApp3\.vs\ConsoleApp3\v15\Server\sqlite3\storage.ide-shm

     文件     618032  2020-05-04 13:19  socket\ConsoleApp3\.vs\ConsoleApp3\v15\Server\sqlite3\storage.ide-wal

     文件        189  2020-05-04 13:19  socket\ConsoleApp3\ConsoleApp3\App.config

     文件       5120  2020-09-03 16:39  socket\ConsoleApp3\ConsoleApp3\bin\Debug\ConsoleApp3.exe

     文件        189  2020-05-04 13:19  socket\ConsoleApp3\ConsoleApp3\bin\Debug\ConsoleApp3.exe.config

     文件      15872  2020-09-03 16:39  socket\ConsoleApp3\ConsoleApp3\bin\Debug\ConsoleApp3.pdb

     文件       2372  2020-05-04 13:19  socket\ConsoleApp3\ConsoleApp3\ConsoleApp3.csproj

............此处省略42个文件信息

评论

共有 条评论