本文整理汇总了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();
}
}