接下来是测试用的客户端代码
namespace WindowsApplication2
{
public partial class Form1 : Form
{
Thread th;
private Socket[] s = new Socket[100];
private delegate void setlist(string s1, string s2);
private setlist SetListview;
private delegate void setTextbox(string s);
private setTextbox setbox;
private byte[] date = new byte[1024];
public Form1()
{
InitializeComponent();
} private void setListView(string s1, string s2)
{
listView1.Items.Add(new ListViewItem(new string[] { s1, s2 }));
} private void setboxcontext(string s)
{
listBox1.Items.Add(s);
}
private void button1_Click(object sender, EventArgs e)
{
int port = 8000;
IPAddress ip = IPAddress.Parse("127.0.0.1");
for (int i = 0; i < 100; i++)
{
IPEndPoint iep=new IPEndPoint(ip,port); s[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
th.Start();
s[i].Connect(iep);
this.Invoke(SetListview, new object[] { ip.ToString(),port.ToString() });
date = Encoding.ASCII.GetBytes("a");
s[i].BeginSend(date, 0, date.Length, SocketFlags.None, new AsyncCallback(check), s[i]);
//port++;
}
} private void check(IAsyncResult iar)
{
Socket OtherSocket = (Socket)iar.AsyncState;
int Len = OtherSocket.EndSend(iar);
if (Len == date.Length)
{
this.Invoke(setbox, new object[] { "服务器发送:" + Encoding.Default.GetString(date) });
}
else
{
this.Invoke(setbox, new object[] { Encoding.Default.GetString(date) + "发送失败!" });
}
} private void Form1_Load(object sender, EventArgs e)
{
setbox = new setTextbox(setboxcontext);
SetListview = new setlist(setListView);
}
//private void connect()
//{
//}
}
}
namespace WindowsApplication2
{
public partial class Form1 : Form
{
Thread th;
private Socket[] s = new Socket[100];
private delegate void setlist(string s1, string s2);
private setlist SetListview;
private delegate void setTextbox(string s);
private setTextbox setbox;
private byte[] date = new byte[1024];
public Form1()
{
InitializeComponent();
} private void setListView(string s1, string s2)
{
listView1.Items.Add(new ListViewItem(new string[] { s1, s2 }));
} private void setboxcontext(string s)
{
listBox1.Items.Add(s);
}
private void button1_Click(object sender, EventArgs e)
{
int port = 8000;
IPAddress ip = IPAddress.Parse("127.0.0.1");
for (int i = 0; i < 100; i++)
{
IPEndPoint iep=new IPEndPoint(ip,port); s[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
th.Start();
s[i].Connect(iep);
this.Invoke(SetListview, new object[] { ip.ToString(),port.ToString() });
date = Encoding.ASCII.GetBytes("a");
s[i].BeginSend(date, 0, date.Length, SocketFlags.None, new AsyncCallback(check), s[i]);
//port++;
}
} private void check(IAsyncResult iar)
{
Socket OtherSocket = (Socket)iar.AsyncState;
int Len = OtherSocket.EndSend(iar);
if (Len == date.Length)
{
this.Invoke(setbox, new object[] { "服务器发送:" + Encoding.Default.GetString(date) });
}
else
{
this.Invoke(setbox, new object[] { Encoding.Default.GetString(date) + "发送失败!" });
}
} private void Form1_Load(object sender, EventArgs e)
{
setbox = new setTextbox(setboxcontext);
SetListview = new setlist(setListView);
}
//private void connect()
//{
//}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货