配置OpenWrt进行socket编程的问题
我有一个代码可以从以太网接收数据包并通过无线网络发送它。这适用于使用 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;
}
I have a code which receives packets from the Ethernet and sends it via wireless network. This works on a wireless box which uses OpenWrt. The code is written below. It gives me the following error at runtime. Based on the printf statements I feel the error is somewhere on this syntax, but I cant figure out whats wrong.
Probable region of error:
if(sendto(b_sock, buf, sizeof(v2vmessage), 0, (struct sockaddr *)&tx_addr, sizeof(tx_addr)) < 0)
Error:
Receving packets
Packets received
Sending packet started
Variables initialized
Send test
./dsrc_dsrc: can't resolve symbol 'sendto' in lib './dsrc_dsrc'.
Code:
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
main()
中,将v2vmessage* buf
替换为v2vmessage buf
,并在下面的while(1)
中,将buf
替换为&buf
即可解决问题。由于没有为指针分配内存,因此在运行时会出错。In the
main()
, replacev2vmessage* buf
byv2vmessage buf
and in the followingwhile(1)
, replace thebuf
by&buf
to solve the problem. Since, no memory was allocated to the pointer, hence it gave an error at runtime.这是一个链接器错误,您确定您的链接器包含所有必需的 lib 文件吗?我不熟悉 posix 套接字(就包含哪些标头以及要构建的库而言),但我确实知道 sendto 是发送 UDP 数据包的调用,并且可能会失败。如果连接是tcp,则使用send()。希望这有帮助。
That's a linker error, are you sure your linker is including all the required lib files? Im not familiar with the posix sockets (as far as which headers to include and wich libs to build against) , but i do know sendto is a call to send a UDP packet, and it could be failing there. if the connection is tcp, used send(). Hope this helps.
您缺少共享库。使用
ldd(1)
找出是哪一个。您可能只需要设置LD_LIBRARY_PATH
环境变量,请参阅ld.so(8)
手册。You are missing a shared library. Use
ldd(1)
to figure out which one. You probably just need to setLD_LIBRARY_PATH
environment variable, seeld.so(8)
manual.