博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SYSTEM.NET.SOCKET命名空间之SOCKET类。。。
阅读量:7260 次
发布时间:2019-06-29

本文共 2488 字,大约阅读时间需要 8 分钟。

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 using System.Net.Sockets;11 using System.Net;12 13 namespace WindowsFormsApplication514 {15     public partial class Form1 : Form16     {17         public Form1()18         {19             InitializeComponent();20         }21         private static Socket ConnectSocket(string server, int port)22         {23             Socket socket = null;24             IPHostEntry iphostentry = null;25             iphostentry = Dns.GetHostEntry(server);26             foreach(IPAddress address in iphostentry.AddressList)27             {28                 IPEndPoint IPEPoint = new IPEndPoint(address,port);29                 Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);30                 newSocket.Connect(IPEPoint);31                 if(newSocket.Connected)32                 {33                     socket = newSocket;34                     break;35                 }36                 else37                 {38                     continue;39                 }40             }41             return socket;42         }43 44         private static string SocketSendReveive(string server, int port)45         {46             string request = "GET/HTTP/1.1\n Host: " + server + "\n connetc: close\n";47             Byte[] btSend = Encoding.ASCII.GetBytes(request);48             Byte[] btReceived = new Byte[256];49             Socket socket = ConnectSocket(server,port);50             if(socket == null)51                 return ("connect failed.");52             socket.Send(btSend,btSend.Length,0);53             int intContent = 0;54             string strContent = server + " 's content :\n";55             do56             {57                 intContent = socket.Receive(btReceived, btReceived.Length, 0);58                 strContent += Encoding.ASCII.GetString(btReceived,0,intContent);59             }60             while(intContent > 0);61             return strContent;62         }63 64 65 66         private void button1_Click(object sender, EventArgs e)67         {68             string server = textBox1.Text;69             int port = Convert.ToInt32(textBox2.Text);70             string strContent = SocketSendReveive(server, port);71             MessageBox.Show(strContent);72 73         }74     }75 }

转载地址:http://bqodm.baihongyu.com/

你可能感兴趣的文章
利用Android NDK编译lapack
查看>>
TOMCAT 集群之 PERSISTENT SESSION
查看>>
URL加载系统----iOS工程师必须熟练掌握
查看>>
PHPCMS标签
查看>>
迁移数据库后出现异常情况,存储过程不能用
查看>>
Android开发在路上:少去踩坑,多走捷径
查看>>
asp.net mvc控制器动作体返回ImageResult,可作验证码
查看>>
微软BI 之SSIS 系列 - 在 SQL 和 SSIS 中实现行转列的 PIVOT 透视操作
查看>>
ylbtech-LanguageSamples-COMInteropPart1(COM 互操作 - 第一部分)
查看>>
RabbitMQ学习之:(七)Fanout Exchange (转贴+我的评论)
查看>>
JS函数匿名替换
查看>>
(转)x264代码详细阅读之x264.c,common.c,encoder.c
查看>>
IOS 面试 --- 网络部分
查看>>
IOS小工具以及精彩的博客
查看>>
Unity3D热更新全书-重头再来
查看>>
Android WebView的注意事项
查看>>
【HDU】3853 LOOPS
查看>>
lua(简单的传参)
查看>>
文章收藏
查看>>
按时间范围查询条件
查看>>