Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:从当前活动套接字向另一个套接字发送消息_C#_Sockets - Fatal编程技术网

C# C:从当前活动套接字向另一个套接字发送消息

C# C:从当前活动套接字向另一个套接字发送消息,c#,sockets,C#,Sockets,服务器侦听器类开始接受来自所有IP的传入连接: while (true) { CommandManager cm = new CommandManager(s.Accept()); } public CommandManager(Socket clientSocket) { this.socket = clientSocket; this.networkStream = new NetworkStream(this.socket); this.bwReceive

服务器侦听器类开始接受来自所有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();
    }
}
但是,如果该命令是可更新的,我还需要将表信息发送回所有活动玩家,这就是问题所在:

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());
        }   
    }
}

我已经在客户端验证了这一点。将端口和IP发送为正确的,但我只收到一条消息返回给原始发件人,即发送消息的播放机收到消息,但其他播放机没有收到来自客户端的任何消息。发送。

t播放机中有多少个席位?循环是否正确处理所有席位?是的,我添加了一个mbox,打印出一条消息,我得到两个弹出窗口,也打印出了不同的端口,这都是正确的-弹出窗口的数量,即玩家的数量,端口号和IP所有IP都是本地的,并且在来自Corona模拟器的同一台机器上。您是否使用网络嗅探器查看消息是否发送到客户端?发送到客户端的两条消息之一包含信息,根据SmartSniff,另一条消息为空。我想知道这是否与互相拦截的消息有关?是否需要通过异步或运行不同线程之类的方式发送它们?我对您的代码有两个问题:1。在调用client.send时,您正在使用一个临时缓冲区作为发送缓冲区。我不确定那有多安全。2.您没有获取发送函数发送的字节数。您的套接字是否处于阻塞/非阻塞模式?3.就设计而言,我不喜欢通信对象暴露套接字的方式。我认为更好的做法是用您自己的界面来包装它,但这当然取决于您。
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());
        }   
    }
}