问题描述
我在寻找进入 SimplSockets 库,用于构建使用套接字的应用程序。然而,经过检查对堆栈溢出,的,源,和几个互联网搜索,我无法找到一个简单的方法使用SimplSockets创建一个Hello World项目。
I’m looking into the SimplSockets library for building an application that uses sockets. However, after checking other questions on Stack Overflow, the author’s blog, the source, and several Internet searches, I can’t find a straightforward way to create a "hello world" project using SimplSockets.
如何创建一个Hello World应用程序使用SimplSockets?
要prevent一个过于宽泛关闭,所有我想要做的就是发送和接收的一些数据。一个字符串,等等。我打开了这个问题的原因是因为我不能确定如何调用构造函数,因为它使用 FUNC< T>
,其中 T
是一个套接字。
To prevent a "too broad" closure, all I want to do is send and receive some data. A string, whatever. The reason I opened this question is because I'm unsure on how to call the constructor since it uses func<T>
where T
is a socket.
推荐答案
我与SimplSockets的问题是,我没有正确理解如何使用需要在构造函数的委托。
The issue I had with SimplSockets is that I didn’t properly understand how to use the delegate needed in the constructor.
下面是一个简单的客户端/服务器相呼应类型的回你的数据。我不知道我应该做 Thread.sleep代码,而不是()
,所以我把它留给那里,除非有人有一个更好的建议。
Below is a sample client/server that echoes the data typed back at you. I have no idea what I should be doing instead of Thread.Sleep()
, so I’ll leave that there unless someone has a better suggestion.
private static void ConnectUsingSimpleSockets()
{
int maxClients = 50;
int maxPeers = 10;
var socketCreator = () => new System.Net.Sockets.Socket(SocketType.Stream, ProtocolType.Tcp);
using (var client = new SimplSockets.SimplSocket(socketCreator, 5000, 10, true))
{
client.MessageReceived += client_MessageReceived;
client.Error += client_Error;
var ss = new System.Net.IPEndPoint(BitConverter.ToInt32(IPAddress.Parse("127.0.0.1").GetAddressBytes(), 0), 4747);
if (client.Connect(ss))
{
Console.WriteLine("type something..");
while (true)
{
string resul = Console.ReadLine();
byte[] data = client.SendReceive(UTF8Encoding.UTF8.GetBytes("Client Send: " + resul + DateTime.Now));
if (data == UTF8Encoding.UTF8.GetBytes("END"))
{
break;
}
Console.WriteLine(UTF8Encoding.UTF8.GetString(data));
}
}
client.Close();
client.Listen(ss);
while (true)
{
Console.WriteLine("sleeping");
Thread.Sleep(7000);
}
client.Close();
}
}
这篇关于如何使用SimplSockets与委托A&QUOT;世界你好&QUOT;项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!