在C#中,如何让套接字继续保持打开状态并接受新数据? [英] In C# how do I have a socket continue to stay open and accept new data?

查看:156
本文介绍了在C#中,如何让套接字继续保持打开状态并接受新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小Windows窗体,当我按下按钮时,套接字进入监听状态。我从客户端程序发送数据(只是简单的文本),我的表单显示信息。

我希望程序继续监听更多数据,直到我按下取消按钮或其他东西。

当我再次按下listen按钮时,我尝试绑定()套接字时出现错误。



提供的参数无效

这个错误只会在第二次按下按钮时弹出。一旦收到信息,我尝试关闭套接字。并将LingerOption设置为false。



当我按下listen按钮时调用的代码是

  s1.Bind(ENDP); 
s1.Listen(10);
connected = true;
Receive();
s1.Shutdown(SocketShutdown.Both);
s1.Close();

有人知道我错过了什么吗?



非常感谢。

解决方案

我想你可能会误解Listen的含义。听并不是开始接受输入,而是开始检查新的连接。您关于收听更多数据的陈述表明您相信收听数据而不是连接。 接收和可用用于检查和接收更多数据。监听是将套接字标记为接收新连接的套接字,并且接受是从一个新连接开始接收。


I have a small Windows Form that when I press a press a button a socket goes into a listen state. I'm sending it data from a client program (just simple text) and my Form displays the information.

I want the program to continue listening for more data until I press a "cancel" button or something.

When I press the listen button again I get an error when I try to bind() the socket.

An invalid argument was supplied

This error only pops up the second time I'm pressing the button. I've tried closing the socket once information is received. And setting the LingerOption to false.

The code that's called when I push the listen button is

        s1.Bind(endP);
        s1.Listen(10);
        connected = true;
        Receive();
        s1.Shutdown(SocketShutdown.Both);
        s1.Close();

Anybody have an idea of what I'm missing?

Thanks a lot.

解决方案

I think you may misunderstand the meaning of Listen. Listen is not to begin receiving input, but rather to start checking for new connections. Your statement about "listening for more data" suggests you belive that listen is for receiving data, not connections. "Receive" and "Available" are for checking on and receiving more data. Listen is for marking the socket as one that receives new connections, and Accept is to begin receiving from a new connection.

这篇关于在C#中,如何让套接字继续保持打开状态并接受新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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