110,545
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BasicSocket.Client;
using System.IO;
using BasicSocket.Data;
using System.Globalization;
using System.Threading;
using System.Net;
namespace testclient
{
public class Connect
{
/// <summary>
/// 建立一个SOCKET客户端
/// </summary>
public static SocketClient[] client = new SocketClient[10000];
// This method will be called when the thread is started.
public void DoWork()
{
client[Program.count] = new SocketClient();
//client.DataOn += new DataOn(client_DataOn); //数据包进入事件
//client.Disconnection += new ExceptionDisconnection(client_Disconnection); //数据包断开事件
string hostip = "192.168.1.17";
string localip = Program.ipAddress + ":" + Convert.ToString(Program.count);
try
{
//if (client.ConnectionTo(hostip, 9982)) //使用同步连接到服务器,一步就用Begin开头的那个
if (client[Program.count].ConnectionTo(hostip, 9982)) //使用同步连接到服务器,一步就用Begin开头的那个
{
while (true)
{
//string localip = Console.ReadLine();
Thread.Sleep(90); //防止调用频繁访问冲突
SendDataClass.PPo temp = new SendDataClass.PPo();
temp.RealWeight = 14.5;
temp.ShaftType = 2;
temp.License = localip;
temp.Checker = "hongyu";
string uploadtime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss", DateTimeFormatInfo.InvariantInfo);
temp.UploadTime = uploadtime;
client[Program.count].SendTo(BufferFormatV2.FormatFCA(temp)); //用组合数据包模拟PPO对象 但GUID 是字符串类型;
//client.SendTo(BufferFormat.FormatFCA(temp));
}
client[Program.count].Close();
}
else
{
Console.WriteLine(Convert.ToString(Program.count) + "无法连接服务器");
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.ToString() + " !!:" + Convert.ToString(Program.count));
}
Console.ReadLine();
//while (!_shouldStop)
//{
// Console.WriteLine("worker thread: working...");
//}
//Console.WriteLine("worker thread: terminating gracefully.");
}
static void client_Disconnection(string message)
{
Console.WriteLine(message);
}
static void client_DataOn(byte[] Data)
{
throw new NotImplementedException();
}
//public void RequestStop()
//{
// _shouldStop = true;
//}
//// Volatile is used as hint to the compiler that this data
//// member will be accessed by multiple threads.
//private volatile bool _shouldStop;
}
class Program
{
public static int count = 0;
public static string ipAddress = "";
static void Main(string[] args)
{
ipAddress = GetIP();
Connect workerObject = new Connect();
Thread[] workerThread = new Thread[10000];
//Console.WriteLine("main thread: Starting worker thread...");
//while (!workerThread.IsAlive);
for ( int i = 0; i < 10000; i++ )
{
workerObject = new Connect();
workerThread[i] = new Thread(workerObject.DoWork);
count++;
Console.WriteLine("new thread." + Convert.ToString(count));
workerThread[i].Start();
Thread.Sleep(1000);
//workerThread.Join();
}
//Console.WriteLine("main thread: Worker thread has terminated.");
}
protected static string GetIP() //获取本地IP
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
return ipAddr.ToString();
}
}
}