?? winsockdnsclient.cpp
字號:
pPort = pArgv[3]; // Get port.
if (0==strcmp(pPort, ".")) // Dot given?
pPort = dfltPort; // Point to default port.
}
else
pPort = dfltPort; // Get port.
port = atoi(pPort); // Convert ascii to integer.
printf(" Using IP address = %s, port = %d\n", pIPAddr, port);
// Initialize Winsocket.
if (0!=WSAStartup(MAKEWORD(2, 0), &WSAData)) // Accept up to Winsock 2.0.
{
printf(JAProgNm ": WSAStartup failed. Error = %d\n", WSAGetLastError());
return 1;
}
// Create a UDP/IP socket that is bound to the server.
if (INVALID_SOCKET==(ServerSock = socket(AF_INET, SOCK_DGRAM, 0)))
{
printf(JAProgNm ": Allocating socket failed. Error = %d\n", WSAGetLastError());
return 1;
}
// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;
// Retrieve the host information corresponding to the host name.
if (NULL==(pHostEnt = gethostbyname(pIPAddr)))
{
printf(JAProgNm ": Unable to get the host name. Error = %d\n", WSAGetLastError ());
closesocket (ServerSock);
return 1;
}
// Assign the socket IP address.
memcpy((char FAR *)&(destination_sin.sin_addr),
pHostEnt->h_addr,
pHostEnt->h_length
);
// Convert to network ordering.
destination_sin.sin_port = htons(port);
// Establish a connection to the server socket.
if (SOCKET_ERROR==connect(ServerSock,
(PSOCKADDR)&destination_sin,
sizeof(destination_sin)
)
)
{
ULONG error = WSAGetLastError();
printf(JAProgNm ": Connecting to the server failed. Error = %d%s\n", error, WSAECONNREFUSED==error ? " (connection refused)" : "");
closesocket(ServerSock);
return 1;
}
// Send data to the server.
#define UDPBfrSz 128
sendLn = UDPBfrSz;
pBuffer = (char *)malloc(sendLn); // Get a buffer.
if (NULL==pBuffer) // No good?
{
printf(JAProgNm ": Couldn't allocate buffer, quitting\n");
goto CloseSocket;
}
pQryData = (pMyDNSQuery)pBuffer; // Point to query.
memset(pQryData, 0, UDPBfrSz); // Zero area.
pQryData->QueryId = GetNetwkPort(2); // Set query id (nominal).
pQryData->RecursDesired = TRUE; // Show recursion OK.
pQryData->QuestionCount = GetNetwkPort(1); // Set number of queries.
pQuestData = ((char *)pQryData) + // Point to question area.
FIELD_OFFSET(myDNSQuery, Question);
*(PUCHAR)pQuestData = 3; // Set length of first piece.
memcpy(pQuestData+1, "www", strlen("www"));
*(PUCHAR)(pQuestData+4) = 3; // Set length of second piece.
memcpy(pQuestData+5, "ibm", strlen("ibm"));
*(PUCHAR)(pQuestData+8) = 3; // Set length of third piece.
memcpy(pQuestData+9, "com", strlen("com"));
pShort = (PUSHORT)((PUCHAR)pQuestData + 13); // Point past question area.
*pShort = GetNetwkPort(1); // Set QNAME.
*(pShort+1) = GetNetwkPort(1); // Set QTYPE.
sendLn = FIELD_OFFSET(myDNSQuery, Question) + // Get total length.
strlen(pQuestData) + 1 +
2 +
2;
printf(JAProgNm ": Sending %d bytes\n", sendLn);
lclStatus = send(ServerSock, // Send datagram.
pBuffer,
sendLn,
0
);
if (SOCKET_ERROR==lclStatus)
{
printf(JAProgNm ": Sending data to the server failed. Error = %d\n", WSAGetLastError());
goto CloseSocket;
}
pRspData = (pMyDNSQuery)szClientA; // Point to response area.
// Receive data from the server socket.
rcRecv = recv(ServerSock, (char *)pRspData, sizeof(szClientA), 0);
// Check if there is any data received. If there is, display it.
if (SOCKET_ERROR==rcRecv)
{
printf(JAProgNm ": No data is received, recv failed. Error = %d\n", WSAGetLastError());
}
else if (0==rcRecv)
{
printf(JAProgNm ": Finished receiving data\n");
}
else
{
printf("\n\n" JAProgNm ": Total received from server = %d\n", rcRecv);
UCHAR FullName[64];
USHORT NameOffset = // Get name offset, including 2 high-order bits.
GetNetwkPort(* (PUSHORT) (((PUCHAR)pRspData)+sendLn) );
NameOffset &= ~0xc000; // Turn off high-order 2 bits.
PUCHAR pNameFd = // Point to name.
((PUCHAR)pQryData) + FIELD_OFFSET(myDNSQuery, QueryId) + NameOffset,
pPartName;
pRRData = // Point to RR field.
(pMyDNSRR)(((PUCHAR)pRspData) + sendLn + 2);
USHORT nbrAnswers = // Get number of RR fields.
GetNetwkPort(pRspData->AnswerCount);
for ( // Go through RR fields.
ULONG i = 0;
i < nbrAnswers;
i ++
)
{
FullName[0] = 0;
pPartName = pNameFd;
do
{
pPartName = // Build name, piece by piece.
CopyNextStanza(pPartName, FullName);
}
while(NULL!=pPartName);
FullName[strlen((const char *)FullName)-1] = 0;// Replace final '.' with null terminator.
char IPAddr[16],
Octet1[4],
Octet2[4],
Octet3[4],
Octet4[4];
ULONG addr = // Get IP address, converting it from network form.
GetNetwkAddrDw(*(PULONG)pRRData->RRData);
_ultoa(addr>>24, Octet1, 10);
_ultoa((0x00ff0000&addr)>>16, Octet2, 10);
_ultoa((0x0000ff00&addr)>>8, Octet3, 10);
_ultoa(0x000000ff&addr, Octet4, 10);
strcpy(IPAddr, Octet1);
strcat(IPAddr, ".");
strcat(IPAddr, Octet2);
strcat(IPAddr, ".");
strcat(IPAddr, Octet3);
strcat(IPAddr, ".");
strcat(IPAddr, Octet4);
printf("Name = %s, IP addr = %s\n", FullName, IPAddr);
pRRData = // Point to next RR field.
(pMyDNSRR) ( ((PUCHAR)pRRData) + FIELD_OFFSET(myDNSRR, RRData) +
4 +
2
);
}
}
CloseSocket:
// Close the socket.
closesocket(ServerSock);
WSACleanup();
if (NULL!=pBuffer)
free(pBuffer); // Free buffer.
ExitPoint:
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -