简体   繁体   中英

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. 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 ! 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). 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.

(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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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