Skip to content

Commit

Permalink
test for max live connection
Browse files Browse the repository at this point in the history
  • Loading branch information
marzwu committed Apr 22, 2012
1 parent c35257f commit 824418a
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 14 deletions.
45 changes: 43 additions & 2 deletions MyServer/Client/Program.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Connect("127.0.0.1", "hello world"); //int i = 0;
//SetInterval(500, e =>
//{
// Connect("127.0.0.1", i.ToString());
// Console.WriteLine(i++);
//});
for (int i = 0; i < 50; i++)
{
Connect("127.0.0.1", i.ToString());
Console.WriteLine(i);
}
Console.ReadKey();
} }
static void Connect(String server, String message) static void Connect(String server, String message)
{ {
Expand Down Expand Up @@ -64,7 +75,37 @@ static void Connect(String server, String message)
} }


Console.WriteLine("Press Enter to continue..."); Console.WriteLine("Press Enter to continue...");
Console.Read(); //Console.Read();
}

/// <summary>
/// 在指定时间过后执行指定的表达式
/// </summary>
/// <param name="interval">事件之间经过的时间(以毫秒为单位)</param>
/// <param name="action">要执行的表达式</param>
public static void SetTimeout(double interval, Action action)
{
System.Timers.Timer timer = new System.Timers.Timer(interval);
timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Enabled = false;
action();
};
timer.Enabled = true;
}
/// <summary>
/// 在指定时间周期重复执行指定的表达式
/// </summary>
/// <param name="interval">事件之间经过的时间(以毫秒为单位)</param>
/// <param name="action">要执行的表达式</param>
public static void SetInterval(double interval, Action<System.Timers.ElapsedEventArgs> action)
{
System.Timers.Timer timer = new System.Timers.Timer(interval);
timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
{
action(e);
};
timer.Enabled = true;
} }
} }
} }
1 change: 1 addition & 0 deletions MyServer/Client/client.bat
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
client.exe
1 change: 1 addition & 0 deletions MyServer/Client/do.bat
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
for /l %%i in (1,1,10) do start client.bat
28 changes: 28 additions & 0 deletions MyServer/MyServer.sln
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,20 +5,48 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyWorld", "MyWorld\MyWorld.
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "log4net", "D:\My Documents\Downloads\log4net-1.2.11\src\log4net.csproj", "{181FE707-E161-4722-9F38-6AAAB6FAA106}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86 Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86 Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|Any CPU.ActiveCfg = Debug|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|Mixed Platforms.Build.0 = Debug|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|x86.ActiveCfg = Debug|x86 {FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|x86.ActiveCfg = Debug|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|x86.Build.0 = Debug|x86 {FD784351-E61B-4AE3-A294-3E6244C511B6}.Debug|x86.Build.0 = Debug|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|Any CPU.ActiveCfg = Release|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|Mixed Platforms.ActiveCfg = Release|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|Mixed Platforms.Build.0 = Release|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|x86.ActiveCfg = Release|x86 {FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|x86.ActiveCfg = Release|x86
{FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|x86.Build.0 = Release|x86 {FD784351-E61B-4AE3-A294-3E6244C511B6}.Release|x86.Build.0 = Release|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|Any CPU.ActiveCfg = Debug|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|Mixed Platforms.Build.0 = Debug|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|x86.ActiveCfg = Debug|x86 {80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|x86.ActiveCfg = Debug|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|x86.Build.0 = Debug|x86 {80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Debug|x86.Build.0 = Debug|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|Any CPU.ActiveCfg = Release|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|Mixed Platforms.ActiveCfg = Release|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|Mixed Platforms.Build.0 = Release|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|x86.ActiveCfg = Release|x86 {80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|x86.ActiveCfg = Release|x86
{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|x86.Build.0 = Release|x86 {80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}.Release|x86.Build.0 = Release|x86
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Debug|Any CPU.Build.0 = Debug|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Debug|x86.ActiveCfg = Debug|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Release|Any CPU.ActiveCfg = Release|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Release|Any CPU.Build.0 = Release|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{181FE707-E161-4722-9F38-6AAAB6FAA106}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
Expand Down
63 changes: 63 additions & 0 deletions MyServer/MyWorld/Log.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;

namespace MyWorld
{
public class Log
{
protected static ILog iLog;
public static ILog logger
{
get
{
if (null == iLog)
iLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
return iLog;
}
}

public static void Debug(object message)
{
logger.Debug(message);
}

public static void Debug(object message, Exception exception)
{
logger.Debug(message, exception);
}

public static void Fatal(object message)
{
logger.Fatal(message);
}

public static void Fatal(object message, Exception exception)
{
logger.Fatal(message, exception);
}

public static void Info(object message)
{
logger.Info(message);
}

public static void Info(object message, Exception exception)
{
logger.Info(message, exception);
}

public static void Warn(object message)
{
logger.Warn(message);
}

public static void Warn(object message, Exception exception)
{
logger.Warn(message, exception);
}
}
}
8 changes: 8 additions & 0 deletions MyServer/MyWorld/MyWorld.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Log.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TcpServer.cs" /> <Compile Include="TcpServer.cs" />
Expand All @@ -51,6 +52,13 @@
<None Include="app.config"> <None Include="app.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>
<None Include="log4net.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="D:\My Documents\Downloads\log4net-1.2.11\src\log4net.csproj">
<Project>{181FE707-E161-4722-9F38-6AAAB6FAA106}</Project>
<Name>log4net</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
6 changes: 5 additions & 1 deletion MyServer/MyWorld/Program.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,22 @@
using System; using System;
using log4net;
using System.Reflection;


namespace MyWorld namespace MyWorld
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
//ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
try try
{ {
new TcpServer().start(); new TcpServer().start();
Log.Info("server started");
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("error in main: {0}", e); Log.Fatal(e);
} }
} }
} }
Expand Down
1 change: 1 addition & 0 deletions MyServer/MyWorld/Properties/AssemblyInfo.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// 有关程序集的常规信息通过以下 // 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改 // 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。 // 与程序集关联的信息。
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
[assembly: AssemblyTitle("MyWorld")] [assembly: AssemblyTitle("MyWorld")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
Expand Down
25 changes: 15 additions & 10 deletions MyServer/MyWorld/TcpServer.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,16 @@
using System; using System;

using System.Net.Sockets;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Threading; using System.Threading;


namespace MyWorld namespace MyWorld
{ {
public class TcpServer public class TcpServer
{ {
private int connectNum;
public void start() public void start()
{ {
connectNum = 0;
init(); init();
} }


Expand All @@ -25,7 +26,7 @@ private void init()
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("listener exception: {0}", e); Log.Fatal("listener exception", e);
} }
} }


Expand All @@ -38,20 +39,23 @@ private void listenClientConnect(Object args)
try try
{ {
newClient = listener.AcceptTcpClient(); newClient = listener.AcceptTcpClient();
//每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息;
Thread recieveThread = new Thread(ReceiveData);
recieveThread.Start(newClient);
} }
catch catch (Exception e)
{ {
//因此可以利用此异常退出循环 //因此可以利用此异常退出循环
Log.Fatal("wait to connect: {0}", e);
break; break;
} }
//每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息;
Thread recieveThread = new Thread(ReceiveData);
recieveThread.Start(newClient);
} }
} }


private void ReceiveData(object tcpClient) private void ReceiveData(object tcpClient)
{ {
connectNum++;
Console.WriteLine("connest: {0}", connectNum);
try try
{ {
TcpClient client = tcpClient as TcpClient; TcpClient client = tcpClient as TcpClient;
Expand All @@ -78,14 +82,15 @@ private void ReceiveData(object tcpClient)
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("SocketException: {0}", e); Log.Fatal("SocketException: {0}", e);
} }
} }
Console.WriteLine("user logout"); Console.WriteLine("user logout");
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("client exception: {0}", e); Log.Fatal("client exception: {0}", e);
connectNum--;
} }
} }
} }
Expand Down
5 changes: 4 additions & 1 deletion MyServer/MyWorld/app.config
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,6 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
23 changes: 23 additions & 0 deletions MyServer/MyWorld/log4net.config
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="999999" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level- %message - %logger [%property{NDC}] (%file:%line)%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level- %message - %logger [%property{NDC}] (%file:%line)%newline" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>

0 comments on commit 824418a

Please sign in to comment.