UDP - 具有两个客户端的服务器C# [英] UDP - Server C# with two clients

查看:209
本文介绍了UDP - 具有两个客户端的服务器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我设法创建了一个聊天程序,其中两个客户端可以相互发送和接收消息。我正在尝试改进它,以便不是客户端发送给对方,而是首先将消息发送到服务器,然后服务器将其发送给客户端。



关于如何创建服务器的任何想法?



谢谢

解决方案

我写了其中一个在VB6很久以前。好玩。 :)



服务器需要绑定到客户端发送的udp端口。然后你的服务器需要发送客户端正在监听的端口。



你想要实现某种协议,以便你的服务器可以看到消息是什么以及它是谁。我不知道有任何内置方法可以做到这一点,所以你可以只构建你的消息,这样第一部分就是它来自谁,然后是谁,等等。



然后您的服务器将解析该消息并能够确定如何处理它。它将看到它将要发送给谁并发送它。这可能会非常快速地变得非常复杂,因为您可能希望收到客户端发回的消息,确认他们收到了消息。你可以做各种各样的事情。



可能有一个.Net的图书馆,你可以下载它来促进其中的一些,所以你不要我必须手工完成,但我不熟悉任何。我建议搜索谷歌以查看是否有任何预先构建的解决方案或至少一些部分,以使其更容易。


请参阅我对该问题的评论,并重新考虑您使用UDP的想法。总的来说,它可能看起来比TCP困难得多。从TCP套接字开始,您可以在几个不同的级别上使用网络。请参阅我对它们的简短概述:

我如何将byte []发送到其他电脑 [ ^ ],

通信b / w局域网上的两个Windows应用程序。 [ ^ ]。



对于其他一些想法(与聊天没有直接关系,但有用的想法),请看看我过去的回答:套接字编程中的业余问题 [ ^ ]。



-SA

Hi
I have managed to create a chat program where two clients can send and receive messages from each other. I am trying to improve it so that instead of the clients sending it to each other, the message is first sent to a server and then the server sends it to the client.

Any Ideas on how to create the server?

Thanks

解决方案

I wrote one of these in VB6 long ago. It was fun. :)

The server needs to bind to the udp port that your clients send on. And then your server needs to send on the port that your clients are listening to.

You'll want to have some sort of protocol that you implement so that your server can see what the message is and who it is for. I don't know of any built in ways to do that so you can just structure your messages so that the first part is who it is from, then who it is to, etc, etc.

Then your server will parse the message and be able to determine what to do with it. It will see who it is going to and send it on. This can get very complicated very quickly because you might want to have a message that the client sends back acknowledging they received the message. There are all kinds of things you can do.

It may be likely that there is a library for .Net you can download to facilitate some of it so you don't have to do it all by hand but I am not familiar with any. I would recommend searching google to see if there are any pre built solutions or at least some pieces to make it easier.


Please see my comment to the question and rethink you idea to use UDP. Overall, it may appear much more difficult than TCP. You can use networking on several different levels, starting from TCP sockets. Please see my short overviews of them:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

For some other ideas (not directly related to the chat, but useful to think at), please take a look at my past answer: an amateur question in socket programming[^].

—SA


这篇关于UDP - 具有两个客户端的服务器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆