TCP监听套接字错误
问题描述:
我有2个程序,客户端和服务器,客户端程序通过TCP协议发送特定端口的数据(EX:1370)。
我使用下面的代码来等待我的服务器程序中的客户端。TCP监听套接字错误
IPAddress IP = (my IP Address);
IPEndPoint ipep = new IPEndPoint(IP, 1370);
listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
listenSocket.Bind((EndPoint) ipep);
listenSocket.BeginReceive(clientData, 0, clientData.Length,
SocketFlags.None, new AsyncCallback(OnReceiveClient), null);
我在最后一行发生错误,socket无法接收TCP协议中的数据。 这段代码在UDP协议中工作得很好。 你能帮我吗?! (感谢)
答
代码应该是这样的
IPAddress IP = (my IP Address);
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IP, 1370);
listenSocket.Bind((EndPoint) ipep);
listenSocket.Listen(4);
// Create the call back for any client connections...
listenSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
,一旦客户端连接
// This is the call back function, which will be invoked when a client is connected
public void OnClientConnect(IAsyncResult asyn)
{
try
{
Socket workerSocket = m_mainSocket.EndAccept (asyn);
workerSocket.BeginReceive();
// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect),null);
}
catch(ObjectDisposedException)
{
}
catch(SocketException se)
{
}
}
+0
非常感谢你......这段代码工作得很好。谢谢。 – user1518295 2012-08-03 09:49:19
答
长话短说,TCP/IP协议有连接建立阶段。所以服务器必须调用bind()
,listen()
和accept()
,客户端必须调用connect()
。连接建立后,服务器accept()
返回一个新客户端服务器套接字。该套接字允许服务器与客户端进行通信(即服务于连接)。
我想向您推荐下面的例子:
答
试试这个代码
public void Listen()
{
string portStr = "5656";
int port = System.Convert.ToInt32(portStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
// Start listening...
m_mainSocket.Listen(4);
btn_start.Enabled = false;
lbl_connect.Visible = true;
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
public void OnClientConnect(IAsyncResult asyn)
{
m_workerSocket[m_clientCount] = m_mainSocket.EndAccept(asyn);
// Let the worker Socket do the further processing for the
// just connected client
WaitForData(m_workerSocket[m_clientCount]);
// Now increment the client count
++m_clientCount;
// Display this client connection as a status message on the GUI
// Since the main Socket is now free, it can go back and wait for
// other clients who are attempting to connect
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
// **等待客户数据
public void WaitForData(System.Net.Sockets.Socket soc)
{
try
{
if (pfnWorkerCallBack == null)
{
// Specify the call back function which is to be
// invoked when there is any write activity by the
// connected client
pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket theSocPkt = new SocketPacket();
theSocPkt.m_currentSocket = soc;
// Start receiving any data written by the connected client
// asynchronously
soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length,
SocketFlags.None,
pfnWorkerCallBack,
theSocPkt);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
“我有一个错误” 是很模糊的 - 你可以提供更多的细节? – 2012-08-03 09:19:16
侦听套接字通常用于接受连接,而不是接收数据。 UDP的交互与TCP的交互不同。你可能想看看这样的东西:http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx – forsvarir 2012-08-03 09:27:58
乔恩,错误是:“一个请求发送或接收数据被禁止,因为套接字未连接,并且(当使用sendto调用在数据报套接字上发送时)没有提供地址“ – user1518295 2012-08-03 09:28:58