QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 808|回复: 2

关于TCP C/S的小问题

[复制链接]
发表于 2003-8-24 16:18:09 | 显示全部楼层 |阅读模式
写了一个小程序 UDP CLIENT sendto udp server a msg 然后server再转发给一个tcp server  .tcp display msg
在转发的时候 accept 会提示bad file descriptor 为什么?
TCP client
[code:1]

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>


#define MYPORT 7000    // the port users will be connecting to
#define MAXDATASIZE 100
#define BACKLOG 10     // how many pending connections queue will hold

void sigchld_handler(int s)
{
    while(wait(NULL) > 0);
}

int main(void)
{
    int sockfd, new_fd,numbyte;  // listen on sock_fd, new connection on new_fd
    struct sockaddr_in my_addr;    // my address information
    struct sockaddr_in their_addr; // connector's address information
    int sin_size;
    char buf[MAXDATASIZE];
    struct sigaction sa;
    int yes=1;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
   
    my_addr.sin_family = AF_INET;         // host byte order
    my_addr.sin_port = htons(MYPORT);     // short, network byte order
    my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
    memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    while(1) {  // main accept() loop
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
            perror("accept");
        }
        printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
        close(sockfd);      
            if ((numbyte=recv(new_fd,buf,MAXDATASIZE-1,0)) == -1){
                perror("recv");
            close(new_fd);
            exit(0);
            }
        
        buf[numbyte]='\0';
        printf("receive: %s",buf);
        close(new_fd);  // parent doesn't need this
       
    }

    return 0;
}
[/code:1]
 楼主| 发表于 2003-8-24 16:20:12 | 显示全部楼层
udp server
[code:1]
    #include <stdio.h>

    #include <stdlib.h>

    #include <unistd.h>

    #include <errno.h>

    #include <string.h>

    #include <sys/types.h>

    #include <sys/socket.h>

    #include <netinet/in.h>

    #include <arpa/inet.h>



#define MYPORT 6789

#define TPORT 7000

#define MAXBUFLEN 100



int main(void)

{

    int sockfd,tsockfd;

    struct sockaddr_in my_addr;    // my address information

    struct sockaddr_in their_addr; // connector's address information

    struct sockaddr_in ttheir_addr;

    int addr_len, numbytes;

    char buf[MAXBUFLEN];



    if((sockfd = socket(AF_INET, SOCK_DGRAM,0)) == -1) {

       perror("socket");

       exit(1);

    }

    my_addr.sin_family = AF_INET;

    my_addr.sin_port = htons(MYPORT);

    my_addr.sin_addr.s_addr = INADDR_ANY;

    memset(&(my_addr.sin_zero), '\0', 8);

   

    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {

       perror("bind");

       exit(1);

    }



    addr_len = sizeof(struct sockaddr);

    if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) ==-1) {

        perror("recvfrom");

        exit(1);

     }
      printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
        printf("packet is %d bytes long\n",numbytes);
        buf[numbytes] = '\0';
        printf("packet contains \"%s\"\n",buf);
      close(sockfd);

     

   if((tsockfd=socket(AF_INET, SOCK_STREAM, 0))==-1) {

       perror("socket");

       exit(1);

   }

   ttheir_addr.sin_family = AF_INET;

   ttheir_addr.sin_port=htons(TPORT);

   ttheir_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

   memset(&(ttheir_addr.sin_zero),'\0',8);

   

   if(connect(tsockfd,(struct sockaddr *)&ttheir_addr, sizeof(struct sockaddr))==-1) {

      perror("connect");

      exit(-1);

   }

   if(send(tsockfd,buf,strlen(buf),0)==-1)

      perror("send");

      close(tsockfd);

      exit(0);

   
   printf("sendto %s\n",inet_ntoa(ttheir_addr.sin_addr));
   close(tsockfd);



   return 0;

}


[/code:1]

UDP间通信没有问题 就是UDP和TCP间有问题 能connect
回复

使用道具 举报

发表于 2003-8-27 15:22:20 | 显示全部楼层
TCP CLIENT AND UDP SERVER?EXPECTING
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-15 06:12 , Processed in 0.040657 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

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