C#UDP套接字客户端和服务器 [英] C# UDP Socket client and server
问题描述
我在这里的第一个问题。我是新来这种编程,而我只编程.NET网站和形式。
My first question here. I am new to this kind of programming, and i've only programmed .NET web sites and forms.
现在,我公司在工作中,要我做的ActiveX组件,侦听UDP消息,并将其转换为事件。
Now, the company I work at, asks me to make an ActiveX component, that listens to UDP messages, and turns them into events.
的UDP封邮件是从Avaya系统发送,所以我被告知,来测试我的ActiveX,在首先我需要创建一个应用程序,只发送UDP(只有一个按钮,发送预先定义的UDP字符串)。然后创建监听套接字,普通的C#应用程序,这将得到测试应用程序的UDP传输的字符串。这两个应用程序将工作在同一台机器上。
The UDP msgs are send from Avaya system, so i was told that to test my ActiveX, at first I need to create an app, that only sends UDP (only one button that sends pre-defined UDP string). And then create listener socket, ordinary C# app, that will get those transmitted UDP string from the tests app. Both apps will work on the same machine.
后来,当我得到这个工作,我需要使听者一个ActiveX组件,但首先第一件事情。
Later, when i get this working, i need to make the listener an ActiveX component, but first things first.
我需要知道,如果有这方面有任何好的教程,以及如何启动任何想法?我为我的无知抱歉,但我真的很新的这一点,我真的没有任何时间去学这个,因为它有2个星期内完成。
I need to know if there are any good tutorials about this, and any idea on how to start? I am sorry for my ignorance, but i am really new on this and i don't really have any time to learn this since it has to be done in 2 weeks.
在此先感谢
编辑:我设法创建2个简单的控制台应用程序,并成功地在它们之间发送UDP消息。发送者将仅用于测试,现在我需要重新做我的接收器,以获得UDP消息,并翻译它的事件。最后,使其成为一个ActiveX控件...
edit: I managed to create 2 simple console applications, and was sending UDP messages between them successfully. The sender will be only for testing, and now I need to re-make my receiver to get the UDP message and 'translate' it to events. And lastly, to make it an ActiveX control...
推荐答案
简单的服务器和客户端:
Simple server and client:
public struct Received
{
public IPEndPoint Sender;
public string Message;
}
abstract class UdpBase
{
protected UdpClient Client;
protected UdpBase()
{
Client = new UdpClient();
}
public async Task<Received> Receive()
{
var result = await Client.ReceiveAsync();
return new Received()
{
Message = Encoding.ASCII.GetString(result.Buffer, 0, result.Buffer.Length),
Sender = result.RemoteEndPoint
};
}
}
//Server
class UdpListener : UdpBase
{
private IPEndPoint _listenOn;
public UdpListener() : this(new IPEndPoint(IPAddress.Any,32123))
{
}
public UdpListener(IPEndPoint endpoint)
{
_listenOn = endpoint;
Client = new UdpClient(_listenOn);
}
public void Reply(string message,IPEndPoint endpoint)
{
var datagram = Encoding.ASCII.GetBytes(message);
Client.Send(datagram, datagram.Length,endpoint);
}
}
//Client
class UdpUser : UdpBase
{
private UdpUser(){}
public static UdpUser ConnectTo(string hostname, int port)
{
var connection = new UdpUser();
connection.Client.Connect(hostname, port);
return connection;
}
public void Send(string message)
{
var datagram = Encoding.ASCII.GetBytes(message);
Client.Send(datagram, datagram.Length);
}
}
class Program
{
static void Main(string[] args)
{
//create a new server
var server = new UdpListener();
//start listening for messages and copy the messages back to the client
Task.Factory.StartNew(async () => {
while (true)
{
var received = await server.Receive();
server.Reply("copy " + received.Message, received.Sender);
if (received.Message == "quit")
break;
}
});
//create a new client
var client = UdpUser.ConnectTo("127.0.0.1", 32123);
//wait for reply messages from server and send them to console
Task.Factory.StartNew(async () => {
while (true)
{
try
{
var received = await client.Receive();
Console.WriteLine(received.Message);
if (received.Message.Contains("quit"))
break;
}
catch (Exception ex)
{
Debug.Write(ex);
}
}
});
//type ahead :-)
string read;
do
{
read = Console.ReadLine();
client.Send(read);
} while (read != "quit");
}
}
这篇关于C#UDP套接字客户端和服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!