关于winsocket的一个问题请高手帮忙看代码!!!!!!!!!!!!!!!!! ( 积分: 50 )

  • 主题发起人 主题发起人 BeCalm
  • 开始时间 开始时间
B

BeCalm

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,我有以下VC中的UDP,Socket代码,运行后两者不能通信,收不到信息,
不知道是什么原因,请高手指点!诚送50分!!!

//主机服务器端代码:-------------------------------------------------------
////////////////////////////////////////////
//基于UDP连接的服务器端SOCKET程序//
////////////////////////////////////////////

#include <stdio.h>
#include <WINSOCK2.H>
//#include <windows.h>
//#include <iostream.h>
//#include <string.h>
#pragma comment(lib, &quot;Ws2_32.lib&quot;)

void main()
{
//初始准备
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );//构造WORD结构的版号,makeword(hibyte,lobyte);

err = WSAStartup( wVersionRequested, &amp;wsaData );
if ( err != 0 ) {
printf(&quot;Can't load the Winsock dll!/n&quot;);
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
printf(&quot;not the right sock dll version!/n&quot;);
WSACleanup();
return;
}
//建立套接字
SOCKET sktSvr=socket(AF_INET,SOCK_DGRAM,0);
//设定主机地址结构
SOCKADDR_IN addrSvr;
addrSvr.sin_addr.S_un.S_addr=inet_addr(&quot;127.0.0.1&quot;);//htonl(ADDR_ANY);
addrSvr.sin_family=AF_INET;
addrSvr.sin_port=htons(6000);
//绑定本机套接字,本地地址,地址长度;
bind(sktSvr,(SOCKADDR*)&amp;addrSvr,sizeof(SOCKADDR));

SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);

char receiveBuffer[100]=&quot;&quot;;
char sendBuffer[100]=&quot;&quot;;
char tempBuffer[100]=&quot;&quot;;

while(1)
{
recvfrom(sktSvr,receiveBuffer,100,0,(SOCKADDR*)&amp;addrClient,&amp;len);
if ('q'==receiveBuffer[0])
{
sendto(sktSvr,&quot;q&quot;,strlen(&quot;q&quot;)+1,0,(SOCKADDR*)&amp;addrClient,len);
printf(&quot;chat end/n&quot;);
break;
}
sprintf(tempBuffer,&quot;%s say: %s/n&quot;,inet_ntoa(addrClient.sin_addr),receiveBuffer);
printf(&quot;%s/n&quot;,tempBuffer);
printf(&quot;please input data:/n&quot;);
gets(sendBuffer);
sendto(sktSvr,sendBuffer,strlen(sendBuffer)+1,0,(SOCKADDR*)&amp;addrClient,len);
}

closesocket(sktSvr);
WSACleanup();

}//--------------------------------------------------------------------------
//客户端代码如下:----------------------------------------------------------
////////////////////////////////////////////
//基于UDP连接的客户端SOCKET程序//
////////////////////////////////////////////

#include <stdio.h>
#include <WINSOCK2.H>
#include <iostream.h>
#pragma comment(lib, &quot;Ws2_32.lib&quot;)

void main()
{
//初始准备
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 1, 1 );//构造WORD结构的版号,makeword(hibyte,lobyte);

err = WSAStartup( wVersionRequested, &amp;wsaData );
if ( err != 0 ) {
cout<<&quot;Can't load the Winsock dll!&quot;<<endl;
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
cout<<&quot;not the right sock dll version!&quot;<<endl;
WSACleanup();
return;
}
//建立套接字
SOCKET sktClient=socket(AF_INET,SOCK_DGRAM,0);
//设定主机地址结构
SOCKADDR_IN addrSvr;
addrSvr.sin_addr.S_un.S_addr=inet_addr(&quot;127.0.0.1&quot;);
addrSvr.sin_family=AF_INET;
addrSvr.sin_port=htons(6000);

// SOCKADDR_IN addrSr;
int len=sizeof(SOCKADDR);

char receiveBuffer[100]=&quot;&quot;;
char sendBuffer[100]=&quot;&quot;;
char tempBuffer[100]=&quot;&quot;;

while(1)
{
printf(&quot;please input data:/n&quot;);
gets(sendBuffer);
sendto(sktClient,sendBuffer,strlen(sendBuffer)+1,0,(SOCKADDR*)&amp;addrSvr,len);
recvfrom(sktClient,receiveBuffer,100,0,(SOCKADDR*)&amp;addrSvr,&amp;len);
if ('q'==receiveBuffer[0])
{
sendto(sktClient,&quot;q&quot;,strlen(&quot;q&quot;)+1,0,(SOCKADDR*)&amp;addrSvr,len);
printf(&quot;chat is end!/n&quot;);
break;
}
sprintf(tempBuffer,&quot;%s says: %s&quot;,inet_ntoa(addrSvr.sin_addr),receiveBuffer);
printf(&quot;%s/n&quot;,tempBuffer);

}

closesocket(sktClient);
WSACleanup();
}
//--------------------------------------------------------------------------
 
后退
顶部