C - 套接字编程 - 我的服务器无法绑定到地址

时间:2014-04-19 15:00:55

标签: c linux sockets

我正在编写一个简单的UDP echo服务器,由于某种原因,服务器在bind()功能之后没有继续执行。 这是代码:

/*Required Headers*/


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

#define MAXLINE 100

void HandleClient(int comm_fd);
void Die (const char * msg)
{
   perror(msg);
   exit(1);
}

int main(int argc, char * argv[])
{

    char str[100];
    int listen_fd, comm_fd,n;
    socklen_t len;
    struct sockaddr_in servaddr, cliaddr;

    if (argc != 2) {
        fprintf(stderr, "USAGE: ./HelloITServer <port>\n");
        exit(1);
    }

    if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        Die("Falied to create socket");
    };

    printf ("%d" ,listen_fd);
    memset( &servaddr,0, sizeof(servaddr));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htons(INADDR_ANY);
    servaddr.sin_port = htons(atoi(argv[1]));

    if (bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0)
    { 
       Die("Failed to bind socket to address");
    }
    else {
        printf ("\n binded successfully");
    }

//    if (listen(listen_fd, 10) < 0)
//    {
//          Die("Failed to listen on server socket");
//    }


    while(1) 
    {

//        if ((comm_fd = accept(listen_fd, (struct sockaddr*) NULL, NULL)) < 0) {
//              Die("Failed to accept client connection");
//          }

//        HandleClient (comm_fd);
        memset (&cliaddr,0,sizeof(cliaddr));
        len = sizeof (cliaddr);
        n = recvfrom (listen_fd,str,MAXLINE,0, (struct sockaddr *) &cliaddr,&len);

        sendto (listen_fd, str, n, 0,(struct sockaddr *) &cliaddr, len);
    }
}


void HandleClient(int comm_fd)
{

  char str[MAXLINE];
  int received;

  memset(str, 0, MAXLINE);
  received  =  read(comm_fd,str,MAXLINE);
  if (received < 0) {
      Die("Failed to receive from client");
   }
  while(received > 0)
  {

     printf("Echoing back - %s",str);

     if ( write(comm_fd, str, received) != received){
         Die("Failed to send bytes to client");
     }
    memset(str, 0, MAXLINE);
    received =   read(comm_fd,str,MAXLINE);
    if (received < 0) {
      Die("Failed to receive from client");
   }


 }       
}

在执行它之后它不会打印出语句也不会执行die()函数! 客户端代码

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include<string.h>
#include <stdlib.h>


int main(int argc,char *argv[])
{
    int sockfd,n;
    char sendline[100];
    char recvline[101];
    struct sockaddr_in servaddr;

    if (argc != 3)
    {
        fprintf(stderr, "USAGE: ./HelloClient <server_ip> <port>\n");
        exit(1);
    }

    sockfd=socket(AF_INET,SOCK_DGRAM,0);
    bzero(&servaddr,sizeof servaddr);

    servaddr.sin_family=AF_INET;
    servaddr.sin_port= htons(atoi(argv[2]));

    inet_pton(AF_INET,argv[1],&(servaddr.sin_addr));

    //connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

//    while(1)
//    {
//        bzero( sendline, 100);
//        bzero( recvline, 100);
//        fgets(sendline,100,stdin); /*stdin = 0 , for standard input */
// 
//        write(sockfd,sendline,strlen(sendline)+1);
//        read(sockfd,recvline,100);
//        printf("%s",recvline);
//    }
    while (1) {
        bzero( sendline, 100);
        bzero( recvline, 100);
        fgets(sendline, 100, stdin);
        sendto(sockfd, sendline, strlen (sendline), 0, (const struct sockaddr *) &servaddr, (socklen_t) sizeof (servaddr));
        n = recvfrom (sockfd, recvline, 100, 0, NULL, NULL);
        printf("%d",n);
        recvline[n]=0;
        printf ("%s",recvline);
    }
}

1 个答案:

答案 0 :(得分:2)

改变这个:

printf ("\n binded successfully");

到此:

printf ("\n binded successfully\n");

(即,在末尾添加\n)。这将导致fflush在标准输出上执行,以便&#34;成功&#34;输出实际上会打印到屏幕上,您可以看到您已进入while循环并正在等待连接。

(或者,你可以在上面的陈述之后添加一个明确的fflush(stdout)来自己刷新标准输出。但是发送换行符是一种很好的做法,而你似乎没有不这样做的原因。)