Instant Messaging App Help


 
Thread Tools Search this Thread
Top Forums Programming Instant Messaging App Help
# 1  
Old 04-27-2005
CPU & Memory Instant Messaging 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 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()
{
   printf("1 = get user list from the server...\n");
   printf("2 = send a msg to a user on the server...\n");
   printf("3 = logout...\n");
   printf("Please enter what you want to do...: ");
}

int main(int argc, char **argv)
{
  int socket_client, bytes;
  char buf[BUF_SIZE];
  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[SML_BUF];
  char opt_par[SML_BUF];
  char data[SML_BUF];
  char sendMe[BUF_SIZE];
  char user[1024];
  int maxfd;
  fd_set var;
  char sendline[BUF_SIZE], recvline[BUF_SIZE];
  int k = 0;
  int i = 0;
  int str_len;
  int usercount = 0;

  // Used for user list function
  union shortUnion
   {
        unsigned short num;
        char bytes[2];
   };
   union shortUnion numUsers;

   //stuct to create the packet
  struct packet
   {
           char  type;
           char  result;
           char  user_len;
           char  param_len;
           short  data_length;
           short sequence;
           char data;
   }packet_data;

  sizeof_packet=sizeof(packet_data);

  if (argc != 3)
     fatal("Usage: server, portnumber...:");
  if (argc == 3)
        servPort = atoi(argv[2]);

  // look up host's IP address
  printf("look up hosts IP Address....: \n");
  host = gethostbyname(argv[1]);
  if (!host)
     fatal("gethostbyname failed");

  printf("creat socket......: \n");
  socket_client = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (socket_client < 0)
     fatal("socket");

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

   FD_ZERO(&var);

   for (;;)
   {
        printf("Enter a user name...: ");
        fgets(userName,SML_BUF,stdin);
        printf("\n");

        printf("Sent the user name:...: %s\n",userName);

        printf("Fill the login request packet...\n");
        packet_data.type = 0;
        packet_data.result= 99;
        packet_data.user_len = strlen(userName);
        packet_data.param_len=0;


        //copy data from buf to packet data
        bcopy(&packet_data,sendMe,8);
        bcopy(userName,sendMe+8,strlen(userName));


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


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


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


        do
        {
        //Function to see what the user would like to do...
        menu ();

        r=getchar();
        
        // Get user list
        if(r == '1')
        {       
           int index = 0;
           printf("Will get user list...\n");
           packet_data.type = 0x04;
           packet_data.result = 99;
           packet_data.user_len = strlen(userName);
           packet_data.param_len = 0;

           bcopy(&packet_data, sendMe, 8);
           bcopy(userName, sendMe+8, strlen(userName));

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


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


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

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

        // Send message to another user
        else if (r == '2')
        {
           printf("Will send a msg...\n");
           printf("Enter recipient's name: ");

        }
        // Logout
        else if (r == '3')
        {
           printf("Will quit now bye...\n");
           packet_data.type=0x02;
           packet_data.result = 99;
           packet_data.user_len = strlen(userName);
             packet_data.param_len=0;

           //copy data from buf to packet data
            bcopy(&packet_data,sendMe,8);
            bcopy(userName,sendMe+8,strlen(userName));


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


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

        }while(r == '1' || r == '2' || r == '3');


        FD_SET(0,&var);
        FD_SET(socket_client,&var);       

       select(maxfd,&var,NULL,NULL,NULL);

        if(FD_ISSET(socket_client, &var))
        {
           if(read(socket_client,recvline,BUF_SIZE) == 0)
            {
               printf("Error here...\n");
               return EXIT_FAILURE;
            }
           fputs(recvline,stdout);
        }

        if(FD_ISSET(0,&var))
        {
           if(fgets(sendline,BUF_SIZE,0) == NULL)
              return;
           write(socket_client,sendline,strlen(sendline));
        }

  
}
}

Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Programming

Wuhan Coronavirus Status App for China - Rapid Prototype using MQTT and the IoT OnOff IOS App

With a little bit of work, was able to build a nice "Wuhan Coronavirus Status" app using MQTT and the IoT-OnOff app. More on this technique here: ESP32 (ESP-WROOM-32) as an MQTT Client Subscribed to Linux Server Load Average Messages The result turned out nice, I think. I like the look and... (10 Replies)
Discussion started by: Neo
10 Replies

2. Red Hat

Instant client

Hi, Could you please tell me how to check instant client is installed in Linux server? OS -- Linux 5.11 Oracle -- 10.2.0.5.0 Regards, Maddy (2 Replies)
Discussion started by: Maddy123
2 Replies
Login or Register to Ask a Question