简体   繁体   English

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

[英]C - Socket Programming - my server can't bind to address

I'm writing a simple UDP echo server and for some reason the server doesn't continue executing after the bind() function. 我正在编写一个简单的UDP echo服务器,由于某种原因,服务器在bind()函数之后不会继续执行。 here's the code: 这是代码:

/*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");
   }


 }       
}

it just doesn't print out the statement after executing it neither do it execute the die () function ! 它只是在执行后不会打印出语句,也不会执行die()函数! the client code 客户端代码

#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);
    }
}

Change this: 改变这个:

printf ("\n binded successfully");

to this: 对此:

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

(ie, add an \\n to the end). (即,在末尾添加\\n )。 This will cause an fflush to be performed on standard output, so that the "success" output will actually be printed to the screen, and you can see that you've entered the while-loop and are waiting for a connection. 这将导致在标准输出上执行fflush ,以便“成功”输出实际上将打印到屏幕上,您可以看到您已进入while循环并正在等待连接。

(Alternatively, you could add an explicit fflush(stdout) after the above statement, to flush standard output yourself. But it's good practice to send the newline, and you don't seem to have a reason for not doing so.) (或者,你可以在上面的语句之后添加一个显式的fflush(stdout)来自己刷新标准输出。但是发送换行符是一种好习惯,你似乎没有理由不这样做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM