这一切背后的故事
我正在尝试编写一个开源 RPG 库存服务器,一次可用于 1 个以上的 RPG,稍后将为其制作游戏。我的第一个版本不支持很多安全或加密。但我想有一个坚实的开始。
问题
它确实接受一个连接,drop in drop out。但是我的线程实现似乎只接受一个连接。有人可以给我一个如何正确执行此操作的指南吗?为了方便您阅读,我已经评论了我将提供的代码。
设置 我使用启用了 SP1 的 visual studio 2010。我只在我的家庭网络中尝试过,因此不需要 IP 转发。它分别接受两台 PC。
非常感谢那些提供帮助的人,如果您提供帮助并希望在最终结果中得到认可,请告诉我。如果有人有一般性意见,我很乐意听到他们;)
编辑:它与 NewIPEP() 方法有关。
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace TCPServer1
{
class ServerV1
{
static int maxthreads = 4;
static bool[] threadavailable = new bool[maxthreads];
static Thread[] listenThread = new Thread[maxthreads];
static IPEndPoint[] ipep = new IPEndPoint[maxthreads];
static bool[] threadconnected = new bool[maxthreads];
static Socket[] newsock = new Socket[maxthreads];
static Socket[] client = new Socket[maxthreads];
static ItemListClassV1 parent;
static Login loginin;
static ProtoColV1[] protocol = new ProtoColV1[maxthreads];
/// <summary>
/// initialises variables and starts a first thread.
/// </summary>
/// <param name="args"></param>
public ServerV1(ItemListClassV1 parentid, Login login)
{
for (int i = 0; i < maxthreads; i++)
{
threadavailable[i] = true;
}
parent = parentid;
loginin = login;
StartThread();
}
/// <summary>
/// should be split but handles the servershit look further for seperate "method" comments
/// </summary>
static void Server()
{
int recv = 0;
byte[] data = new byte[1024];
//looks for incoming connections
ipep[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = NewIPEP();
newsock[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = new
Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Bind(ipep[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)]);
newsock[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Listen(10);
Console.WriteLine("Waiting for a client...");
client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = newsock[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Accept();
//if connected
IPEndPoint clientep =
(IPEndPoint)client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",
clientep.Address, clientep.Port);
//if connection start a new thread
StartThread();
//send welcome message to client
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Send(data, data.Length,
SocketFlags.None);
while (true)//this looks for incoming data and sends reply.
{
try
{
data = new byte[1024];
recv = client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Receive(data);
}
catch (SocketException e)
{
if (e != null)
break;
}
if (client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Connected != true)//when not connected break the loop
{
break;
}
////EXECUTES moet wer trug
//byte[][] packets = protocol[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Execute(data,recv);//EXECUTES THE DATA EXTERNALLY
byte[][] packets = new byte[1][];
packets[0] = data;
for (int i = 0; i < packets.Length; i++)//stuurt vervolgens alle pakketjes
{
client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Send(packets[i], packets[i].Length,
SocketFlags.None);
}
}
//when not connected this continues
Console.WriteLine("Disconnected from {0}",
clientep.Address);
//close socket
client[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Close();
newsock[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)].Close();
bool full = true;
//if the server was full now a slot opened
for (int l = 0; l < maxthreads; l++)
{
if (threadavailable[l])
{
full = false;
}
if (!threadavailable[l] && !threadconnected[l])
{
full = false;
}
}
//closes thread, deletes information
threadavailable[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = true;
ipep[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = null;
protocol[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = null;
if (full)
{
StartThread();
}
//stops thread
listenThread[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = null;
System.Threading.Thread.CurrentThread.Abort();
}
/// <summary>
/// keeps searching for incoming connections and returns the found connection when found.
/// </summary>
/// <returns></returns>
static IPEndPoint NewIPEP()
{
IPEndPoint temp = null;
bool newconnection = false;
while (!newconnection)
{
temp = new IPEndPoint(IPAddress.Any, 9050);
for (int k = 0; k < maxthreads; k++)
{
if (!threadavailable[k] && ipep[k] != null && ipep[k].Address != null )
{
if (temp.Address == ipep[k].Address)
{
k = maxthreads;
break;
}
}
if (k == maxthreads - 1)
{
newconnection = true;
threadconnected[Convert.ToInt32(System.Threading.Thread.CurrentThread.Name)] = true;
}
}
}
return temp;
}
/// <summary>
/// finds the first unstarted thread and starts it
/// </summary>
static void StartThread()
{
for (int i = 0; i < maxthreads; i++)
{
if (threadavailable[i])
{
threadavailable[i] = false;
threadconnected[i] = false;
protocol[i] = new ProtoColV1(parent,loginin);
listenThread[i] = new Thread(Server);
listenThread[i].Name = Convert.ToString(i);
listenThread[i].Start();
break;
}
}
}
}
}
最佳答案
您只调用了一次 Socket client = newsock.Accept();,这就是为什么只接受一个客户端。
关于c# - 为什么这个多线程多连接的TCP服务器只接受一个连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755339/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以