1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    Help w/ Multiple Client Socket Programming (UDP)

    Hi guys, I need some help with socket programming using UDP. I had successfully connected to a server run by another host, but my problem now is to be able to chat with other users or clients in that server. I'm not sure if my messaged actually got through to the user when prompted. Need some help heading in the right direction.. Thank you! Here are my codes for the client webchat:

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

    char username[16];
    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; s
    truct sockaddr_in chateeAddr; //address of user you want to chat to
    char prompt[10]; //user response when asked to continue chatting

    /*the first step is to get a UDP socket */ UDP_socket = socket(AF_INET, SOCK_DGRAM, 0); char b1[100], b2[100];

    //the second step is to store the information of server inside

    the chateeAddr structure,
    bzero((char *)&chateeAddr,sizeof(chateeAddr)); chateeAddr.sin_family = AF_INET;
    chateeAddr.sin_port = UDPchatee.portnumber; chateeAddr.sin_addr.s_addr = UDPchatee.userIP; memset(chateeAddr.sin_zero, '\0', sizeof chateeAddr.sin_zero);
    int n = sizeof(chateeAddr);

    while(1) {
    //get message you want to send
    printf("Enter a line of text as your message \n"); gets(b2);
    printf("You entered: %s", b2); sendto(UDP_socket,message_sent,sizeof(message_sent),0,(struct sockaddr *)&chateeAddr,n);

    break; recvfrom(UDP_socket,b1,sizeof(b1),0,NULL,NULL); printf("\nReply:%s",b1); }

    //the final steps is to shut down and close connection shutdown ( UDP_socket , 2 );
    close ( UDP_socket );
    for (i = 0; i<10; i++)
    prompt[i] = '\0';
    printf("Please type 'exit' to exit, or anything else to continue chattingg\n");
    fgets(prompt, MAX_MSG_LEN, stdin);

    if (strcmp(prompt, "exit\n") == 0)
    break; //close the UDP socket and exit
    close ( UDP_socketID );
    return 0;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    That is the crappiest job of formatting that I have ever seen! Not only hiding close braces but also hiding entire statements within lines! The worst was this:
    break; recvfrom(UDP_socket,b1,sizeof(b1),0,NULL,NULL); printf("\nReply:%s",b1); }
    where you not only hid the close brace, but you also hid two statements which have nothing to do with the if-statement! And no indentation! Where did you learn to screw up a code listing so horrifically?

    Frankly, my attitude was, "Well, if he wanted us to read his code, then he would have at least tried to make it readable. Since he went out of his way to make it unreadable, then he just doesn't want anybody to read it."

    Even after unravelling that mess, I can't see what you're trying to do. For one thing, a very important ingredient is missing: What is the application protocol that the char server is using? From what I can tell, this isn't a sockets problem, but rather a protocol problem. Since UDP is connection-less and hence "send and forget", it falls on the application protocol to do such things as verifying delivery. What does the application protocol tell you that you need to do?

    Also, you need to tell us what language you're using. It's obviously not C. Is it C++? Is it C99? Why do you want to try to force us to play guessing games?

IMN logo majestic logo threadwatch logo seochat tools logo