zoukankan      html  css  js  c++  java
  • C# socket

    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
    Host: " + server + "
    Connection:Close
    
    ";
                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 + ":
    ";
                    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#开源框架
    8 种 NoSQL 数据库系统对比
    安装补丁“此更新不适用于你的计算机”解决办法
    .net开源资料
    winform程序退出
    jquery.chained与jquery.chained.remote使用以及区别
    存储过程使用回滚
    C# Panel中绘图如何出现滚动条
    C#结构体的特点浅析
    如何用堆栈和循环结构代替递归调用--递归转换为非递归的10条军规
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11927138.html
Copyright © 2011-2022 走看看