0

I am creating socket communication through TCP.

If I close client side I am getting an error while closing newClientFd in server side like this

cannot assign requested address

Here is the server code:

#include<stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    int retCode = 0;
    struct sockaddr_in servAddr;

    pipe(cliPipe);
    serverFd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == serverFd)
    {
        retCode = -1;
    }
    else
    {
        mem_memset(&servAddr, '\0', sizeof(servAddr));
        servAddr.sin_family = AF_INET;
        servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servAddr.sin_port = htons(CLI_UTIL_PORT_NUM);
        if (setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0)
        {
            perror("setsockopt(SO_REUSEADDR) failed");
            retCode = -1;
        } 
        if(bind(serverFd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1)
        {
            retCode = -1;
        }
        else
        {
            listen(serverFd, 4);
        }
    }
    newClientFd = accept(serverFd, (struct sockaddr*)NULL, NULL);
    if(-1 != read(newClientFd, &cliMsgHdr, sizeof(CliMsgHdr_t)))
    {printf("read success");}
write(newClientFd, &cliMsgHdr, sizeof(CliMsgHdr_t));
close(newClientFd);

return retCode;
}

Here is the client code:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    int retCode = 0;
    struct sockaddr_in servAddr;
    char *loopbackAddr = "127.0.0.1";


    if(!ipaddr)
    {
        ipaddr = loopbackAddr;
    }

    if((clientFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        retCode = -1;
    }
    if(RC_SUCCESS == retCode)
    {
        memset(&servAddr, '0', sizeof(servAddr)); 
        servAddr.sin_family = AF_INET;
        servAddr.sin_port = htons(CLI_UTIL_PORT_NUM); 

        if(inet_pton(AF_INET, ipaddr, &servAddr.sin_addr)<=0)
        {
            printf("\n inet_pton error occured\n");
            retCode = -1;
        } 
        else if(connect(clientFd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
        {
           printf("\n Error : Connect Failed \n");
           perror("");
           retCode = -1;
        }
    }
    write(clientFd, &cliMsgHdr, sizeof(CliMsgHdr_t));
    read(clientFd, &cliMsgHdr, sizeof(CliMsgHdr_t));
    close(clientFd);
    return retCode;
}

cliMsghdr is an structure to read and send some data.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    This "*while closing newClientFd in server side like this `cannot assign requested address`*" reads like a misdiagnosis to me. – alk Jan 10 '17 at 15:22
  • 2
    This code does not compile. Post the **exact** code that generate the error. – dbush Jan 10 '17 at 15:22
  • Maybe the port identified by CLI_UTIL_PORT_NUM is already in use. Since the value isn't shown, it's hard to know. If that's a port number less than 1024, you may not have the relevant privileges to use the port. – Jonathan Leffler Jan 10 '17 at 16:58
  • @alk while server and client running i am closing client side like ctrl+c or terminal exit i have added perror before closing the newClientFd in server side then i am getting "cannot assign requested address" in server side. – Krishna murthy Setty Jan 11 '17 at 05:44
  • i just want to know how to resolve it.......... – Krishna murthy Setty Jan 11 '17 at 05:46
  • You should only call `perror()` if an error occurred *and* `errno` had been set, else `perror()` prints a "random"-message. Recommended reading: `man errno`, `man perror`, as well as the man-pages of any function call the code use, in this context especially their `RETURN VALUE` and `ERRORS` sections. – alk Jan 11 '17 at 05:59
  • @alk if i didnt call perror() the server get crashes. – Krishna murthy Setty Jan 12 '17 at 06:45

0 Answers0