如何用winsock编写接收多客户发送数据(较短)的接收程序?(100分)

  • 主题发起人 主题发起人 yanyuwuhen
  • 开始时间 开始时间
Y

yanyuwuhen

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟刚开始winsock编程(呵呵,以前用控件),我处理接收时不响应,recv(fd,BufRecv^,1024,0)前应做
哪协处理,用开多线程么(如果处理速度够快)?(希望有源码能让我学学,谢谢)


 
如果用消息做,应该怎样写?数据到达是的消息是什么?
 
不必多线程

多启几个SOCKET就可以了



 
msdn中说:接发UDP时,bind后,recvfrom(sendto)就可以了,是么?不用listen,accept么?
还有:Re:=recvfrom(fd,BufRecv^,1024,0,addrS,Pinteger(sizeof(addrS)));错在哪里?
addrS:sockaddr_in;
[Error]Types of actual and formal var parameters must be identical
我不知道addrS应如何设,还是接收时自动获得?
 
addrS
应该在CONNECT前设置啊
 
一个C语言UDP程序

#include /* for printf(), fprintf() */
#include /* for socket(),... */
#include /* for exit() */

#define ECHOMAX 255 /* Longest string to echo */

void DieWithError(char *errorMessage); /* External error handling function */

void main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
struct sockaddr_in fromAddr; /* Source address of echo */
unsigned short echoServPort; /* Echo server port */
unsigned int fromSize; /* In-out of address size for recvfrom() */
char *servIP; /* IP address of server */
char *echoString; /* String to send to echo server */
char echoBuffer[ECHOMAX]; /* Buffer for echo string */
int echoStringLen; /* Length of string to echo */
int respStringLen; /* Length of response string */
WORD wVersionRequested; /* Version of Winsock to load */
WSADATA wsaData; /* Winsock implementation details */

if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr,"Usage: %s []/n", argv[0]);
exit(1);
}

servIP = argv[1]; /* first arg: server IP address (dotted quad)*/
echoString = argv[2]; /* second arg: string to echo */

if ((echoStringLen = strlen(echoString) + 1) > ECHOMAX) /* Check input length */
DieWithError("Echo word too long");

if (argc == 4)
echoServPort = atoi(argv[3]); /* Use given port, if any */
else
echoServPort = 7; /* otherwise, 7 is the well-known port for the echo service */

wVersionRequested = MAKEWORD(2, 0); /* Request Winsock v2.0 */
if (WSAStartup(wVersionRequested, &wsaData) != 0) /* Load Winsock DLL */
{
fprintf(stderr,"WSAStartup() failed");
exit(1);
}

/* Create a best-effort datagram socket using UDP */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");

/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */

/* Send the string, including the null terminator, to the server */
if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr)) != echoStringLen)
DieWithError("sendto() sent a different number of bytes than expected");

/* Recv a response */

fromSize = sizeof(fromAddr);
if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &fromAddr,
&fromSize)) != echoStringLen)
DieWithError("recvfrom() failed");

if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
{
fprintf(stderr,"Error: received a packet from unknown source./n");
exit(1);
}

if (echoBuffer[respStringLen-1]) /* Do not printf unless it is terminated */
printf("Received an unterminated string/n");
else
printf("Received: %s/n", echoBuffer); /* Print the echoed arg */

closesocket(sock);
WSACleanup(); /* Cleanup Winsock */

exit(0);
}

void DieWithError(char *errorMessage)
{
fprintf(stderr,"%s: %d/n", errorMessage, WSAGetLastError());
exit(1);
}

 
to pbuild:
addrS我是在前边设置的,这里我只是说一下数据类型;但我没用connect,udp接收流程中
不用它。
谢谢你的源码。我的也是按这个方法(流程写的),但是我遇到一个参数不匹配问题,因
为是delphi中的,没法用相应的结构!
请看
c语言中:recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &fromAddr,
&fromSize))
我写的(delphi中)Re:=recvfrom(fd,BufRecv^,1024,0,addrS,Pinteger(sizeof(addrS)))
提示错误:[Error]Types of actual and formal var parameters must be identical
我不知该如何改!应该是addrS错了,但我已按要求定义了??
 
recvfrom我改过来了。
 
用 winsock2单元吧,比DELPHI里的socket好多了。。。。。。
 
结束吧。我想知道的还是没有知道!
 
多人接受答案了。
 
先看你的源码,tgmngy@sohu.com
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部