No data returned by client over socket [Transport endpoint already connected]

I have two programs Node -A and Node -B where Node -A sends some char * data to Node -B and Node -B sends ack (char *) to Node -A. those. clientNodeA_thread sends data to Node -B serverNodeB_thread. The code looks like this:

node A

int main()
    {
      pthread_t clientNodeA, serverNodeA;

      usleep(3000000);
      pthread_create(&clientNodeA, NULL, clientNodeA_thread, "clientNodeA");
      pthread_create(&serverNodeA, NULL, serverNodeA_thread, "serverNodeA");
      usleep(100000000);

      pthread_join(clientNodeA, NULL);
      pthread_join(serverNodeA, NULL);
      return 0;
    }

    void* clientNodeA_thread(void* pString)
    {
        int connSock, in, i, ret, flags;
        struct sockaddr_in servaddr, NodeAaddr;
        struct sctp_status status;
        char buffer[MAX_BUFFER+1];
        /* Sample input*/
         strncpy(buffer, "FrmNodeAClt", 12);
         buffer[12]='\0';

         connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );

         if(connSock == -1)
           die("socket()");
         #if 0
         bzero( (void *)&NodeAaddr, sizeof(NodeAaddr) );
         NodeAaddr.sin_family = AF_INET;
         NodeAaddr.sin_port = htons(MY_PORT_NUM_NodeA2);
         NodeAaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
         #endif

         bzero( (void *)&servaddr, sizeof(servaddr) );
         servaddr.sin_family = AF_INET;
         servaddr.sin_port = htons(MY_PORT_NUM_NodeB);
         servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );



    ret = connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );

         if(ret == -1)
            die("connect()");

           ret = sctp_sendmsg( connSock, (void *)buffer, (size_t)strlen(buffer),
                              NULL, 0, 0, 0, 0, 0, 0 );
        return 0;
    }

    void* serverNodeA_thread(void* pString)
    {
      int listenSock, connSock, ret, in , flags, i;
      struct sockaddr_in servaddr;
      struct sockaddr_in src_addr;
      struct sctp_initmsg initmsg;
      int addr_len = 0;

      listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
      bzero( (void *)&servaddr, sizeof(servaddr) );
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
      servaddr.sin_port = htons(MY_PORT_NUM_ENB2);

  ret = bind( listenSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );

      /* Specify that a maximum of 5 streams will be available per socket */
      memset( &initmsg, 0, sizeof(initmsg) );
      initmsg.sinit_num_ostreams = 5;
      initmsg.sinit_max_instreams = 5;
      initmsg.sinit_max_attempts = 4;
      ret = setsockopt( listenSock, IPPROTO_SCTP, SCTP_INITMSG, 
                         &initmsg, sizeof(initmsg) );

      listen( listenSock, 5 );

      while( 1 ) {

      char buffer[MAX_BUFFER + 1];
      int len ;  

      bzero(buffer, MAX_BUFFER + 1);

      printf("Awaiting a new connection\n");

      connSock = accept( listenSock, (struct sockaddr *)NULL, (int *)NULL );
      if(connSock == -1)
          die("accept()");
        else
          printf("New client connected....\n");
          addr_len = sizeof (src_addr);
          recvfrom(connSock, buffer, sizeof(buffer), 0, &src_addr, &addr_len);
          printf("Received data from NodeB : %s\n", (char*)buffer);
      }
    }

      

NodeB

int connSock;

/*NodeA context maintenance */
struct NodeAStruct {
  char ipAddr[20];
  unsigned int portNum;
};

static int NodeAInstCount;
struct NodeAStruct NodeAInst[7];

int main()
{

  pthread_t clientNodeA, serverNodeA;

  pthread_create(&serverNodeA, NULL, serverNodeB_thread, "serverNodeA");
  usleep(10000000);

  pthread_join(clientNodeA, NULL);
  pthread_join(serverNodeA, NULL);

  return 0;
}

void* serverNodeB_thread(void* pString)
{
  int listenSock, ret, flags, i;
  struct sockaddr_in src_addr;
  struct sockaddr_in servaddr;
  struct sctp_initmsg initmsg;
  struct sctp_event_subscribe events;
  struct sctp_sndrcvinfo sndrcvinfo;
  char sendBuffer[MAX_BUFFER+1];
  unsigned int NodeA_PORT_NUM;
  char* NodeA_IP_ADDR;  
  char  from_ip[1024] = "", myip[2014] = "";
  int addr_len = 0;

  listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); 
  bzero( (void *)&servaddr, sizeof(servaddr) );
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
  //servaddr.sin_addr.s_addr = htonl("127.0.0.1");
  servaddr.sin_port = htons(MY_PORT_NUM_NodeB);

  ret = bind( listenSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );

  /* Specify that a maximum of 5 streams will be available per socket */
  memset( &initmsg, 0, sizeof(initmsg) );
  initmsg.sinit_num_ostreams = 5;
  initmsg.sinit_max_instreams = 5;
  initmsg.sinit_max_attempts = 4;
  ret = setsockopt( listenSock, IPPROTO_SCTP, SCTP_INITMSG, 
                     &initmsg, sizeof(initmsg) );

  listen( listenSock, 5 );

  while( 1 ) {

  char buffer[MAX_BUFFER + 1];
  int len ;  

  bzero(buffer, MAX_BUFFER + 1);
  printf("Awaiting a new connection\n");

  connSock = accept( listenSock, (struct sockaddr *)NULL, (int *)NULL );
  if(connSock == -1)
      die("accept()");
    else
    {
      printf("New client connected....\n");     
      addr_len = sizeof (src_addr);
      recvfrom(connSock, buffer, sizeof(buffer), 0, &src_addr, &addr_len);

      printf("Received message: %s from NodeA IP: %s Port: %u  \n", (char*)buffer, inet_ntop(AF_INET, &src_addr.sin_addr, from_ip, sizeof(from_ip)), ntohs(src_addr.sin_port));
      strcpy(NodeAInst[NodeAInstCount].ipAddr, inet_ntop(AF_INET, &src_addr.sin_addr, from_ip, sizeof(from_ip)));
      NodeAInst[NodeAInstCount].portNum = ntohs(src_addr.sin_port);
      printf("NodeA instance [%d] added \n", NodeAInstCount);

     /* Send data to NodeA*/
     strncpy(sendBuffer, "From NodeB", 12);
     sendBuffer[12]='\0';
     NodeA_PORT_NUM = NodeAInst[NodeAInstCount].portNum;
     usleep(10000000);
     ret = sctp_sendmsg( connSock, (void *)sendBuffer, (size_t)strlen(sendBuffer),
                          NULL, 0, 0, 0, 0, 0, 0 );
     if(ret>0) {
        printf("Data sent to NodeA \n");
     }
     else {
        printf("Sending data to NodeA failed");
     }
     usleep(6000000);
     NodeAInstCount++;

    }
  }
}

      

I can send data from Node -A client to Node -B server and Node -B can also send Ack data (as tested in Wireshark), but it doesn't reach Node -A to get the code.

Any help in this regard would be very helpful.

+3


source to share


2 answers


The problem is that you are binding the client socket to a local address, but you are not binding the passive server socket.



Do not bind client sockets, it is often unnecessary. You need to bind the server sockets locally, or the system will just bind it to an arbitrary port when you call listen

.

+2


source


Problem solved! I just moved the NodeA socket creation and bound the part to the main function so that it shares both client and server threads, and client and server threads just do send and receive functions.



Thanks @Joachim Pileborg

0


source







All Articles