[英]Multi Client TCP Server not acceping some clients c#
I am trying to make a server that will accept clients, and will communicate with them until they say "exit". 我正在尝试制作一个将接受客户端的服务器,并将与他们进行通信,直到他们说“退出”为止。 the problem is, after 5-8 clients, the server decides that he don't want to accept any more clients, and when a client is connected.. I simply don't see a "client connected : 123.242.123.13:92313" in the console.
问题是,在5到8个客户端之后,服务器决定他不想再接受任何客户端,并且在客户端连接后。.我根本看不到“客户端已连接:123.242.123.13:92313”在控制台中。 anyone knows the problem?
有人知道这个问题吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace GatherServer
{
class Program
{
public static int connections = 0, team1 = 0, team2 = 0, playing = 12;
public static string lbleader = "";
public static bool isleader = false;
static TcpListener myList = new TcpListener(IPAddress.Any, 7846);
static void Listeners()
{
string charname = "";
bool inque = false;
Socket socketForClient = myList.AcceptSocket();
if(socketForClient.Connected)
{
Console.WriteLine("Client Connected: " + socketForClient.RemoteEndPoint);
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);
while (true)
{
string newString = streamReader.ReadLine();
if (newString.Contains("#") && inque == false)
{
if (newString.Contains("Leader"))
{
newString = newString.Replace("Leader","");
if (!isleader)
{
isleader = true;
lbleader = newString;
charname = lbleader;
Console.WriteLine(lbleader + " is the gather leader");
}
}
inque = true;
charname = newString;
connections++;
Console.WriteLine("connections: " + connections + " | from: "+charname);
}
if (newString == "exit")
{
if (charname == lbleader)
{
isleader = false;
Console.WriteLine(lbleader + " Left, there is not active leader");
}
else
Console.WriteLine(charname + " has left the game: "+socketForClient.RemoteEndPoint);
if (inque == true)
connections--;
break;
}
else
{
streamWriter.WriteLine(connections.ToString());
streamWriter.Flush();
if (connections == 12)
{
if (!isleader)
{
lbleader = charname;
Console.WriteLine(lbleader + " is the gather leader");
isleader = true;
}
Random rnd = new Random();
int team = rnd.Next(1, 3); // creates a number between 1 and 2
if(team == 1)
{
if (team1 < 6)
{
team1++;
Console.WriteLine(newString + " " + socketForClient.RemoteEndPoint + " assigned to Team 1");
streamWriter.WriteLine("team1"+lbleader);
streamWriter.Flush();
inque = false;
playing--;
break;
}
else
{
Console.WriteLine(newString + " " + socketForClient.RemoteEndPoint + " assigned to Team 2");
team2++;
streamWriter.WriteLine("team2" + lbleader);
streamWriter.Flush();
inque = false;
playing--;
break;
}
}
if(team == 2)
{
if (team2 < 6)
{
Console.WriteLine(newString + " " + socketForClient.RemoteEndPoint + " assigned to Team 2");
team2++;
streamWriter.WriteLine("team2" + lbleader);
streamWriter.Flush();
inque = false;
playing--;
break;
}
else
{
Console.WriteLine(newString + " " + socketForClient.RemoteEndPoint + " assigned to Team 1");
team1++;
streamWriter.WriteLine("team1" + lbleader);
streamWriter.Flush();
inque = false;
playing--;
break;
}
}
}
}
}
if (playing == 0)
{
connections = playing;
playing = 12;
lbleader = null;
isleader = false;
}
Console.WriteLine("playing:" + playing);
Console.WriteLine("connections: " + connections);
inque = false;
streamWriter.Close();
streamReader.Close();
networkStream.Close();
socketForClient.Close();
}
}
static void Main(string[] args)
{
myList.Start();
Console.WriteLine("Sever is up and running...");
for(int i=0; i<1000;i++)
{
Thread newThread = new Thread(new ThreadStart(Listeners));
newThread.Start();
}
}
}
}
Basically, what I want to do is to give each client a thread of its own. 基本上,我想做的就是给每个客户端一个自己的线程。 but after 5-8 connections, the server Socket socketForClient = myList.AcceptSocket();
但经过5到8次连接后,服务器Socket socketForClient = myList.AcceptSocket(); not working, I think its because I am running out of threads.. but I have no clue why, there should be 1000 threads open when only 5 to 8 connections are made.
无法正常工作,我认为这是因为我用完了线程..但是我不知道为什么,当仅建立5到8个连接时,应该有1000个线程打开。
please help! 请帮忙!
发现问题所在后,我在服务器控制台中强调了一些内容,这导致服务器暂停。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.