在使用WIN32 API的程序上工作,我希望程序从收集系统中某些信息的函数中获取数据,并将参数作为char传递给DbgPrint函数,我希望将该char发送到该ip和port如何获取工作完成,而函数args没有任何错误
我的代码就像:
#include <winsock2.h>
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
void socketfnt (auto *ipaddr, auto *portn, char*msg){
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr(ipaddr);
server.sin_family = AF_INET;
server.sin_port = htons( portn );
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}
puts("Connected");
message = msg;
if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("Data Send\n");
return 0;
}
void DbgPrint(char *msg){
socketfnt("192.168.1.6","4444", msg);
}
编译器显示
之类的错误fff.c:6:23: error: storage class specified for parameter 'ipaddr'
fff.c:6:37: warning: type defaults to 'int' in declaration of 'portn' [-
Wimplicit-int]
void socketfnt (auto *ipaddr, auto *portn, char*msg){
你们能告诉我如何以正确的方式将参数传递给 socketfnt 函数吗 ,问候
答案 0 :(得分:0)
您不能使用auto
作为非lambda函数的参数。您需要使用适当的数据类型。
您也在泄漏资源。
尝试以下方法:
#include <winsock2.h>
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
void socketfnt (const char *ipaddr, uint16_t portn, const char *msg)
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
puts("\nInitializing Winsock...");
int ret = WSAStartup(MAKEWORD(2,2), &wsa);
if (ret != 0)
{
printf("Failed. Error Code : %d\n", ret);
return 1;
}
puts("Initialized.\n");
//Create a socket
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Could not create socket : %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
puts(Socket created.\n");
memset(&server, 0, sizeof(server));
server.sin_addr.s_addr = inet_addr(ipaddr);
server.sin_family = AF_INET;
server.sin_port = htons(portn);
if (server.sin_addr.s_addr == INADDR_NONE)
{
puts("Invalid IP address.\n");
closesocket(s);
WSACleanup();
return 1;
}
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Could not connect socket : %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return 1;
}
puts("Connected\n");
int size = strlen(msg);
while (size > 0)
{
ret = send(s, msg, size, 0);
if (ret < 0)
{
printf("Could not send data : %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return 1;
}
msg += ret;
size -= ret;
}
puts("Data Send\n");
closesocket(s);
WSACleanup();
return 0;
}
void DbgPrint(const char *msg)
{
socketfnt("192.168.1.6", 4444, msg);
}