套接字寻址机制卡住了

时间:2014-01-06 07:25:22

标签: c linux multithreading sockets communication

基本上我正在尝试做一个与套接字通信的服务器 - 客户端程序。 我发现奇怪的是,服务器一旦启动它甚至不打印第一行。为什么是这样? 必须有一些东西从我身上溜走,我真的需要知道什么。

Server.c

int rvsock;

void stop(int sig){
    close(rvsock);
}

void* worker(void* p){
        struct mymsg m;
        int err;
        int sock = (int)p;
    err = recv(sock,&m,sizeof(m),0);
    if(err < 0){
        printf("Failed to receive");
        exit(1);
    }

    m.a = ntohl(m.a);
    m.b = ntohl(m.b);
    m.c = ntohl(m.c);
    m.ip = ntohl(m.ip);

    printf("Received numbers: %d %d %d from IP:%d", m.a,m.b,m.c,m.ip);

    if(m.a < m.b && m.b <= m.c)
        m.a = m.c;
    else if(m.a < m.b && m.b >= m.c)
            m.a = m.b;
    else if(m.a > m.b && m.b <= m.c)
            m.a = m.a;

    m.a = htonl(m.a);
    m.b = htonl(m.b);
    m.c = htonl(m.c);
    err = send(sock, &m,sizeof(m),0);
    if(err < 0){
        printf("Failed to send!");
        close(sock);
        return NULL;
    }

    close(sock);
    return NULL;
}

int main(int argc, char** argv) {
        printf("DAFUQ");   //It doesn't even print this. Why?
        int port;
        int sock;
        int err;
        unsigned int len;
        struct sockaddr_in saddr;
        struct sockaddr_in caddr;
        pthread_t w[100];
        int wn = 0;
        int i;
        sscanf(argv[1], "%d", &port);

        signal(SIGINT, stop);

        rvsock = socket(AF_INET, SOCK_STREAM, 0);
        if(rvsock < 0) {
            perror("Failed to create socket");
            exit(1);
        }

        memset(&saddr, 0, sizeof(saddr));
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = INADDR_ANY;
        saddr.sin_port = htons(port);

        err = bind(rvsock, (struct sockaddr*)&saddr,
        sizeof(struct sockaddr_in));
        if(err < 0) {
            perror("Failed to bind");
            exit(1);
        }

        err = listen(rvsock, 5);
        if(err < 0) {
            perror("Failed to listen");
            close(rvsock);
            exit(1);
        }

       while(1) {
           len = sizeof(struct sockaddr_in);
           sock = accept(rvsock, (struct sockaddr*)&caddr, &len);
           if(sock < 0) {
               perror("Failed to accept");
               break;
           }

           pthread_create(&w[wn], 0, worker, (int*)sock);
           wn++;
       }

      for(i=0; i<wn; i++) {
          pthread_join(w[i], 0);
      }

      return 0;
}

client.c

int main(int argc, char** argv){
    int sock;
    int err;
    int port;
    struct sockaddr_in saddr;
    struct mymsg m;

sscanf(argv[1], "%d", &port);

sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
    printf("failed to create");
    exit(1);
}

memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(port);

err = connect(sock, (struct sockaddr*)&saddr, sizeof(struct sockaddr_in));
if(err < 0){
    perror("Failed to connect!");
    exit(1);
}

printf("give a:");scanf("%d",&m.a);
printf("give b:");scanf("%d",&m.b);
printf("give c:");scanf("%d",&m.c);

m.a = htonl(m.a);
m.b = htonl(m.b);
m.c = htonl(m.c);

send(sock,&m,sizeof(m),0);

return 0;
}

message.h

struct mymsg{
    int a;
    int b;
    int c;
    int ip;
};

1 个答案:

答案 0 :(得分:2)

在您的服务器中,您拥有此行。 printf打印到stdout FILE,当你启动程序时它会为你打开。

printf("DAFUQ");   //It doesn't even print this. Why?

man stdout提供了有关stdout的以下信息:

The stream  stdout  is  line-buffered
when  it  points  to  a  terminal.  Partial lines will not appear until
fflush(3) or exit(3) is called, or a newline is printed.

您不打印换行符,不刷新stdout并且您的程序没有退出,因此无法满足将输出打印到终端所需的任何条件。

因此,为了打印该行,您有3个选项:

1

printf("DAFUQ");   //It doesn't even print this. Why?
fflush(stdout);

2

printf("DAFUQ\n"); /*notice added '\n'*/   

3 打印后调用exit()(可能不是很有用)。