2015-07-10 80 views
0

客户的TcpClient/TCPSERVER /监听器,服务器停止读取或客户端停止30秒后

class Client 
{ 
    public TcpClient client; 
    public StreamWriter server; 
    public ServerPlayer serverPlayer; 
    public Player player; 
    public void Connect(Player p) 
    { 
     player = p; 
     serverPlayer = new ServerPlayer(); 
     client = new TcpClient(AddressFamily.InterNetwork); 
     client.Connect("Don't wory my IPV4 Is Here", 8888); 
     NetworkStream stream = client.GetStream(); 
     server = new StreamWriter(stream); 
     Timer t = new Timer(Send, null, 0, 10); 
    } 
    public void Send(Object o) 
    { 
     server.WriteLine("Hello I am bob"); 
     server.Flush(); 

    } 

} 

服务器

class TcpServerConnection 
{ 
    private static TcpListener tcpListener; 
    static void Main(string[] args) 
    { 
     tcpListener = new TcpListener(IPAddress.Any, 8888); 
     tcpListener.Start(); 
     Console.WriteLine("Server started."); 

     while (true) 
     { 
      //blocks until a client has connected to the server 
      TcpClient client = tcpListener.AcceptTcpClient(); 

      //create a thread to handle communication 
      //with connected client 
      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
      clientThread.Start(client); 
     } 
    } 

    private static void HandleClientComm(object client) 
    { 
     while (true) 
     { 
      TcpClient tcpClient = (TcpClient)client; 
      NetworkStream clientStream = tcpClient.GetStream(); 
      StreamReader server = new StreamReader(clientStream); 
      Console.WriteLine(server.ReadLine()); 
     } 


} 
} 

问题

事件追踪:

入门!

喂我鲍勃

喂我鲍勃

(许多你好,我是后来鲍勃)

喂我鲍勃

(你好我鲍勃刚刚停止)

(约30秒)

不知道是因为客户端停止发送还是服务器停止接收或者两者兼而有之!但是,一旦这个运行大约30秒,服务器就停止获取发送信息。没有错误只是它不发送。

+0

呃那些while循环看起来很无聊。尝试删除您的服务器上的while(true)。如果您试图使应用程序保持活动状态,请在主块的末尾使用console.readline。那些while循环将尝试启动一大堆线程。 – kmacdonald

+0

@kmacdonald如果你认为它停止,因为while(true)那么请建议一些东西来替换它?对不起,我是相当新的, –

+0

@kmacdonald也许每个客户端的对象然后每10ms定时器我会通过每个客户端并接收列表中的数据? –

回答

1

发现这是我的互联网。在我的谷歌后。我将我的服务器端口设置为TCP打开。发现垃圾邮件进入随机端口后,我的路由器变得可疑。 IP:IPV4 PORT:8888 SETTINGS:OPEN

0

在你的ServerConnection对象中,第一个while循环没问题。但一个在你的处理器会导致一些问题,试试这个:

private static void HandleClientComm(object client) 
{ 
    TcpClient tcpClient = (TcpClient)client; 
    NetworkStream clientStream = tcpClient.GetStream(); 
    using(var stream = new StreamReader(clientStream)) 
    { 
     while (stream.Peek() >= 0) 
     { 
      Console.WriteLine(server.ReadLine()); 
     } 
    } 
} 
+0

一段时间后仍然停止。大约30秒后停止发送: –

+0

您可以在发送中添加一个断点,看看您是否仍在进入? 10毫秒是一个很短的时间间隔。有些事情必须停止。 – kmacdonald

+0

试用1000ms。花了一分钟左右。为什么一分钟没有意义。断点没有空。一切都很好。 –

0

因为要创建一个且只有一个连接到服务器,然后在客户端不断地发送消息,因此处理客户端的消息时,在服务器程序中,您应该接受一个TcpClient,然后在循环中读取消息,而不是接受Tcplient并在循环中读取所有消息。

private static void HandleClientComm(object client) 
{ 
    TcpClient tcpClient = (TcpClient)client; 
    NetworkStream clientStream = tcpClient.GetStream(); 
    StreamReader server = new StreamReader(clientStream); 
    while (true) 
    { 
     Console.WriteLine(server.ReadLine()); 
    } 
} 
+0

好主意!性能更好!还有30秒就会中断。 –

+0

@ElliotBewey酷!所以这不是一个编码问题。 – ssett

+0

是的:D ..感谢上帝。 –