服务器侦听器类开始接受来自所有IP的传入连接:
while (true)
{
CommandManager cm = new CommandManager(s.Accept());
}
public CommandManager(Socket clientSocket)
{
this.socket = clientSocket;
this.networkStream = new NetworkStream(this.socket);
this.bwReceiver = new BackgroundWorker();
this.bwReceiver.DoWork += new DoWorkEventHandler(StartReceive);
this.bwReceiver.RunWorkerAsync();
}
private void StartReceive(object sender, DoWorkEventArgs e)
{
while (this.socket.Connected)
{
string s = dataStr.Substring(10);
Command c = JsonConvert.DeserializeObject<Command>(s);
CommandHandler.HandleCommand(c, clientIP, this.socket);
this.networkStream.Flush();
}
}
CommandHandler
根据命令执行不同的操作,并且当收到命令时(来自Corona SDK应用程序),一切都很好。问题是我需要在收到消息后发回消息。
首先,如果玩家登录,我在通信类中保存该通信信息,保存套接字信息:
Player.Player p = new Player.Player(cmd.PlayerID);
p.Communication = new ClientCommunication(clientSocket);
但是,如果命令是&#34; UpdateTable&#34;我还需要将表信息发回给所有活跃的玩家,这就是问题发生的地方:
if (cmd.Cmd = Command.UpdateTable)
{
//Do the updates
//Send back the updates to the players
Command c = new Command();
c.Cmd = Commands.ReceiveTables;
c.Tables = new List<Table.Table>();
Table.Table t = Table.TableManager.ReturnTable(cmd.Table.TableID);
c.Tables.Add(t);
string command = JsonConvert.SerializeObject(c);
command = command + Environment.NewLine;
foreach (Table.Seat seat in t.Players)
{
try
{
Communication.ClientCommunication com = Player.PlayerManager.GetCommunication(seat.Player.PlayerID);
Socket client = com.ReturnSocket();
client.Send(Encoding.ASCII.GetBytes(command));
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
}
}
}
我已经验证在client.Send()
端口和IP是正确的,但我只收到一封回原始发件人的消息(即发送消息的播放器收到消息但其他播放器没有& #39; t从client.Send()
收到任何内容。