UDP的listen函数问题

Free_Man 2002-06-05 03:53:42
我的代码如下:
void CNet_TestDlg::OnStartUdpServer()
{
SOCKET sk_Server , sk_Rcv;
sockaddr_in sk_addr;
int n_SockErr;
CString str_Out;

sk_Server = socket( AF_INET , SOCK_DGRAM , IPPROTO_UDP );
//sk_Server = socket( AF_INET , SOCK_STREAM , 0 );
sk_addr.sin_family = AF_INET;
sk_addr.sin_port = htons( 5050 );
sk_addr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); //htonl( INADDR_ANY );
if( bind( sk_Server , (LPSOCKADDR) &sk_addr , sizeof( sk_addr )) == SOCKET_ERROR ){
n_SockErr = WSAGetLastError();
str_Out = "";
str_Out.Format( "%s%d" , str_Out , n_SockErr );
MessageBox( "错误代码:" + str_Out );
MessageBox( "绑定地址出错!" );
closesocket( sk_Server );
return;
}
if( listen( sk_Server , 2 ) == SOCKET_ERROR ){
n_SockErr = WSAGetLastError();
str_Out = "";
str_Out.Format( "%s%d" , str_Out , n_SockErr );
MessageBox( "错误代码:" + str_Out );
MessageBox( "监听数据包出错!" );
closesocket( sk_Server );
return;
}
MessageBox( "OK,已经成功启动UDP服务器!" );
}
问题:
为何总是在每次运行listen函数时,不能正确返回.
导致"监听数据包出错!"
...全文
891 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yokishiro 2002-06-05
  • 打赏
  • 举报
回复
完全同意archoo的说法
xuying 2002-06-05
  • 打赏
  • 举报
回复
同意 archoo(archoo), UDP不需要listen。
alphapiao 2002-06-05
  • 打赏
  • 举报
回复
同意 archoo(archoo)的看法
archoo 2002-06-05
  • 打赏
  • 举报
回复
1)MSDN中关于listen()函数的解释:

To accept connections, a socket is first created with the socket function and bound to a local address with the bind function, a backlog for incoming connections is specified with listen, and then the connections are accepted with the accept function. /****注意*/Sockets that are connection oriented, those of type SOCK_STREAM for example, are used with listen. /*注意*/The socket s is put into "passive'' mode where incoming connection requests are acknowledged and queued pending acceptance by the process.

2)肯定已经调用WSAStartup,要不bind()怎不出错呢?
archoo 2002-06-05
  • 打赏
  • 举报
回复

1)如果你是用的UDP的协议,不能调用listen
面向联接的才需要调用listen
2)我有一个封好的UDP类,如果需要,告诉我你的email

Free_Man 2002-06-05
  • 打赏
  • 举报
回复
Have you called WSAStartup? Yes ,I have Finished it.
masterz 2002-06-05
  • 打赏
  • 举报
回复
Have you called WSAStartup? Here is an example using udp.
//http://www.district86.k12.il.us/central/activities/computerclub/Tutorials/Winsock/Lesson5.htm
// file name: wsudprcv.c
//
// description: A UDP packet receiver that waits for
// UDP packets on a specific port

#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib,"ws2_32")
int main(int argc, int **argv)
{
WSADATA wsda; // Structure to store info
// returned from WSAStartup

char szMessage[512];
int iMessageLen;
int ret;

char szAddress[64];
int iPort;

SOCKET s; // Our TCP socket handle
SOCKADDR_IN addr, // The local interface address
remote_addr; // The sender's address
int iRemoteAddrLen; // Contains the length of remte_addr, passed to recvfrom
/*
// Check arguments
if(argc != 2 ||
(argc==2 && strcmp((char *) &argv[1][0], "/?")==0))
{
printf("wsudprcv port\n");
printf(" port: the port the server should listen to\n");
exit(1);
}

// Get the remote port
iPort = atoi((char *) &argv[1][0]);

if(iPort<0 || iPort>65563)
{
printf("Invalid port number! (%s)\n", argv[2]);
exit(1);
}
*/
iPort = 2020;
iMessageLen = 512; // Set to the length of szMessage buffer

// Load version 1.1 of Winsock

WSAStartup(MAKEWORD(1,1), &wsda);

// Create an UDP socket
printf("Creating socket...");
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

// Error?
if(s == SOCKET_ERROR)
{
printf("Error\nCall to socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); failed with:\n%d\n", WSAGetLastError());
exit(1);
}

printf("OK\n");

// Fill in the interface information
printf("Binding socket...");
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
addr.sin_addr.s_addr = INADDR_ANY;

if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR)
{
printf("Error\nCall to bind(s, (struct sockaddr *) &addr, sizeof(addr)); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("OK\n");

// Ready to receive data

printf("Waiting for packets (Press Ctrl-C to exit)...");

iRemoteAddrLen = sizeof(remote_addr);
ret = recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen);

if(ret == SOCKET_ERROR)
{
printf("Error\nCall to recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("Packet received\n");

iMessageLen = ret; // Length of the data received

szMessage[iMessageLen] = '\0'; // Convert to cstring

printf("\"%s\" received from %s\n", szMessage, inet_ntoa(remote_addr.sin_addr));

closesocket(s);

WSACleanup();
getch();
return 0;
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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