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 the "messaging other users" and "getting a user list" functions. The server code that the client will communicate with is not available to me, but I will list the implementation details below. Any help 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.
0x06-Message-Includes recipient user name and message(there is no message reply or acknowledgement)


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: Name of the message recipient, or number of user names in a user list

Optional Data: Arbitrary message data from the user, or 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 or send a message to a user.

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.

iii. If the user wants to send a message, the client will prompt the user
for the recipient and message, and send a Message packet to the
server.

iv. The client will not receive confirmation that the message was
delivered.

b. Message for the user from the server (socket file descriptor)

i. If there is an incoming message from the server, the client will
display the user it’s from along with the message.

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 the client code I have come up with so far. I have bolded the "user list" and "message user" areas.

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 SML_BUF 1024

void menu&#40;&#41;
&#123;
   printf&#40;"1 = get user list from the server...\n"&#41;;
   printf&#40;"2 = send a msg to a user on the server...\n"&#41;;
   printf&#40;"3 = logout...\n"&#41;;
   printf&#40;"Please enter what you want to do...&#58; "&#41;;
&#125;

int main&#40;int argc, char **argv&#41;
&#123;
  int socket_client, bytes;
  char buf&#91;BUF_SIZE&#93;;
  char r;
  struct hostent *host; // info about server
  struct sockaddr_in channel; // holds IP address 
  struct sockaddr_in address_from;
  unsigned short servPort;
  unsigned short size, sizeof_packet;
  unsigned int address_size;
  int string_length;
  char userName&#91;SML_BUF&#93;;
  char opt_par&#91;SML_BUF&#93;;
  char data&#91;SML_BUF&#93;;
  char sendMe&#91;BUF_SIZE&#93;;
  char user&#91;1024&#93;;
  int maxfd;
  fd_set var;
  char sendline&#91;BUF_SIZE&#93;, recvline&#91;BUF_SIZE&#93;;
  int k = 0;
  int i = 0;
  int str_len;
  int usercount = 0;

  &#91;B&#93;// Used for user list function
  union shortUnion
   &#123;
        unsigned short num;
        char bytes&#91;2&#93;;
   &#125;;
   union shortUnion numUsers;&#91;/B&#93;

   //stuct to create the packet
  struct packet
   &#123;
           char  type;
           char  result;
           char  user_len;
           char  param_len;
           short  data_length;
           short sequence;
           char data;
   &#125;packet_data;

  sizeof_packet=sizeof&#40;packet_data&#41;;

  if &#40;argc != 3&#41;
     fatal&#40;"Usage&#58; server, portnumber...&#58;"&#41;;
  if &#40;argc == 3&#41;
        servPort = atoi&#40;argv&#91;2&#93;&#41;;

  // look up host's IP address
  printf&#40;"look up hosts IP Address....&#58; \n"&#41;;
  host = gethostbyname&#40;argv&#91;1&#93;&#41;;
  if &#40;!host&#41;
     fatal&#40;"gethostbyname failed"&#41;;

  printf&#40;"creat socket......&#58; \n"&#41;;
  socket_client = socket&#40;PF_INET, SOCK_DGRAM, IPPROTO_UDP&#41;;
  if &#40;socket_client < 0&#41;
     fatal&#40;"socket"&#41;;

  // Zero out structure 
  memset&#40;&channel, 0, sizeof&#40;channel&#41;&#41;;
  // Internet address family 
  channel.sin_family= AF_INET;
  // Server IP address 
  memcpy&#40;&channel.sin_addr.s_addr, host->h_addr, host->h_length&#41;;
  // Server port 
  channel.sin_port= htons&#40;servPort&#41;;
  // Recv a response  from the server....
  address_size = sizeof&#40;address_from&#41;;

   FD_ZERO&#40;&var&#41;;

   for &#40;;;&#41;
   &#123;
        printf&#40;"Enter a user name...&#58; "&#41;;
        fgets&#40;userName,SML_BUF,stdin&#41;;
        printf&#40;"\n"&#41;;

        printf&#40;"Sent the user name&#58;...&#58; %s\n",userName&#41;;

        printf&#40;"Fill the login request packet...\n"&#41;;
        packet_data.type = 0;
        packet_data.result= 99;
        packet_data.user_len = strlen&#40;userName&#41;;
        packet_data.param_len=0;


        //copy data from buf to packet data
        bcopy&#40;&packet_data,sendMe,8&#41;;
        bcopy&#40;userName,sendMe+8,strlen&#40;userName&#41;&#41;;


        // Send the echo statement to the server 
        if&#40;sendto&#40;socket_client, sendMe, 8+strlen&#40;userName&#41;, 0, &#40;struct 
        sockaddr *&#41; &channel, sizeof&#40;channel&#41;&#41; !=8+strlen&#40;userName&#41;&#41;
        fatal&#40;"fatal sendto&#40;&#41;...\n"&#41;;


        // get the info back from the server
        if &#40;&#40;bytes = recvfrom&#40;socket_client, &packet_data, 8+strlen
        &#40;userName&#41;, 0, &#40;struct sockaddr *&#41; &address_from, &address_size&#41;&#41; <0&#41;
        fatal&#40;"fatal recvfrom&#40;&#41;...\n"&#41;;


        //Test to see if login was a success or not...
        if &#40;packet_data.result == 0&#41;
            printf&#40;"login successful...\n"&#41;;
        else if &#40;packet_data.result == 1&#41;
            printf&#40;"Login Failure&#58; user is already logged in...\n"&#41;;
        else if &#40;packet_data.result == 2&#41;
            printf&#40;"Login Failure&#58; other cause &#40;max num reached, bad request
            packet...&#41;\n"&#41;; 
        else if &#40;packet_data.result == 3&#41;
            printf&#40;"Login Failure&#58; user is not logged in...\n"&#41;;
        else if &#40;packet_data.result == 4&#41;
            printf&#40;"Login Failure&#58; other cause &#40;server failure, bad request 
            packet...\n"&#41;;
        else
           printf&#40;"Login Failure&#58; UNKNOWN.\n\n"&#41;;


        do
        &#123;
        //Function to see what the user would like to do...
        menu &#40;&#41;;

        r=getchar&#40;&#41;;
        
        &#91;B&#93;// Get user list
        if&#40;r == '1'&#41;
        &#123;       
           int index = 0;
           printf&#40;"Will get user list...\n"&#41;;
           packet_data.type = 0x04;
           packet_data.result = 99;
           packet_data.user_len = strlen&#40;userName&#41;;
           packet_data.param_len = 0;

           bcopy&#40;&packet_data, sendMe, 8&#41;;
           bcopy&#40;userName, sendMe+8, strlen&#40;userName&#41;&#41;;

           // Send the echo statement to the server
            if&#40;sendto&#40;socket_client, sendMe, 8+strlen&#40;userName&#41;, 0, &#40;struct 
            sockaddr *&#41; &channel, sizeof&#40;channel&#41;&#41; !=8+strlen&#40;userName&#41;&#41;
            fatal&#40;"fatal sendto&#40;&#41;...\n"&#41;;


        // get the info back from the server
        if &#40;&#40;bytes = recvfrom&#40;socket_client, &packet_data, 8+strlen
        &#40;userName&#41;, 0, &#40;struct sockaddr *&#41; &address_from, &address_size&#41;&#41; <0&#41;
        fatal&#40;"fatal recvfrom&#40;&#41;...\n"&#41;;


           numUsers.bytes&#91;0&#93; = packet_data.data&#91;index&#93;;
           numUsers.bytes&#91;1&#93; = packet_data.data&#91;index+1&#93;;

           printf&#40;"User list reply&#58; %d users logged in.\n",ntohs&#40;numUsers.num&#41;&#41;;
         &#125;

        // Send message to another user
        else if &#40;r == '2'&#41;
        &#123;
           printf&#40;"Will send a msg...\n"&#41;;
           printf&#40;"Enter recipient's name&#58; "&#41;;

        &#125;&#91;/B&#93;
        // Logout
        else if &#40;r == '3'&#41;
        &#123;
           printf&#40;"Will quit now bye...\n"&#41;;
           packet_data.type=0x02;
           packet_data.result = 99;
           packet_data.user_len = strlen&#40;userName&#41;;
             packet_data.param_len=0;

           //copy data from buf to packet data
            bcopy&#40;&packet_data,sendMe,8&#41;;
            bcopy&#40;userName,sendMe+8,strlen&#40;userName&#41;&#41;;


            // Send the echo statement to the server 
            if&#40;sendto&#40;socket_client, sendMe, 8+strlen&#40;userName&#41;, 0, &#40;struct 
            sockaddr *&#41; &channel, sizeof&#40;channel&#41;&#41; !=8+strlen&#40;userName&#41;&#41;
            fatal&#40;"fatal sendto&#40;&#41;...\n"&#41;;


        // get the info back from the server
        if &#40;&#40;bytes = recvfrom&#40;socket_client, &packet_data, 8+strlen
        &#40;userName&#41;, 0, &#40;struct sockaddr *&#41; &address_from, &address_size&#41;&#41; <0&#41;
        fatal&#40;"fatal recvfrom&#40;&#41;...\n"&#41;;
        &#125;

        &#125;while&#40;r == '1' || r == '2' || r == '3'&#41;;


        FD_SET&#40;0,&var&#41;;
        FD_SET&#40;socket_client,&var&#41;;       

       select&#40;maxfd,&var,NULL,NULL,NULL&#41;;

        if&#40;FD_ISSET&#40;socket_client, &var&#41;&#41;
        &#123;
           if&#40;read&#40;socket_client,recvline,BUF_SIZE&#41; == 0&#41;
            &#123;
               printf&#40;"Error here...\n"&#41;;
               return EXIT_FAILURE;
            &#125;
           fputs&#40;recvline,stdout&#41;;
        &#125;

        if&#40;FD_ISSET&#40;0,&var&#41;&#41;
        &#123;
           if&#40;fgets&#40;sendline,BUF_SIZE,0&#41; == NULL&#41;
              return;
           write&#40;socket_client,sendline,strlen&#40;sendline&#41;&#41;;
        &#125;

  
&#125;
&#125;