一台机器为物理机,装的ubuntu,另一台是虚拟机,装的redhat。收发程序如果运行在同一个系统里面,则通信正常,分开放在2个系统里面则不通。
收的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <netinet/in.h>
#define SERPORT 65530
#define CLIPORT 65531
#define BUFLEN 1500
#define IPADDR "192.168.1.108"
int main ( int argc, char *argv[] )
{
int serverfd;
int nready,rn = 0,wn = 0;
fd_set rset,wset;
struct sockaddr_in server_addr, client_addr;
socklen_t peer_addr_size = sizeof(client_addr);
struct timeval tv;
char buf[BUFLEN];
serverfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERPORT);
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
inet_pton(AF_INET, IPADDR, &server_addr.sin_addr);
memset(&client_addr, 0, sizeof(struct sockaddr_in));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(CLIPORT);
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
inet_pton(AF_INET, IPADDR, &client_addr.sin_addr);
if (bind(serverfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in)) == -1)
{
perror("bind");
close(serverfd);
return -1;
}
while ( 1 )
{
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_SET(serverfd, &rset);
tv.tv_sec = 1;
tv.tv_usec = 0;
nready = select(serverfd+1, &rset, NULL, NULL, &tv);
if ( nready < 0 )
{
perror("select");
continue;
}
else if ( nready == 0 )
{
continue;
}
else
{
if (FD_ISSET(serverfd, &rset))
{
rn = recvfrom(serverfd, buf, BUFLEN, 0, NULL, NULL);
if ( rn < 0 )
{
perror("recvfrom");
continue;
}
printf("rn:%d>>%s\n",rn, buf);
}
}
}
return 0;
}
发的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <netinet/in.h>
#define SERPORT 65530
#define CLIPORT 65531
#define BUFLEN 1500
#define IPADDR "192.168.1.103"
int main ( int argc, char *argv[] )
{
int sockfd;
int nready,rn = 0,wn = 0;
fd_set rset,wset;
struct sockaddr_in server_addr, client_addr;
socklen_t peer_addr_size = sizeof(client_addr);
struct timeval tv;
char buf[BUFLEN];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERPORT);
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
inet_pton(AF_INET, IPADDR, &server_addr.sin_addr);
memset(&client_addr, 0, sizeof(struct sockaddr_in));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(CLIPORT);
//client_addr.sin_addr.s_addr = htonl(INADDR_ANY);
inet_pton(AF_INET, IPADDR, &client_addr.sin_addr);
if (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(struct sockaddr_in)) == -1)
{
perror("bind");
close(sockfd);
return -1;
}
while ( 1 )
{
FD_ZERO(&wset);
FD_SET(sockfd, &wset);
tv.tv_sec = 1;
tv.tv_usec = 0;
nready = select(sockfd+1, NULL, &wset, NULL, &tv);
if ( nready < 0 )
{
perror("select");
continue;
}
else if ( nready == 0 )
{
continue;
}
else
{
if (FD_ISSET(sockfd, &wset))
{
wn = sendto(sockfd, "abc", 4, 0, (struct sockaddr *)&server_addr, peer_addr_size);
sleep(1);
}
}
}
close(sockfd);
return 0;
}
```
```
已关闭双方防火墙,selinux。
使用nmap分别在2个系统上扫描对方的udp端口,状态均为
PORT STATE SERVICE
65530/udp open|filtered unknown
PORT STATE SERVICE
65531/udp open|filtered unknown
网络是自家局域网,没发现路由做了端口禁用。如何分析?