当前位置: 技术问答>linux和unix
大虾Help!!! Linux下一个网络编程的问题
来源: 互联网 发布时间:2015-12-11
本文导语: 或者应该叫做虚拟机的问题,我在我的xp系统下装了一个VMware 5.0,然后装了FC5,整个系统运行都没有什么问题。但是我遇到了一个这样的问题: 我写了下面这样一段代码: #include #include #include #include #include #...
或者应该叫做虚拟机的问题,我在我的xp系统下装了一个VMware 5.0,然后装了FC5,整个系统运行都没有什么问题。但是我遇到了一个这样的问题:
我写了下面这样一段代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char * argv[])
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); //* create a socket
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("224.1.1.1");
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
//add IP_ADD_MEMBERSHIP
if(0 > setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq))){
perror("setsockopt IP_ADD_MEMBERSHIP is error ");
close(sockfd);
return -1;
}
//set the socket timeout
struct timeval tv;
tv.tv_sec = 1; //* TBD
tv.tv_usec = 0;
if(0>setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))){
perror("setsockopt SO_RCVTIMEO is error !n");
close(sockfd);
return -1;
}
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr)); // init servaddr
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(1234);
if(0 > bind(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr))){
perror("bind error : ");
close(sockfd);
return -1;
}
char buff[64000];
int rev_len = recvfrom(sockfd, buff, 64000, 0, NULL, NULL);
if(rev_len
我写了下面这样一段代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char * argv[])
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0); //* create a socket
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("224.1.1.1");
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
//add IP_ADD_MEMBERSHIP
if(0 > setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq))){
perror("setsockopt IP_ADD_MEMBERSHIP is error ");
close(sockfd);
return -1;
}
//set the socket timeout
struct timeval tv;
tv.tv_sec = 1; //* TBD
tv.tv_usec = 0;
if(0>setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))){
perror("setsockopt SO_RCVTIMEO is error !n");
close(sockfd);
return -1;
}
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr)); // init servaddr
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(1234);
if(0 > bind(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr))){
perror("bind error : ");
close(sockfd);
return -1;
}
char buff[64000];
int rev_len = recvfrom(sockfd, buff, 64000, 0, NULL, NULL);
if(rev_len