我有一个代码,它接收来自以太网的数据包并通过无线网络发送。这适用于使用OpenWrt的无线盒。代码如下。它在运行时给出了以下错误。根据printf语句,我觉得错误是在这个语法的某个地方,但我不知道什么是错的。
可能的错误区域:
if(sendto(b_sock, buf, sizeof(v2vmessage), 0, (struct sockaddr *)&tx_addr, sizeof(tx_addr)) < 0)
错误:
Receving packets
Packets received
Sending packet started
Variables initialized
Send test
./dsrc_dsrc: can't resolve symbol 'sendto' in lib './dsrc_dsrc'.
代码:
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#include <arpa/inet.h> /* for sockaddr_in */
#define BROADCAST_IP "192.168.255.255"
#define BROADCAST_PORT 4545
struct v2vmessage
{
...
...
};
int b_sock=-1;
void init_socket()
{
unsigned short b_port = BROADCAST_PORT;
struct sockaddr_in b_addr;
int broadcastPermission;
char* rx_ip = BROADCAST_IP;
if ((b_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
perror("socket() failed");
/* Set socket to allow broadcast */
broadcastPermission = 1;
if (setsockopt(b_sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0)
perror("setsockopt() failed");
int opts;
opts = fcntl(b_sock,F_GETFL);
if(opts < 0)
perror("fcntl get failed");
opts = (opts | O_NONBLOCK);
if(fcntl(b_sock,F_SETFL,opts) < 0)
perror("fcntl set failed");
memset(&b_addr, 0, sizeof(b_addr)); /* Zero out structure */
b_addr.sin_family = AF_INET; /* Internet address family */
b_addr.sin_addr.s_addr = inet_addr(rx_ip);/* Broadcast IP address */
b_addr.sin_port = htons(b_port); /* Broadcast port */
if (bind(b_sock, (struct sockaddr *) &b_addr, sizeof(b_addr)) < 0)
perror("rx bind() failed");
}
void send_thread_body(v2vmessage *buf)
{
printf("Sending packet started\n");
char *tx_ip = BROADCAST_IP;
unsigned short tx_port = BROADCAST_PORT;
struct sockaddr_in tx_addr;
printf("Variables initialized\n");
memset(&tx_addr, 0, sizeof(tx_addr)); /* Zero out structure */
tx_addr.sin_family = AF_INET; /* Internet address family */
tx_addr.sin_addr.s_addr = inet_addr(tx_ip);/* Broadcast IP address */
tx_addr.sin_port = htons(tx_port); /* Broadcast port */
printf("Send test\n");
if(sendto(b_sock, buf, sizeof(v2vmessage), 0, (struct sockaddr *)&tx_addr, sizeof(tx_addr)) < 0)
perror("tx sent diff num bytes than expected");
}
int main(int argc, char *argv[])
{
init_socket();
{
int sock, length, n,p;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
if (argc < 2)
{
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}
sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
perror("Opening socket");
length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi(argv[1]));
if (bind(sock,(struct sockaddr *)&server,length)<0)
perror("binding");
fromlen = sizeof(struct sockaddr_in);
v2vmessage *buf;
while (1)
{
printf("Receving packets\n");
n = recvfrom(sock,buf,sizeof(v2vmessage),0,(struct sockaddr *)&from,&fromlen);
if (n < 0)
perror("recvfrom");
printf("Packets received\n");
send_thread_body(buf);
printf("Packets sent\n");
}
}
return 0;
}
答案 0 :(得分:1)
在main()
中,按v2vmessage* buf
和v2vmessage buf
替换while(1)
,将buf
替换为&buf
以解决问题。因为,没有为指针分配内存,因此它在运行时给出了错误。
答案 1 :(得分:0)
这是一个链接器错误,您确定您的链接器包含所有必需的lib文件吗?我不熟悉posix套接字(至于要包含哪些头文件以及要构建的库),但我知道sendto是一个发送UDP数据包的调用,它可能会失败。如果连接是tcp,则使用send()。希望这会有所帮助。
答案 2 :(得分:0)