-
C#教程之C# socket
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.IO; using System.Net; namespace ConsoleApp396 { class Program { private static Socket ConnectSocket(string server,int port) { Socket s = null; IPHostEntry hostEntry = null; //Get host related information hostEntry = Dns.GetHostEntry(server); foreach(IPAddress address in hostEntry.AddressList) { IPEndPoint ipe = new IPEndPoint(address, port); Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(ipe); if (tempSocket.Connected) { s = tempSocket; break; } else { continue; } } return s; } private static string SocketSendReceive(string server,int port) { string request = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection:Close\r\n\r\n"; byte[] bytesSent = Encoding.ASCII.GetBytes(request); byte[] bytesReceived = new byte[256]; string page = ""; using(Socket s = ConnectSocket(server, port)) { if(s==null) { return ("Connection failed!"); } s.Send(bytesSent, bytesSent.Length, 0); int bytes = 0; page = "Default HTML page on " + server + ":\r\n"; do { bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes); } while (bytes > 0); } return page; } public static void Main(string[] args) { string host; int port = 80; if(args.Length==0) { host = Dns.GetHostName(); } else { host = args[0]; } string result = SocketSendReceive(host, port); Console.WriteLine(result); Console.ReadLine(); } } }
栏目列表
最新更新
C#如何判断委托是实例方法还是静态方法
C# socket
C#中获取指定目录下所有目录的名称、全
Python中编写类的各种技巧和方法
解决pip使用异常No module named 'pip'
python(生成器)
服务器重启后启动Docker命令
单表序列化 和 单表反序列化
零基础小白入门IT开发指南
用Python复制文件的9个方法
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能
将Session值储存于SQL Server中
使用SQL Server数据库嵌套子查询的方法
SQL概述