title: UDP server not responding to client
body: <p>Working on a program that is meant to emulate data layers in networking. I've got messages coming through to the server correctly, however, the client is not receiving the ACK frame from the server. This is causing my program to wait endlessly. Any help in fixing the matter is appreciated. </p>
  
  <p>Sender</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #define MAXFRAME  97
  main(int argc, char* argv[]){
      char *frame;
      int len = 0;
      int c;
      dlinits("spirit.cba.csuohio.edu", 43525);
      frame = malloc(MAXFRAME);
  
      FILE *file = fopen(argv[1], "r");
      if (file == NULL)
          return NULL;
  
      while ((c = fgetc(file)) != EOF)
      {
          if(len == (MAXFRAME-1)){
              dlsend(frame, len, 0);
              len = 0;
              memset(frame,0,strlen(frame));
          }       
  
          frame[len++] = (char) c;
      }
  
      dlsend(frame, len, 1);
  
  
  
  
  }
  </code></pre>
  
  <p>Receiver</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  char* dlrecv();
  
  main(){
      char* test[100];
      dlinitr(43525);
      while(1){
          strcpy(test,dlrecv());
  
          printf("%s\n", test);
      }
  
  
  }
  </code></pre>
  
  <p>Data Layer</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #define BUFMAX 100
  
  static int sk;
  static struct sockaddr_in remote;
  static struct sockaddr_in local;
  static int fnum = 0;
  static expFra = 0x00;
  
  dlinits(char* host, int port){//initialize sender
  
      struct hostent *hp;
      sk = socket(AF_INET, SOCK_DGRAM, 0);
  
      remote.sin_family = AF_INET;
  
      hp = gethostbyname(host);
      if (hp == NULL){
          printf("Can't find host name\n");
          exit(1);
      }
  
      bcopy(hp-&gt;h_addr,&amp;remote.sin_addr.s_addr,hp-&gt;h_length);
  
      remote.sin_port = ntohs(port);
  }
  
  dlinitr(int port){//initialize receiver
      int rlen = sizeof(remote);
      int len = sizeof(local);
      char buf[BUFMAX];
  
      sk = socket(AF_INET,SOCK_DGRAM,0);
  
      local.sin_family = AF_INET;
      local.sin_addr.s_addr = INADDR_ANY;
      local.sin_port = htons(port);
      bind (sk, &amp;local,sizeof(local));
  
      getsockname(sk,&amp;local,&amp;len);
  
  }
  
  dlsend(char* msg, int len, int end){//send data
      int header = 0x00;
      int result;
      char *ackframe = malloc(3);
      unsigned char *nmsg;
      nmsg = malloc(100);
      if ((fnum%2) == 1){
          header = header|0x02;
      }
      if (end == 1){
          header = header|0x40;
      }
      header = header^0xff;
      printf("%x\n %x\n", header, 0);
      nmsg[0] = (char)header;
      len++;
      printf("%s\n", nmsg);
      memcpy(nmsg + 1, msg, strlen(msg));
      result = crc(nmsg, len);
      nmsg[len++] = ((result &gt;&gt; 8) &amp; 0xff);
      nmsg[len++] = (result &amp; 0xff);
  
  
      printf("%s\n", nmsg);
      sendto(sk,nmsg,len,0,&amp;remote,sizeof(remote));
  
      read(sk,ackframe,3);
      printf("Ack Received: %s\n", ackframe);
  
      fnum++;
  } 
  
  char* dlrecv(){//receive data
      int result;
      int header;
      int ACK = 1;
      char alen = 1;
      char *ackframe = malloc(3);
      unsigned char* msg = malloc(100);
      while (ACK){
          recvfrom(sk,msg,BUFMAX,0,&amp;remote,sizeof(remote));
          int len = strlen(msg);
          result = crc(msg, len);
          if (result == 0){
              msg[--len] = 0;
              msg[--len] = 0;
              header = msg[0];
              printf("Header %x expFra %x\n", header, expFra);
              header = header^0xff;
              printf("Header %x expFra %x\n", header, expFra);
              if ((header&lt;&lt;4) == (expFra&lt;&lt;4)){
                  expFra = expFra^0x02;
                  ackframe[0] = (0x10|header);
                  result = crc(ackframe, alen);
                  ackframe[alen++] = ((result &gt;&gt; 8) &amp; 0xff);
                  ackframe[alen++] = (result &amp; 0xff);
                  sendto(sk,ackframe,strlen(ackframe),0,&amp;remote,sizeof(remote));
                  printf("Ack Sent: %s\n", ackframe);
                  ACK = 0;
  
              }
          }
      }
      printf("%s\n", msg);
      return ++msg;
  }
  </code></pre>
  
  <p>EDIT for the moment these are working on the same machine.
  EDIT I ran a check using errno, which returned error 22 for the sendto inside of dlrecv.</p>