C# socket

网友投稿 721 2022-09-11

C# socket

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\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();

}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ARCOCAD 五轴测量PH20
下一篇:PowerShell - 基础参考
相关文章

 发表评论

暂时没有评论,来抢沙发吧~