可能重复:C#如何制作一个简单的UDP服务器
我想用 C# 制作一个 UDP 服务器.我怎么做?如何自定义监听哪个端口(即 1212)?
I want to make a UDP server in C#. How do I do that? How can I customize which port it listens on (namely 1212)?
这里有一个 C#中的示例:
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpSrvrSample
{
public static void Main()
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
UdpClient newsock = new UdpClient(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = newsock.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
newsock.Send(data, data.Length, sender);
while(true)
{
data = newsock.Receive(ref sender);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
newsock.Send(data, data.Length, sender);
}
}
}
上一篇:带epoll的多线程UDP服务器?多线程、服务器、epoll、UDP
下一篇:在GLSurfaceView播放SurfaceView的视频,而不是而不是、视频、GLSurfaceView、SurfaceView