本文介绍了C#中的套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有多少个客户端可以连接到该服务器?如何更改服务器代码?
How many clients can connect to this server? How do I change the server code?
private void btnserverconect_Click(object sender, EventArgs e)
{
Form1_Load(sender, e);
}
private void Form1_Load(object sender, EventArgs e)
{
try {
server =
new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
//IPAddress local = IPAddress.Parse("127.0.0.1");
EndPoint destination = new IPEndPoint(IPAddress.Any, 8000);
server.Bind(destination);
server.Listen(5);
Thread wait = new Thread(wa);
wait.Start();
}
catch(Exception)
{
MessageBox.Show("connection error !");
}
}
void wa()
{
label1.Text = "please wait...";
server = server.Accept();
label1.Text = "connect";
while (true) {
try
{
byte[]by = new byte[100];
int n = server.Receive(by);
lstserver.Items.Add("client :" +
Encoding.ASCII.GetString(by, 0, n));
}
catch(Exception)
{
}
}
}
推荐答案
这篇关于C#中的套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!