C#中的套接字编程 [英] socket programming in C#

查看:72
本文介绍了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) 
        {
        }
    }
}

推荐答案

在同一端口上,您可以将无限数量的客户端连接到System.Net.Sockets.TcpListener实例的每个侦听套接字(受类似可用因素的限制)内存之类的东西).在这种情况下,在服务器端接受的每个套接字或TcpClient上,服务器端都获得一个套接字或TcpClient的实例,该实例表示所连接客户端的远程套接字.服务器端可以使用这些实例向网络流发送/接收或向网络流写入/从网络流中读取/读取(如果TcpListener的话).

我不知道如何更改代码,只是因为我不知道您的目标.但是,您显示的代码看起来并不完整.首先,这是因为您没有使用线程.服务器端至少需要两个附加线程:一个用于接受新连接,另一个用于发送/接收套接字数据或向/从网络流写入/读取.

如果您试图在用户界面中执行任何阻止调用,请认为您已完成-它永远无法正常工作.

不清楚为什么要在服务器端使用Forms应用程序.服务器端应用程序的适当形式是Windows服务.但是,如果您想使用Form应用程序进行入门实验,研究和/或原型制作,那就很好了.

您可以在过去的解决方案中找到我在该主题上分享的一些想法:
来自同一端口号的多个客户端 [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
On the same port, you can connect unlimited number of clients to each listening socket of instance of System.Net.Sockets.TcpListener (limited by factors like available memory and the like). In this case, on each socket or TcpClient accepted by the server side, the server side obtains an instance of socket or TcpClient representing remote socket of the connected client. These instances can be used by a server side to send/recieve or write/read to/from the network stream (in case if TcpListener).

I don''t know how to change your code just because I don''t know your goal. However, the code you''ve shown does not look like having full sense. First of all, this is because you are not using threads. A server side needs two additional thread as minimum: one to accept new connections, another one to send/receive socket data or write/read to/from the network streams.

If you are trying to perform any blocking calls in your UI, consider you are done — it can never work properly.

Not clear why do you want to use Forms application for the server side. The adequate form of server-side application is a Windows Service. However, if you want to use the Form application for introductory experiments, research and/or prototyping, it''s perfectly fine.

You can find some ideas I shared on this topic in my past solution:
Multple clients from same port Number[^].

For threads working with UI (which is more typically developed on the client side), you need to use UI thread invocation. Please see:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

For more on threading, please see my collection of my past solutions:

How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于C#中的套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆