我已经糊涂了!

VFanYan 2002-11-28 02:06:26
我昨天问了一个问题:
---------------------------
在SOCKET里怎么没有事件?
那么如果对方发数据过来,我如果触发处理程序?
-----------------------------
有人回答我:
------------------------------
要用两个SOCKET一个收,一个发!
-----------------------------------
可我在程序里建立了两个SOCKET,一个负责接收,一个负责发送,为什么当其中一个:
sock.Listen(10)时,发送的那个一就无法向对方发送数据了?如果把sock.Listen(10)去掉,则发送正常。
--------------------------------
又有人说:
建立一个就行了。
---------------------------------
如果是一个,那在SOCKET里怎么没有事件?
那么如果对方发数据过来,我如何触发处理程序?




我现在该听谁的???
...全文
53 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yarshray 2002-11-28
  • 打赏
  • 举报
回复
Server Side
If you have understood whatever I have described so far, you will easily understand the Server part of the socket application. So far we have been talking about a client making connection to a server and sending and receiving data. On the Server end, the application has to send and receive data. But in addition to adding and receiving data, server has to allow the clients to make connections by listening at some port. Server does not need to know client I.P. addresses. It really does not care where the client is because its not the server but client who is responsible for making connection. Server's responsibility is to manage client connections.

On the server side there has to be one socket called the Listener socket that listens at a specific port number for client connections. When the client makes a connection, the server needs to accept the connection and then in order for the server to send and receive data from that connected client it needs to talk to that client through the socket that it got when it accepted the connection . Following code illustrates how server listens to the connections and accepts the connection:

public Socket m_socListener;
public void StartListening()
{
try
{
//create the listening socket...
m_socListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
//bind to local IP Address...
m_socListener.Bind( ipLocal );
//start listening...
m_socListener.Listen (4);
// create the call back for any client connections...
m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
cmdListen.Enabled = false;
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}

waitforyoueveryday 2002-11-28
  • 打赏
  • 举报
回复
我的意思是用偵听端口的方法
shenanigan 2002-11-28
  • 打赏
  • 举报
回复
哪个成功了就听哪个地
waitforyoueveryday 2002-11-28
  • 打赏
  • 举报
回复
你不会帧听吗?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧