Sockets e选择。这样试试,你可能会赶走一些其他的bug
注意:不能仅仅因为创建套接字的顺序,就依赖于第二个套接字描述符大于第一个。您必须测试或使用Sockets e选择。这样试试,你可能会赶走一些其他的bug,sockets,select,networking,udp,server,Sockets,Select,Networking,Udp,Server,注意:不能仅仅因为创建套接字的顺序,就依赖于第二个套接字描述符大于第一个。您必须测试或使用max() 谢谢。我使用了两个套接字,因为其中一个是从客户机接收数据,另一个是通过另一个套接字向另一台服务器发送数据。此程序正在客户端和服务器之间充当中间人。您好。。我希望在接收到来自客户端1的字节后,字节读取=recvfrom(socket\u fd\u ob,recieve\u data,MAX\u LENGTH,0,(struct sockaddr*)&客户端地址,地址长度);我想将其存储在send_
max()
谢谢。我使用了两个套接字,因为其中一个是从客户机接收数据,另一个是通过另一个套接字向另一台服务器发送数据。此程序正在客户端和服务器之间充当中间人。您好。。我希望在接收到来自客户端1的字节后,字节读取=recvfrom(socket\u fd\u ob,recieve\u data,MAX\u LENGTH,0,(struct sockaddr*)&客户端地址,地址长度);我想将其存储在send_data中,并将其发送到socket socket_fd_hm。您对我的代码有什么需要更改的建议吗?注意:我与Wireshark进行了检查,消息已收到,但未发送到服务器地址。sendto似乎有问题。Thnak yout没有理由不能同时使用同一个套接字。它们是全双工的。我不明白你的第二条评论。如果我使用一个套接字,如何绑定server2地址?它给出了绑定错误。。目前我使用两个套接字,一个用于ClientA,另一个用于clientb(实际上充当服务器),为什么您认为需要绑定两个地址?
bsnayak@ubuntu:~/Desktop/learn$ ./ar4
Main server waiting for client to respond...
(192.168.37.1 , 1901) rcvd: 0x1ae50990
(0.0.0.0 , 0) sent: 0x1ae69030
server_address_hmi.sin_port = htons(1902);
server_address_hmi.sin_addr.s_addr = inet_addr("192.168.37.134");
//Main SERVER
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>//use select() for multiplexing
#include <sys/fcntl.h> // for non-blocking
#define MAX_LENGTH 100000
#define PORT 1901
/* Select() params
* int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
* FD_SET(int fd, fd_set *set);
* FD_CLR(int fd, fd_set *set);
* FD_ISSET(int fd, fd_set *set);
* FD_ZERO(fd_set *set);
*/
void error(char *message)
{
perror(message);
exit(1);
}
int main()
{
// select parameters declared
fd_set original_socket;
fd_set original_stdin;
fd_set readfds;
fd_set writefds;
struct timeval tv;
int numfd, numfd2;
// socket parameters declared
int socket_fd_ob, socket_fd_hm;
int bytes_read, bytes_sent;
char address_length, address_length2;
char recieve_data[MAX_LENGTH];
char send_data[MAX_LENGTH];
struct sockaddr_in server_address_ob, server_address_hm, client_address;
int z = 0;
//Socket creation done separately for both OB and HM communications
if ((socket_fd_ob = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
error("socket()");
}
if ((socket_fd_hm = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
error("socket()");
}
fcntl(socket_fd_ob, F_SETFL, O_NONBLOCK); //set socket to non-blocking
fcntl(socket_fd_hm, F_SETFL, O_NONBLOCK); //set socket to non-blocking
// clear the set ahead of time
FD_ZERO(&original_socket);
FD_ZERO(&original_stdin);
FD_ZERO(&readfds);
FD_ZERO(&writefds);
// add our descriptors to the set (0 - stands for STDIN)
FD_SET(socket_fd_ob, &original_socket);//instead of 0 put socket_fd_ob
FD_SET(socket_fd_ob, &readfds);
FD_SET(0,&original_stdin);
FD_SET(0, &writefds);
// since we got s2 second, it's the "greater", so we use that for
// the n param in select()
numfd = socket_fd_ob + 1;
numfd2 = socket_fd_hm + 1;
// wait until either socket has data ready to be recv()d (timeout 10.5 secs)
tv.tv_sec = 1;
tv.tv_usec = 500000;
server_address_ob.sin_family = AF_INET;
server_address_ob.sin_port = htons(1901);
server_address_ob.sin_addr.s_addr = inet_addr("192.168.37.1");
bzero(&(server_address_ob.sin_zero),sizeof(server_address_ob));
server_address_hm.sin_family = AF_INET;
server_address_hm.sin_port = htons(1902);
server_address_hm.sin_addr.s_addr = inet_addr("192.168.37.134");
bzero(&(server_address_ob.sin_zero),sizeof(server_address_hm));
// Bind socket to the particular addresses
if (bind(socket_fd_ob,(struct sockaddr *)&server_address_ob, sizeof(struct sockaddr)) == -1)
{
error("bind()");
}
if (bind(socket_fd_hm,(struct sockaddr *)&server_address_hm, sizeof(struct sockaddr)) == -1)
{
error("bind()");
}
address_length = sizeof(struct sockaddr);
address_length2 = sizeof(struct sockaddr);
printf("\nMain server waiting for client to respond...\n");
fflush(stdout);
while (1)
{
readfds = original_socket;
writefds = original_stdin;//problem
int recieve = select(numfd, &readfds, &writefds,/*NULL,*/ NULL, &tv);
int sent = select(numfd2, &readfds, &writefds,/*NULL,*/ NULL, &tv);
if (recieve == -1 || sent == -1)
{
perror("select"); // error occurred in select()
}
else if (recieve == 0 || sent == 0)
{
printf("Timeout occurred! No data after 1.5 seconds.\n");
}
else
{
// one or both of the descriptors have data
if (FD_ISSET(socket_fd_ob, &readfds)) //if set to read
{
FD_CLR(socket_fd_ob, &readfds);
bytes_read = recvfrom(socket_fd_ob,recieve_data,MAX_LENGTH,0,(struct sockaddr *)&client_address, &address_length);
for (z = 0; z < bytes_read; ++z) {
FD_ISSET(socket_fd_hm, &writefds);
FD_CLR(socket_fd_hm, &writefds);
//recvfrom speech recognition client and decide what to send to HM accordingly..
send_data[bytes_read] = recieve_data[bytes_read];
//block call, will wait till client enters something, before proceeding
//send the corresponding to HM
bytes_sent = sendto(socket_fd_hm,send_data,strlen(send_data)+1,0,(struct sockaddr *)&server_address_hm, &address_length2);
fflush(stdout);
}
recieve_data[bytes_read] = '\0'; //add null to the end of the buffer
send_data[bytes_read] = '\0'; //add null to the end of the buffer
printf("\n(%s , %d) rcvd: %02x\n",inet_ntoa(client_address.sin_addr),ntohs(client_address.sin_port),recieve_data);
printf("\n(%s , %d) sent: %02x\n",inet_ntoa(server_address_hm.sin_addr),ntohs(server_address_hm.sin_port),send_data);
}
} //end else
}//end while
close (socket_fd_ob);
return 0;
}