I was hoping someone could assist me on a text-based instant messaging client(using UDP) I am working on. I have the network communication basics already in place but, specifically, needed some further help on a "getting a user list" function. Eventually, I will get a "send message to user" function going, but I just wanted to get the user list working for now. The server code that the client will communicate with is not available to me, but I will list the implementation details below. Any help or advice on how to recieve a user list from the server would be greatly appreciated.

The client header fields include:

Type:
0x00-Login request
0x01-Login reply
0x02-Logout request
0x03-Logout reply
0x04-User list request
0x05-User list reply-Parameter indicates the number of users. Data contains list of users each separated by a comma.

User Len: Length of the From User Name field

Param Len: Length of the Optional Parameter field

Data Length: Length of the Optional Data field

Sequence Number: Used to match requests and replies between client and server. In other words, a request’s corresponding reply will have the same sequence number. The client should increment the sequence number (by one) with each new request.

From User Name: Client user name

Optional Parameter: Number of user names in a user list

Optional Data: List of user names

Implementation Details:
Here is an example sequence of events:

1. The client will prompt for a user name.
2. The client will issue a login request to the server, and receive a reply from the server.
3. Upon a successful login, the client will use the select() call and wait for input from:
a. User input (file descriptor 0)

i. Upon receiving user input, the client will determine if the user
wants to request a user list.

ii. If the user asks for a user list, the client will send a user list request
packet to the server, and display the response.


4. If the user chooses to quit, a logout request is sent to the server, and the client exits.
5. Repeat steps 3-4

Here is a rough go at the client code I have come up with so far. I have bolded the "user list" area.

Code:
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define BUF_SIZE 4096
#define BUF_SIZE2 1024

void menu()
{
  printf("*************** User Menu *******************\n");
  printf("1 - Request list of users currently logged on\n");
  printf("2 - Logout\n");
  printf("Enter selection:");
}

union shortUnion
{
  unsigned short num;
  char bytes[2];
};
union shortUnion numUsers;



int main(int argc, char **argv)
{
  int sock;
  int nbytes;
  int address_size;
  int server_port;
  int string_length;
  int str_len;
  int len;
  int maxfd;
  int k = 0;
  int i = 0;
  int usercount = 0;
  char buf[BUF_SIZE];
  char buffer[BUF_SIZE];
  char choice;
  char user_name[BUF_SIZE2];
  char opt_par[BUF_SIZE2];
  char data[BUF_SIZE2];
  char user[BUF_SIZE2];
  char recipient[BUF_SIZE2];
  char message[BUF_SIZE];
  char sendline[BUF_SIZE];
  char recvline[BUF_SIZE];
  struct hostent *host_name;
  struct sockaddr_in ip_address;
  struct sockaddr_in echo_address;
  fd_set var;

  
  
  // Header used for client requests and server replies
  struct udphder
  {
    char  type;
    char  result;
    char  user_len;
    char  param_len;
    int   data_len;
    int   sequence;
    char  data[1024];
  } packet;

 

  if (argc != 3)
  {
    fprintf(stderr, "Usage: %s <Server IP Address> <Port Number>\n", argv[0]);
    exit(1);
  }

  server_port = atoi(argv[2]);

  // Get network host entry
  host_name = gethostbyname(argv[1]);

  // Create a communication point
  sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0)
  {
    fprintf(stderr, "socket error.\n");
    exit(1);
  }


  // Build server adress structure
  memset(&ip_address, 0, sizeof(ip_address));
  ip_address.sin_family= AF_INET;
  memcpy(&ip_address.sin_addr.s_addr, host_name->h_addr, host_name->h_length);
  ip_address.sin_port= htons(server_port);
  address_size = sizeof(echo_address);


   
  printf("Enter user name: ");
  fgets(user_name,BUF_SIZE2,stdin);
  printf("\n");

 // Populate login request packet
  printf("Logging in...\n\n");
  packet.type = 0;
  packet.result = 6;
  packet.user_len = strlen(user_name);
  packet.param_len = 0;
	
  // Copy data from bufer to packet data
  bcopy(&packet,buffer,8);
  bcopy(user_name,buffer+8,strlen(user_name));

  // Send entered data to server
  if(sendto(sock, buffer, 8+strlen(user_name), 0, (struct sockaddr *)
    &ip_address, sizeof(ip_address)) != 8+strlen(user_name))
  {
     fprintf(stderr, "send error.\n");
     exit(1);
  }



  if ((recvfrom(sock, &packet, 8+strlen(user_name), 0,
     (struct sockaddr *) &echo_address, &address_size)) < 0)
 {
    fprintf(stderr, "recv error.\n");
    exit(1);
 }


  // Test login attempt status
    /*switch (packet.result)
    {
       case 0:
         printf("login successful...\n");\
       case 1:
       {
         printf("Login Failure: user is already logged in...\n");
         exit(1);
       }
       case 2:
       {
         printf("Login Failure: other cause (max num reached, bad request packet...)\n");
         exit(1);
       }
       case 3:
       {
         printf("Login Failure: user is not logged in...\n");
         exit(1);
       }
       case 4:
       {
         printf("Login Failure: other cause (server failure, bad request packet...\n");
         exit(1);
       }
     }*/
	
      	
      
      menu ();
      choice=getchar();
      switch(choice)
      {

      case '1':
      {
         int index = 0;
         // Prepare packet for user list request
         packet.type = 0x04;
         packet.result = 6;
         packet.user_len = strlen(user_name);
         packet.param_len = 0;

       // Copy data from buffer to packet data
       bcopy(&packet, buffer, 8);
       bcopy(user_name, buffer+8, strlen(user_name));


       if(sendto(sock, buffer, 8+strlen(user_name), 0, (struct sockaddr *)
          &ip_address, sizeof(ip_address)) != 8+strlen(user_name))
       {
         fprintf(stderr, "send error.\n");
         exit(1);
       }

       if ((recvfrom(sock, &packet, 8+strlen(user_name), 0,
           (struct sockaddr *) &echo_address, &address_size)) < 0)
       {
         fprintf(stderr, "recv error.\n");
         exit(1);
       }


        
      numUsers.bytes[0] = packet.data[index];
      numUsers.bytes[1] = packet.data[index+1];

      printf("User list reply: %d users logged in.\n",ntohs(numUsers.num));

 	   	   
      }
 
      case '2':
      {
        printf("Logging out\n");
        packet.type=0x02;
        packet.result = 6;
        packet.user_len = strlen(user_name);
        packet.param_len=0;
 
        // Copy data from buffer to packet data
        bcopy(&packet,buffer,8);
        bcopy(user_name,buffer+8,strlen(user_name));

        if(sendto(sock, buffer, 8+strlen(user_name), 0, (struct sockaddr *)
          &ip_address, sizeof(ip_address)) != 8+strlen(user_name))
       {
          fprintf(stderr, "send error.\n");
          exit(1);
       }


        if ((recvfrom(sock, &packet, 8+strlen(user_name), 0,
           (struct sockaddr *) &echo_address, &address_size)) < 0)
        {
          fprintf(stderr, "recv error.\n");
          exit(1);
        }


        }
         	        
}

}