有没有更好的方法来制作服务器客户端应用程序?
我正在使用 c# 中的 tcp 套接字创建一个应用程序服务器客户端。
该应用程序有多个任务,例如文件传输..文件管理器..聊天..(稍后语音聊天)
所以我决定创建一个套接字来接收命令作为细绳 和另一个用于传输文件的套接字..
这是编写服务器客户端应用程序的好方法还是我应该尝试其他方法? 因为用户可以在接收/发送文件时发送消息
以及我如何告诉(文件传输服务器)仅接受已与主服务器连接的同一客户端
例如:服务器侦听端口8000 并接受客户端..并在端口 8111 上传输文件
public StartSever()
{
sr = new StreamReader(networkStream);
while(connected)
{
string[] command = sr.ReadLine().split(',');
switch (Command[0])
{
case "RecFile":
StartFileTransferServer(); // creating new socket tcp listens on port 8111
Receiving();
break;
case "SendFile":
StartFileTransferServer(); // creating new socket tcp listens on port 8111
Sending();
break;
case "Chat":
chat(Command[1]);
break;
default:
break;
}
}
I'm making an application server client using tcp sockets in c# ..
The application has multi tasks like file transfer .. file manager .. chat .. (voice chat later)
So I decided to create a socket to receive the commands as a string
and another socket to transfer files ..
is that a good way for programming a server-client application or should I try another way?
because user could send a message while receiving/sending a file
and how could I tell the (file-transfer-server) accept only the same client who has already connected with the main server
ex: server listen on port 8000 and accept clients .. and file transfer on port 8111
public StartSever()
{
sr = new StreamReader(networkStream);
while(connected)
{
string[] command = sr.ReadLine().split(',');
switch (Command[0])
{
case "RecFile":
StartFileTransferServer(); // creating new socket tcp listens on port 8111
Receiving();
break;
case "SendFile":
StartFileTransferServer(); // creating new socket tcp listens on port 8111
Sending();
break;
case "Chat":
chat(Command[1]);
break;
default:
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用某种通信框架或库,它可以抽象 TCP/IP 套接字的详细信息,并允许您在客户端和服务器端之间发送对象、消息和文件,而不必担心所有细节。
您可以考虑使用一些东西:
这些只是一些例子我突然想到有大量这样的框架,它们在不同的抽象级别上工作,并提供更重或更轻的实现。我相信您一定能找到一款满足您基本需求的产品。
You could use some kind of communication framework or library which would abstract the details of TCP/IP sockets and allow you to send objects and messages and files between your client and server end and not have to worry about all the details.
Some things you could look into using instead:
Those are just some examples I thought of off of the top of my head there are tons of such frameworks that work at different levels of abstraction and offer more heavy or light weight implementations. I'm sure you could find one that meets your basic needs.
如果您没有比使用冒号分隔的字符串协议和
switch
语句更长的时间,我建议您就停在那里。我强烈建议您使用带有 netTcpBinding 和双工绑定的 WCF: http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,b891610a-6b78-4b54-b9a6-4ec81c82b7c0.aspx
仅靠 VoiceChat 是不会成功的。为此购买一个组件。
If you haven't gotten longer than using a colon separated string protocol and
switch
statements I recommend that you stop right there.I strongly suggest that you use WCF with netTcpBinding and duplex binding instead: http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,b891610a-6b78-4b54-b9a6-4ec81c82b7c0.aspx
You will not succeed with a VoiceChat on your own. Buy a component for that.
当您调用 Accept 时,您会收到新用户的套接字。之后您可以创建 NetworkStream。
Write 和 Read 是阻塞方法。您可以尝试使用异步方法:BeginRead 和 BeginWrite。
如果您有数千个用户,那么为每个用户创建新线程也不好。
我提出这样的解决方案:一个线程接受连接并将工作发送到线程池。
像这样的东西(注意这只是草稿):
}
When you call Accept you receive socket for new user. After this you can create NetworkStream.
Write and Read are blocking methods. You can try to use asynchronous methods: BeginRead and BeginWrite.
Also creating new thread per user is not good if you will have thousands users.
I propose such solution: One thread Accpects connections and send work to threads pool.
Something like this (note this is only draft):
}