即時通訊只是開始瞭解套接字,我已經給了這個代碼,我必須使端口查找邏輯工作。但問題是我不斷收到這個運行時錯誤,我不知道爲什麼?爲什麼在strcpy_s之後得到Debug斷言失敗?
// portlookup.cpp
// Given a service name, this program displays the corresponding port number.
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#include <winsock2.h>
using namespace std;
int main (int argc, char **argv)
{
char service[80]; // This string contains name of desired service
struct servent *pse; // pointer to service information entry
short port; // Port # (in Network Byte Order) of desired service
if (argc < 2)
{
cout << "Please specify a service." << endl;
}
strcpy_s(service, sizeof(service), argv[1]);
WORD wVersion = 0x0202;
WSADATA wsaData;
int iResult = WSAStartup(wVersion, &wsaData); // Returns zero if successful
if (iResult != 0) {
cout << "Insufficient resources to startup WINSOCK." << endl;
return 0;
}
port = htons((u_short) atoi(service)); // 1st try to convert string to integer
if (port == 0) { // if that doesn't work, call service function
pse = getservbyname(service,NULL);
if (pse) {
port = pse->s_port;
}
else
{
cout << "Invalid service request." << endl;
return INVALID_SOCKET;
}
}
cout << "Service: " << service << endl;
cout << "Port: " << htons(port) << endl;
}
什麼運行時錯誤? – GManNickG 2010-02-06 21:09:00
第一次使用strcpy之後,它表示Debug斷言失敗 – Zerobu 2010-02-06 21:11:14
應該是strncpy嗎? – Anonymous 2010-02-06 21:13:54