Skip to content
Browse files

test for max live connection

  • Loading branch information...
1 parent c35257f commit 824418ac89b2da8c76d8183c9d0559323cb2f556 @marzwu committed Apr 22, 2012
View
45 MyServer/Client/Program.cs
@@ -11,7 +11,18 @@ class Program
{
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)
{
@@ -64,7 +75,37 @@ static void Connect(String server, String message)
}
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;
}
}
}
View
1 MyServer/Client/client.bat
@@ -0,0 +1 @@
+client.exe
View
1 MyServer/Client/do.bat
@@ -0,0 +1 @@
+for /l %%i in (1,1,10) do start client.bat
View
28 MyServer/MyServer.sln
@@ -5,20 +5,48 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyWorld", "MyWorld\MyWorld.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{80DB81A4-EFAB-400A-9DC7-FCFDA3AB6536}"
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
GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
+ Release|Any CPU = Release|Any CPU
+ Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
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.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.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.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.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
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
63 MyServer/MyWorld/Log.cs
@@ -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);
+ }
+ }
+}
View
8 MyServer/MyWorld/MyWorld.csproj
@@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Log.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TcpServer.cs" />
@@ -51,6 +52,13 @@
<None Include="app.config">
<SubType>Designer</SubType>
</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>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
6 MyServer/MyWorld/Program.cs
@@ -1,18 +1,22 @@
using System;
+using log4net;
+using System.Reflection;
namespace MyWorld
{
class Program
{
static void Main(string[] args)
{
+ //ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
try
{
new TcpServer().start();
+ Log.Info("server started");
}
catch (Exception e)
{
- Console.WriteLine("error in main: {0}", e);
+ Log.Fatal(e);
}
}
}
View
1 MyServer/MyWorld/Properties/AssemblyInfo.cs
@@ -5,6 +5,7 @@
// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
+[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
[assembly: AssemblyTitle("MyWorld")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
View
25 MyServer/MyWorld/TcpServer.cs
@@ -1,15 +1,16 @@
using System;
-
-using System.Net.Sockets;
using System.Net;
+using System.Net.Sockets;
using System.Threading;
namespace MyWorld
{
public class TcpServer
{
+ private int connectNum;
public void start()
{
+ connectNum = 0;
init();
}
@@ -25,7 +26,7 @@ private void init()
}
catch (Exception e)
{
- Console.WriteLine("listener exception: {0}", e);
+ Log.Fatal("listener exception", e);
}
}
@@ -38,20 +39,23 @@ private void listenClientConnect(Object args)
try
{
newClient = listener.AcceptTcpClient();
+ //每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息;
+ Thread recieveThread = new Thread(ReceiveData);
+ recieveThread.Start(newClient);
}
- catch
+ catch (Exception e)
{
- //因此可以利用此异常退出循环
+ //因此可以利用此异常退出循环
+ Log.Fatal("wait to connect: {0}", e);
break;
}
- //每接收一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息;
- Thread recieveThread = new Thread(ReceiveData);
- recieveThread.Start(newClient);
}
}
private void ReceiveData(object tcpClient)
{
+ connectNum++;
+ Console.WriteLine("connest: {0}", connectNum);
try
{
TcpClient client = tcpClient as TcpClient;
@@ -78,14 +82,15 @@ private void ReceiveData(object tcpClient)
}
catch (Exception e)
{
- Console.WriteLine("SocketException: {0}", e);
+ Log.Fatal("SocketException: {0}", e);
}
}
Console.WriteLine("user logout");
}
catch (Exception e)
{
- Console.WriteLine("client exception: {0}", e);
+ Log.Fatal("client exception: {0}", e);
+ connectNum--;
}
}
}
View
5 MyServer/MyWorld/app.config
@@ -1,3 +1,6 @@
<?xml version="1.0"?>
<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>
View
23 MyServer/MyWorld/log4net.config
@@ -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.
Something went wrong with that request. Please try again.