LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1735|回复: 7

多进程TCP服务器程序

[复制链接]
发表于 2004-5-16 09:24:25 | 显示全部楼层 |阅读模式
哪位能帮我一下!!!???

我要做一个多客户端的服务器程序,要求是服务器端启动之后一直处于监听状态,当又客户机连接的时候就accept,然后给客户端发数据(数据是由传感器经过A/D转换得到的,程序还没有写,所以用常数代替),要能同时给不同的客户端发送相同的数据,我用了fork函数(由于是用uclinux,所以只能用vfork代替fork),但是始终实现不了这个功能,谁能给我看一下,我的程序列出如下:
──────────────────
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<netdb.h>
#include<unistd.h>

#define THEPORT 5000
#define BACKLOG 10

void str_send(int sock_fd)
{
  int short heads=16;
  int short length=htons(2*heads);
  int short num[16];
  int i;
  for(i=0;i<heads;i++)
    num=i+1;
  while(1)
  {    if((send(sock_fd,&length,sizeof(length),0)<0)||(send(sock_fd,&num,sizeof(num),0)<0))
      return;
  }
}

main()
{
  int sock_fd,new_fd;
  struct sockaddr_in this_addr;
  struct sockaddr_in that_addr;
  int sin_size;

  if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
  {
    perror("socket");
    exit(1);
  }
  this_addr.sin_family=AF_INET;
  this_addr.sin_port=htons(THEPORT);
  this_addr.sin_addr.s_addr=INADDR_ANY;
  bzero(&(this_addr.sin_zero),8);
  if (bind(sock_fd,(struct sockaddr *) &this_addr,sizeof(struct sockaddr))==-1)
  {
    perror("bind");
    exit(1);
  }
  while(1)
  {
    if (listen(sock_fd,BACKLOG)==-1)
    {
      perror("listen");
      exit(1);
    }
    sin_size=sizeof(struct sockaddr_in);
    if ((new_fd=accept(sock_fd,(struct sockaddr *) &that_addr,&sin_size))==-1)
    {
      perror("accept");
      exit(1);
    }
    printf("Server:Got connection from %s\n",inet_ntoa(that_addr.sin_addr));
    if(fork()==0)
    {                /*创建子进程*/
      str_send(new_fd);
      close(new_fd);     
      _exit(0);
    }
    /*父进程*/
  }
  close(sock_fd);
}
────────────────────────────
谁给我改一下程序,或者给我指点一下,
我不胜感激!!!
我的QQ:30890545
E-mail:sunheartlee@163.com

再次谢过!!!
发表于 2004-5-16 10:02:11 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include<netdb.h>
#include<unistd.h>

#define THEPORT 5000
#define BACKLOG 10

#define HEADS 16
void str_send(int sock_fd)
{
    int length;
    int num[HEADS];
    int i;
   
    for(i=0;i<HEADS;i++)
        num = i;

    length = sizeof(num[0]) * HEADS;
   
    while((send(sock_fd,&length,sizeof(length),0) > 0)&&(send(sock_fd,num,length,0) > 0));
}

main()
{
    int sock_fd,new_fd;
    struct sockaddr_in this_addr;
    struct sockaddr_in that_addr;
    int sin_size;
    int child;
   
    if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        perror("socket");
        exit(1);
    }
    this_addr.sin_family=AF_INET;
    this_addr.sin_port=htons(THEPORT);
    this_addr.sin_addr.s_addr=INADDR_ANY;
    bzero(&(this_addr.sin_zero),8);
    if (bind(sock_fd,(struct sockaddr *) &this_addr,sizeof(struct sockaddr))==-1)
    {
        perror("bind");
        exit(1);
    }
    while(1)
    {
        if (listen(sock_fd,BACKLOG)==-1)
        {
            perror("listen");
            exit(1);
        }
        sin_size=sizeof(struct sockaddr_in);
        if ((new_fd=accept(sock_fd,(struct sockaddr *) &that_addr,&sin_size))==-1)
        {
            perror("accept");
            exit(1);
        }
        printf("Server:Got connection from %s\n",inet_ntoa(that_addr.sin_addr));

        child = fork();
        
        if(child == 0)
        {
            /*创建子进程*/
            str_send(new_fd);
            close(new_fd);
            _exit(0);
        }
        else
        {
            /*父进程*/
            printf("Server: fork() OK! child = %d\n",inet_ntoa(that_addr.sin_addr), child);
        }
    }
    close(sock_fd);
}
发表于 2004-5-16 14:14:28 | 显示全部楼层

  1. ......
  2. listen
  3. while(1){
  4.   newfd = accept
  5.   pid = fork
  6.   if(pid == 0){
  7.     /* 子进程 */
  8.   }
  9.   close(newfd);
  10. }
  11. close(sock_fd);
复制代码

对于子进程结束以后的僵尸进程可以在父进程里设置SIGCHLD信号处理
 楼主| 发表于 2004-5-16 14:22:16 | 显示全部楼层

怎么在父进程里设置sigchld信号处理??

很感谢chice和libinary版主的帮助,

libinary版主能不能再说的清楚点呀??!
怎么在父进程里设置sigchld信号处理??
最好能举个例子!!

我是刚接触这东西,很不太明白
希望大家多多帮忙!!
谢了先!!
发表于 2004-5-16 15:04:07 | 显示全部楼层
发表于 2004-5-16 22:31:10 | 显示全部楼层
waitpid
处理掉僵死进程
发表于 2004-7-2 11:34:21 | 显示全部楼层
listen应该在while (1)外边吧?
发表于 2004-7-7 10:04:56 | 显示全部楼层
对,listen()不是一个循环,使一直都在运行的一个,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表