下面是我书写的代码 求大神们帮忙看一下哪里有问题 服务器
#include<iostream>
#include<WinSock2.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
SOCKET s1=socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in addr1;//sever
sockaddr_in addr2;//client
//sever 短板
addr1.sin_family=AF_INET;
addr1.sin_addr.S_un.S_addr=INADDR_ANY;
addr1.sin_port=htons(1234);
//client
addr2.sin_family=AF_INET;
addr2.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addr2.sin_port=htons(1234);
//________________________________
if (INVALID_SOCKET == s1)
{
cout << "socket failed!" << endl;
WSACleanup();
return -1;
}
else
{
cout << "Server UDP Socket init!" << endl;
}
if (SOCKET_ERROR == bind(s1,(const sockaddr *)&addr1,sizeof(addr1)))
{
cout << "bind failed!" << endl;
closesocket(s1);
WSACleanup();
return -1;
}
else
{
cout << "Server UDP Socket bind IP & Port !" << endl;
}
char szbuf[1234]={0};
char sztext[]="hello";
int n=sizeof(sockaddr_in);
while(1)
{
int nRecv=recvfrom(s1,szbuf,sizeof(szbuf),0,(sockaddr*)&addr2,&n);
if (SOCKET_ERROR == nRecv)
{
cout << "Recv Failed!" << endl;
closesocket(s1);
WSACleanup();
Sleep(5000);
return -1;
}
else
{
cout<<szbuf<<endl;
}
sendto(s1,sztext,sizeof(sztext),0,(const sockaddr*)&addr2,n);
}
closesocket(s1);
WSACleanup();
return 0;
}
下面是客户端 #include #include<winsock2.h> using namespace std; #pragma comment(lib, "ws2_32.lib") int main() { WORD wVersionRequested; WSADATA wsaData; int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
/* Confirm that the WinSock DLL supports 2.2./ / Note that if the DLL supports versions greater / / than 2.2 in addition to 2.2, it will still return / / 2.2 in wVersion since that is the version we / / requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
SOCKET s1=socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in addr1;//sever
sockaddr_in addr2;//client
char szbuf[]="hello";
int nSize=sizeof(sockaddr);
//sever 短板
addr1.sin_family=AF_INET;
addr1.sin_addr.S_un.S_addr=INADDR_ANY;
addr1.sin_port=htons(1234);
//client
addr2.sin_family=AF_INET;
addr2.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addr2.sin_port=htons(1234);
//________________________________
if (INVALID_SOCKET == s1)
{
cout << "socket failed!" << endl;
WSACleanup();
return -1;
}
else
{
cout << "client UDP Socket init!" << endl;
}
if (SOCKET_ERROR == bind(s1,(const sockaddr *)&addr2,sizeof(addr2)))
{
cout << "bind failed!" << endl;
closesocket(s1);
WSACleanup();
return -1;
}
else
{
cout << "client UDP Socket bind IP & Port !" << endl;
}
char sztext[1000]={0};
while(1)
{
sendto(s1,szbuf,sizeof(szbuf),0,(const sockaddr*)&addr2,nSize);
int nRecv=recvfrom(s1,sztext,sizeof(sztext),0,(sockaddr*)&addr2,&nSize);
if (SOCKET_ERROR == nRecv)
{
cout << "Recv Failed!" << endl;
closesocket(s1);
WSACleanup();
Sleep(5000);
return -1;
}
}
closesocket(s1);
WSACleanup();
return 0;
}
检查监听的端口。