Multiple Chat Programming


 
Thread Tools Search this Thread
Operating Systems Linux Multiple Chat Programming
# 1  
Old 05-25-2013
Multiple Chat Programming

Hello everyone. I have to connect to a running server on another host [COMPLETED]. Then, I have to perform a webchat with any chosen single client in the server and I have some questions about that. I am able to connect to any chosen client in the server, I am able to send my messages through and receive something back. However,

Problems faced: The messages I receive and sent are not what is expected to be. For instance, I sent "Hello" to the receiver and he receives jibberish codes. When the receiver replies to me, I only receive his username in his reply but not his message.

I hope that made sense. I really need help to solve this problem.. Here are my necessary codes for this issue:

Code:
 

   #define MAX_MSG_LEN 1200 /* max size of any msg */

   struct UDP{ /*to perform chat*/
       char username[16];
       char msg[100];
       unsigned long userIP;
       unsigned short portnumber;
  } UDPchatee;    


  /************** UDP variables *************/
  int UDP_socketID;
  struct sockaddr_in serverAddr; //server details!
  struct RegMsg *RegMessage; //registration message sent to server
  int size = sizeof(serverAddr); //size of serverAddr
  RegRespMsg_t RespMessage; //this is the response message received from
  server after UDP connection
  struct in_addr userIP;
  /****************************************/

  /* variables used in both protocols */
  int i; //the for-loop counter
  int check; //this variable store the return values of sendto and receive
  functions to make sure transmission is correct

  /************ UDP variables for chat *****************/
  char username[14];
  int userfound; //flag that is set to 1 when user name type in exists
  char message_sent[MAX_MSG_LEN];
  int len; //message length
  int UDP_socket;
  struct sockaddr_in chateeAddr; //address of user you want to chat to
  struct UDP *UdpChat;
  int sizec = sizeof(chateeAddr);
  char prompt[10]; //user response when asked to continue chatting
  /*********************************************/

  /* UDP Chat */
    //get the user name and IP you want to talk to
    for (i=0; i < 16; i++)
      username[i]= '\0';
    printf("\nPlease type user name you want to talk to: ");
    fgets(username, MAX_MSG_LEN, stdin);
    strcpy(UDPchatee.username, username);
    UDPchatee.username[strlen(UDPchatee.username)-1] = '\0'; //remove the '\n' character
    userfound=-1;

    for(i = 0; i< ntohl(RespMessage.nusers); i++)
     {
        if(strcmp(UDPchatee.username, RespMessage.user[i].username) == 0)
        {
            UDPchatee.userIP = RespMessage.user[i].ipAddr;
            UDPchatee.portnumber = RespMessage.user[i].udpPort;
            userfound = i;
        }
    }
    if(userfound == -1){
        printf("The username does not exist! Try again.\n\n");
        continue;
    }

    //(1) Get a UDP socket //
    UDP_socket = socket(AF_INET, SOCK_DGRAM, 0);
    char msg[100], b1[100];

    //(2) Store the information of server inside the chateeAddr structure,
    bzero((char *)&chateeAddr,sizeof(chateeAddr));
    chateeAddr.sin_family = AF_INET;
    chateeAddr.sin_port = UDPchatee.portnumber; //get port number
    chateeAddr.sin_addr.s_addr = UDPchatee.userIP; //get ip address
    memset(chateeAddr.sin_zero, '\0', sizeof chateeAddr.sin_zero);
    
    while(1)
        {
                //get message you want to send
        printf("\nEnter a line of text as your message: ");
                fgets(msg,MAX_MSG_LEN,stdin);
        strcpy(UDPchatee.msg, msg);
                check = sendto(UDP_socket,UdpChat,sizeof(UDPchatee),0,(struct sockaddr *)&chateeAddr,sizec);
        if (check == -1)
          printf("Sending is in error!\n\n");
                check = recvfrom(UDP_socket,b1,sizeof(b1),0,(struct sockaddr *)&chateeAddr,&sizec);
        if (check == -1)
          printf("Receiving is in error\n");
                printf("\nReply: %s",b1);
        }

I guess my main problem area is the last part where I'm not too sure what are the proper struct to input in the sendto() and recvfrom(). I hope it made sense! Thank you in advanced! Smilie
# 2  
Old 05-25-2013
There are several issues with your code. And some design issues. I think it would help you to read Beej's Guide. This is meant for people just starting to do this. There are example chat programs. You may also want to consider using TCP.

Beej's Guide to Network Programming
# 3  
Old 05-26-2013
I actually started this project with Beej's. Haha.

Could you highlight to me what are the issues with my codes? Cause I don't know if I'm heading in the right direction. When other users tried to send a message to me, I don't receive anything either. :/
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

Very Funny and Somewhat Amazing 2006 Chat Bot Chat

Working on the badging system, Just found this old thread for 2006 and started reading it. ROTFL ... what a great discussion between forum members and our chat bot Gollum "back in the good old days"... You must check this out if you want a laugh and big smile: ... (1 Reply)
Discussion started by: Neo
1 Replies

2. Web Development

Can you embed Skype or any other video chat/chat program into a webpage?

Hi, I am trying to embed Skype or any other video chat/chat program into a webpage. Has anyone had success doing this? or know how? Thanks Phil (2 Replies)
Discussion started by: phil_heath
2 Replies

3. UNIX for Dummies Questions & Answers

socket programming using udp for chat application

hi, i have a source code for 1 server and 2 clients ...but the clients are not able to send data..1 server only receives data from clients and forwards to any other client, the data is in the buffer.....please help... thank you in advance..... /**********client1***************/ // Here Data... (1 Reply)
Discussion started by: unsweety
1 Replies

4. Programming

socket programming (UDP with multiple clients)

Hi all, I have an application where there are 5 udp clients/senders which keep sending data to same IP with different port number can I design my udp server to recieve data from all 5 clients at the same time? how should I use the server address structure? should I use different... (3 Replies)
Discussion started by: shashi
3 Replies
Login or Register to Ask a Question