Tcp 我从客户端接收数据,但客户端只能在本地PC上从服务器读取数据。为什么?
我有一个TCPC客户端和服务器,在我的电脑上一切正常,如果我将客户端发送到其他电脑(在同一网络中),客户端连接到服务器并可以发送数据,但客户端不读取数据或服务器不发送,我没有任何错误…这是什么原因 我试着用词 从tcp更改为套接字 完全一样 添加防火墙例外 删除防病毒软件 服务器Tcp 我从客户端接收数据,但客户端只能在本地PC上从服务器读取数据。为什么?,tcp,server,client,message,tcpclient,Tcp,Server,Client,Message,Tcpclient,我有一个TCPC客户端和服务器,在我的电脑上一切正常,如果我将客户端发送到其他电脑(在同一网络中),客户端连接到服务器并可以发送数据,但客户端不读取数据或服务器不发送,我没有任何错误…这是什么原因 我试着用词 从tcp更改为套接字 完全一样 添加防火墙例外 删除防病毒软件 服务器 using System.Threading; using System.Net.Sockets; using System.Text; using System.Collections; using System.N
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public static Hashtable clientsList = new Hashtable();
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(IPAddress.Any,3306);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
Console.WriteLine("Chat Server Started ....");
counter = 0;
while ((true))
{
try
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
clientsList.Add(dataFromClient, clientSocket);
broadcast(dataFromClient + " Joined ", dataFromClient, false);
Console.WriteLine(dataFromClient + " Joined chat room ");
handleClinet client = new handleClinet();
client.startClient(clientSocket, dataFromClient, clientsList);
}
catch { }
}
Console.ReadKey();
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine("exit");
Console.ReadLine();
}
public static void broadcast(string msg, string uName, bool flag)
{
try
{
foreach (DictionaryEntry Item in clientsList)
{
TcpClient broadcastSocket;
broadcastSocket = (TcpClient)Item.Value;
NetworkStream broadcastStream = broadcastSocket.GetStream();
Byte[] broadcastBytes = null;
if (flag == true)
{
broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
}
else
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
broadcastStream.Flush();
}
}
catch { Console.ReadKey(); }
} //end broadcast function
}//end Main class
public class handleClinet
{
TcpClient clientSocket;
string clNo;
Hashtable clientsList;
public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
this.clientsList = cList;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
if (dataFromClient == "closing")
{
clientsList.Remove(clNo);
Program.broadcast("Sa deconectat", clNo, true);
break;
}
Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
rCount = Convert.ToString(requestCount);
Program.broadcast(dataFromClient, clNo, true);
}
catch (Exception ex)
{
Console.WriteLine("Opsy");
Console.ReadKey();
}
}//end while
}//end doChat
} //end class handleClinet
}//end namespace
即使我删除了try-catch,我也没有错误,我从服务器和客户端删除了它们,没有错误…只是快速查看了一下,所以不确定这是否是问题所在,但当您不在自己的本地系统上测试时,您需要告诉程序特定的IP和要连接的端口。只要您在同一子网上,就不必担心端口转发,但您应该让客户端和服务器知道您要使用的端口和地址