Thread: Instant messenger app help

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    5

    Instant messenger app help

    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);
            }
    
    
            }
             	        
    }
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure exactly what you're having problems with. You just set aside some field in your packet to denote the number of users, and then fill the 'data' segment with user names.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing is to figure out that functions can contain more than printf statements. Your main() is already over 200 lines long, and you've barely started on the code.

    Like for example, separate functions for all the sending stuff, and receiving stuff
    Separate functions to create / disassemble each packet type.

    Get lots of paper, and start sketching out all the things you need to do and try and organise those things into something more coherent. Because your code has all the hallmarks of a sit down and start hacking approach.

    > instant messaging client(using UDP)
    But UDP is insecure - packets may be dropped and you'll never find out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Instant Messenger
    By Mahi~Mahi in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2008, 03:02 PM
  2. Need some help w/ my instant messenger
    By bikr692002 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-09-2007, 03:54 PM
  3. Instant Messenger for DOS
    By nitinol in forum C Programming
    Replies: 2
    Last Post: 12-21-2005, 10:17 PM
  4. IRC/Flood.c virus from instant messenger
    By lschmidt in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 09-27-2005, 03:41 PM
  5. Editing Instant Messenger (please help!)
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 10-09-2001, 02:23 AM