Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Instant Messaging App Help
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 04-27-2005   #1 (permalink)
Just Joined!
 
Join Date: Mar 2005
Posts: 3
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;

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

   //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();
        
        [B]// 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: ");

        }[/B]
        // 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));
        }

  
}
}
TexAus is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 06:54 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0