我有一个特定的设备(IP:192.168.0.10端口3000 - 它是一个信号接收器),必须通过UDP控制。我应该能够发送这个设备基本命令,例如'reset'; 'log'; '跑'......
经过一番努力和stackoverflow的温和帮助后,我设法使用netcat与我的设备进行通信。它现在完美无缺。更改计算机eth IP后,我使用了命令 nc -u 192.168.0.10 3000 。 (我在linux下)。
现在,这就是目标,我需要和netcat一样,但是,使用C语言。由于我是初学者,我尝试使用互联网并写下:
/* Standard Linux headers */
#include <stdio.h> //printf
#include <string.h> //memset
#include <stdlib.h> //exit(0);
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
/* DEFINE */
#define SERVER "192.168.0.10"
#define BUFFLEN 1024 //Max length of buffer
#define PORT 3000 //The port on which to send data
int main (void)
{
/*
** Definition of the local variables.
*/
struct sockaddr_in serv_addr; /* Server Socket address structure*/
/* short int status = 0; /* internal status return*/
/* unsigned short int return_status = 0; /* the returnes status value */
char recepbuff[BUFFLEN];
char cmd[BUFFLEN];
int socket_fd;
/*
** Initialisation of the local variables.
*/
memset(recepbuff, '0' ,sizeof(recepbuff)); /*pareil que bzero*/
/*
** Creating the socket
** call socket (it creates an endpoint for communication and
** returns the socket descriptor.
** To create an UDP socket here are the param
** The protocol family should be AF_INET
** The protocol type is SOCK_DGRAM
** The protocol should beset to default ie
** DEF_PROTOCOL wich is default so 0. );
*/
if ( (socket_fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0 ) /* return 1 if okay */
{
printf("ERROR opening socket");
}
serv_addr.sin_family = AF_INET; /*Define the domain used*/
serv_addr.sin_port = htons(PORT); /*Declare port #PORT to be used*/
/*serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); /*Permit any incoming IP address by declaring INADDR_ANY*/
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, SERVER, &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
else
{
printf("\n Connected \n");
}
/* Messaging*/
while(1)
{
printf("Enter command : ");
gets(cmd);
//send the message
if ( (sendto(socket_fd , cmd , strlen(cmd) , 0 , (struct sockaddr *)&serv_addr, sizeof(serv_addr)) ) < 0 )
{
printf("DEBUG_CMD: Failed sending cmd\n");
}
else
{
printf("DEBUG_CMD : CMD * %s * SENT\n", cmd);
puts(cmd);
}
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
memset(recepbuff, '0' ,sizeof(recepbuff));
printf("mini-debug memset done\n");
//try to receive some data, this is a blocking call
if ((recvfrom( socket_fd, recepbuff, strlen(recepbuff), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) ) < 0)
{
printf("DEBUG_RECEP: Failed received cmd\n");
}
else
{
printf("DEBUG_RECEP : CMD RECEIVED\n");
}
puts(recepbuff);
}
close(socket_fd);
return 0;
}
所以,我启动我的程序,它似乎正确连接。我输入我的命令,一个简单的重置然后没有任何反应。就像它在 recvfrom 处被阻止一样(因为打印了“mini debug memset done”)。而且我找不到任何关于我做错的线索。
编辑:修正了recvfrom =&gt;中的strlen一样 事实上,我甚至不确定我是否正在使用正确的方法,即使我真的“联系”......