本文整理匯總了C#中System.Net.Sockets.Socket.Bind方法的典型用法代碼示例。如果您正苦於以下問題:C# Socket.Bind方法的具體用法?C# Socket.Bind怎麽用?C# Socket.Bind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.Bind方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1:
try {
aSocket.Bind(anEndPoint);
}
catch (Exception e) {
Console.WriteLine("Winsock error: " + e.ToString());
}
示例2: Socket.Bind(IPEndPoint ip)
//引入命名空間
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MainClass
{
public static void Main()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999);
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address, clientep.Port);
string welcome = "Welcome";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,SocketFlags.None);
Console.WriteLine("Disconnected from {0}",clientep.Address);
client.Close();
socket.Close();
}
}