我正在开发一个控制台应用程序,允许客户端将字符串发送到另一个客户端。 这里是我当前需求的简要
1.)多个TcpClient使用多线程连接到我的服务器。 (完成)
2.)连接时更新数据库。 (完成)
2.)向客户回复消息。 (完成)
3.)从客户端A转发消息到目标客户端B.客户端A需要传递2参数,它是消息和目标客户端名称,我将在数据库中搜索IP地址和端口(填充)C#Tcpclient客户端发送消息到另一个客户端
现在我将消息转发给客户端B正在stucking它会返回错误:
"The Request Address Is Not valid In Its Context".
这里是我的代码发送消息。函数在Client A线程中调用。
static bool SendTargetMessage(CommandJson commandJson, Machine machine) { try {
IPAddress targetIP = IPAddress.Parse(machine.machineIP); int targetPort = Convert.ToInt32(machine.machinePort); Console.WriteLine("Target IP : " + targetIP.ToString()); Console.WriteLine("Target Port : " + targetPort); IPEndPoint targetEndPoint = new IPEndPoint(targetIP, targetPort); TcpClient targetClient = new TcpClient(targetEndPoint);Console.WriteLine("LOL"); if (targetClient.GetStream().CanWrite) { byte[] responseByte = ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(commandJson)); targetClient.GetStream().Write(responseByte, 0, responseByte.Length); return true; } else { return false; } } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } }
从'machine.machineIP'检查IP,可能是IP地址无效。也尝试获取发生错误的特定行。 – NikolayKondratyev
该ip是正确的,该错误发生在TcpClient targetClient = new TcpClient(targetEndPoint); –