error C2664: "sendto": 不能将参数 2 从"CString"转换为"const char *"

今天在利用多线程和网络按照孙鑫视频上的来编写的时候,因为不是在vc6.0平台,而是在VS2010平台下,然后就出现了各种问题。error C2664: "sendto": 不能将参数 2 从"CString"转换为"const char *"便是其中的一个,找了许久,终于找到了解决方案,现在把那句拿出来分享一下。

原句:sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR *)&addrTo,sizeof(SOCKADDR));

改句:sendto(m_socket,(char*)(LPCTSTR)strSend,strSend.GetLength()+1,0,(SOCKADDR *)&addrTo,sizeof(SOCKADDR));

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是将代码转化为C++的结果: ```cpp #include <iostream> #include <cstring> #include <cstdlib> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> void scan_ports(const char* host, int start_port, int end_port, const char* protocol) { struct sockaddr_in target; int result, sock, port; memset(&target, 0, sizeof(target)); target.sin_family = AF_INET; target.sin_addr.s_addr = inet_addr(host); for(port = start_port; port <= end_port; port++) { try { if(strcmp(protocol, "tcp") == 0) { sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock < 0) { std::cerr << "Failed to create socket.\n"; exit(1); } target.sin_port = htons(port); struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); result = connect(sock, (struct sockaddr*)&target, sizeof(target)); if(result == 0) { std::cout << "Port " << port << " is open.\n"; } close(sock); } else if(strcmp(protocol, "udp") == 0) { sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if(sock < 0) { std::cerr << "Failed to create socket.\n"; exit(1); } target.sin_port = htons(port); struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); char buffer[] = "Ping"; result = sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&target, sizeof(target)); if(result < 0) { std::cerr << "Failed to send data.\n"; exit(1); } char recv_buffer[1024]; socklen_t recv_len = sizeof(target); result = recvfrom(sock, recv_buffer, sizeof(recv_buffer), 0, (struct sockaddr*)&target, &recv_len); if(result > 0) { std::cout << "Port " << port << " is open.\n"; } close(sock); } else { std::cerr << "Unsupported protocol " << protocol << "\n"; exit(1); } } catch(...) { continue; } } } int main(int argc, char** argv) { const char* host = "127.0.0.1"; int start_port = 1; int end_port = 65535; const char* protocols[] = {"tcp", "udp"}; int i, num_protocols = sizeof(protocols) / sizeof(protocols[0]); for(i = 0; i < num_protocols; i++) { std::cout << "Scanning " << protocols[i] << " ports...\n"; scan_ports(host, start_port, end_port, protocols[i]); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值