gpt4 book ai didi

c - _popen 崩溃[Windows - C]

转载 作者:行者123 更新时间:2023-11-30 15:44:37 24 4
gpt4 key购买 nike

我是 C 语言的新手(实际上是所有编译语言),下面你将看到我可怕的代码,我尝试接收命令,然后我希望执行它(尚未实现)。

我从 Microsoft 获取了 _popen 函数的示例代码并对其进行了修改(非常轻微),但由于某种原因,这导致我的程序在收到命令后崩溃。如果我将这段代码移到命令接收器部分上方,它就可以正常工作。我是否覆盖了另一个内存地址或其他什么?

如果您需要服务器(用 python 编写),请告诉我。希望您能帮助有需要的小白。

#include <winsock2.h>
#include <stdio.h>
#include <windows.h>

int sendall(int sock, char *message, unsigned int size);
int connect(char address[], short port);
char* recvall(int sock, unsigned int *msgsize);


int main(){
char address[] = "127.0.0.1";
short port = 1234;
int wait = 5000;

//Connect back
int sock = 1;
printf("Connecting...\n");
sock = connect(address, port);
while(sock == 1){
WSACleanup();
Sleep(wait);
sock = connect(address, port);
}

printf("Succesfully conneted\n");

char test[] = "ABCDEFGH";
sendall(sock,test, 8);

unsigned int msgsize;
char *msg = recvall(sock,&msgsize);


printf("%s\n", msg);
printf("%d\n", msgsize);


//RUN COMMAND

char psBuffer[128];
FILE *pPipe;


printf("There\n");

if( (pPipe = _popen( "ipconfig", "rt" )) == NULL )
exit( 1 );

printf("Out\n");

while(fgets(psBuffer, 128, pPipe)){
printf(psBuffer);
}


if (feof( pPipe)){
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}else{
printf( "Error: Failed to read the pipe to the end.\n");
}

//RUN COMMAND END

Sleep(10000);
return 0;
}

int sendall(int sock, char *message, unsigned int size){

//Send size packet
char sizepacket[12];
sprintf (sizepacket, "%012u",size);
send(sock, sizepacket, 12, 0);

//Send data
unsigned int left = size;
unsigned int offset = 0;
while(left>0){
if(left<1024){
send(sock, (message+offset), left, 0);
left = 0;
}else{
send(sock, (message+offset), 1024, 0);
offset = offset + 1024;
left = left - 1024;
}
}
return 0;
}

char* recvall(int sock, unsigned int *size){
char *message;

//Get size packet
char sizebuff[24];
memset (&sizebuff,0,24);
recv(sock, sizebuff, 12, 0);
*size = atoi(sizebuff);

//Alloc space for message
message = (char*) malloc(*size + 1);
memset(message,0,1024);

//Get data
unsigned int left = *size;
unsigned int offset = 0;
while(left>0){
if(left<1024){
recv(sock, (message+offset), left, 0);
left = 0;
}else{
recv(sock, (message+offset), 1024, 0);
offset = offset + 1024;
left = left - 1024;
}
}
return message;
}



int connect(char address[], short port){
int returnvalue;

WSADATA wsaData;

returnvalue = WSAStartup(MAKEWORD(2,2), &wsaData);
if(returnvalue != 0){
return 1;
}

int sock;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == 0){
return 1;
}


//Create struct
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(address);
clientService.sin_port = htons(port);


returnvalue = connect(sock, (SOCKADDR*) &clientService, sizeof(clientService));
if (returnvalue != 0){
closesocket(sock);
WSACleanup();
return 1;
}

return sock;
}

最佳答案

您的 popen 似乎并不依赖于您的套接字通信。

请描述您的服务器发送的消息(十六进制转储?)

请查看您的recvall() 函数。您从套接字读取 12 个字节,将其转换为 int,然后 malloc 那么多字节,将其存储到 *size 中,然后将消息缓冲区(仅 malloc'ed)memset 为大小 1024。然后,您将消息接收到缓冲区但不放置防护装置以确保不会超出消息的末尾[*大小]。

int main(){
//...
unsigned int msgsize;
char *msg = recvall(sock,&msgsize);
printf("%d\n", msgsize);
printf("%s\n", msg);
return 0;
}

char* recvall(int sock, unsigned int *size){
char *message;
//Get size packet
char sizebuff[24];
memset (&sizebuff,0,24);
recv(sock, sizebuff, 12, 0);
*size = atoi(sizebuff);
printf("size: %d\n",*size);fflush(stdout);
//Alloc space for message
message = (char*) malloc(*size + 1);
memset(message,0,1024); //why 1024? why not *size?
//Get data
unsigned int left = *size;
unsigned int offset = 0;
while(left>0){
if(left<1024){
recv(sock, (message+offset), left, 0);
left = 0;
}else{
recv(sock, (message+offset), 1024, 0); //why 1024?
offset = offset + 1024; //again, why 1024?, this is > 1024
left = left - 1024;
}
}
return message;
}

关于c - _popen 崩溃[Windows - C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com