Csharp/C Sharp/Network/IPEndPoint — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:47, 26 мая 2010
IPAddress: AddressFamily,MinPort,MaxPort,Serialize
using System;
using System.Net;
class IPEndPointSample {
public static void Main() {
IPAddress test1 = IPAddress.Parse("192.168.1.1");
IPEndPoint ie = new IPEndPoint(test1, 8000);
Console.WriteLine("The IPEndPoint is: {0}",ie.ToString());
Console.WriteLine("The AddressFamily is: {0}",ie.AddressFamily);
Console.WriteLine("The address is: {0}, and the port is: {1}\n", ie.Address, ie.Port);
Console.WriteLine("The min port number is: {0}",IPEndPoint.MinPort);
Console.WriteLine("The max port number is: {0}\n",IPEndPoint.MaxPort);
ie.Port = 80;
Console.WriteLine("The changed IPEndPoint value is: {0}", ie.ToString());
SocketAddress sa = ie.Serialize();
Console.WriteLine("The SocketAddress is: {0}",sa.ToString());
}
}
new IPEndPoint(IPAddress.Parse("0.1"), 8888)
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
class MainClass {
private static void Main() {
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
Thread receiveThread = new Thread(ReceiveData);
receiveThread.IsBackground = true;
receiveThread.Start();
UdpClient client = new UdpClient();
try {
string text = "message";
byte[] data = Encoding.UTF8.GetBytes(text);
client.Send(data, data.Length, remoteEndPoint);
} catch (Exception err) {
Console.WriteLine(err.ToString());
} finally {
client.Close();
}
}
private static void ReceiveData() {
UdpClient client = new UdpClient(5555);
while (true) {
try {
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
string text = Encoding.UTF8.GetString(data);
Console.WriteLine(">> " + text);
} catch (Exception err) {
Console.WriteLine(err.ToString());
}
}
}
}