当前位置: 技术问答>linux和unix
redhat 7.0 进程问题
来源: 互联网 发布时间:2015-06-13
本文导语: int main() { int sockfd, new_fd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size; int retval; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("socketn"); ...
int main()
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int retval;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("socketn");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
printf("setsockoptn");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(12345); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
printf("bindn");
exit(1);
}
if (listen(sockfd, 20) == -1) {
printf("listenn");
exit(1);
}
while(1) {
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
printf("acceptn");
continue;
}
if ((retval=fork())==0) {
close(sockfd);
if (send(new_fd, "Hello, world!n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
~~~~~~~~~~~~~//这里fork没有释放
}else if(retval
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int retval;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("socketn");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
printf("setsockoptn");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(12345); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
printf("bindn");
exit(1);
}
if (listen(sockfd, 20) == -1) {
printf("listenn");
exit(1);
}
while(1) {
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
printf("acceptn");
continue;
}
if ((retval=fork())==0) {
close(sockfd);
if (send(new_fd, "Hello, world!n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
~~~~~~~~~~~~~//这里fork没有释放
}else if(retval