本文整理汇总了C#中System.Net.Sockets.Socket类的典型用法代码示例。如果您正苦于以下问题:C# Socket类的具体用法?C# Socket怎么用?C# Socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Socket类属于System.Net.Sockets命名空间,在下文中一共展示了Socket类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: ConnectSocket
//引入命名空间
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
// an exception that occurs when the host IP Address is not compatible with the address family
// (typical in the IPv6 case).
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;
}
// This method requests the home page content for the specified server.
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 = "";
// Create a socket connection with the specified server and port.
using(Socket s = ConnectSocket(server, port)) {
if (s == null)
return ("Connection failed");
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
page = "Default HTML page on " + server + ":\r\n";
// The following will block until the page is transmitted.
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)
// If no server name is passed as argument to this program,
// use the current host name as the default.
host = Dns.GetHostName();
else
host = args[0];
string result = SocketSendReceive(host, port);
Console.WriteLine(result);
}
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:90,代码来源:Socket
示例2: new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp )
//引入命名空间
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
const int echoPort = 7;
[STAThread]
static void Main( string[] args )
{
Socket s = new Socket( AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp );
s.Connect( new IPEndPoint( IPAddress.Loopback, echoPort ) );
UTF8Encoding enc = new UTF8Encoding();
s.Send( enc.GetBytes( "test message" ) );
Byte[] buff = new Byte[ 1024 ];
s.Receive( buff );
System.Console.WriteLine( enc.GetString( buff ) );
}
}
开发者ID:C#程序员,项目名称:System.Net.Sockets,代码行数:25,代码来源:Socket
注:本文中的System.Net.Sockets.Socket类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论