Suggestions on how to handle this more efficiently

From: Materialised (materialised_at_ntlworld.com)
Date: 09/04/03


Date: Thu, 04 Sep 2003 16:54:54 +0100

Hello everyone,
I have just started programming in c on freeBSD. I have been reading
Beej's Guide to network programming, and am currently developing my
first program, a tcp based 1 on one chat client and server.

I am looking for suggestions on how I can handle the sending and
recieving of the strings differently, rather than having a continuous
loop running through the program, as to me this doesnt seem very efficient.

Here is my code....

//chatclient.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 2100
#define MAXDATASIZE 10000
#define MAX_LENGTH 1024

int main(int argc, char *argv[])
{
        int len, sockfd, numbytes;
        char buf[MAXDATASIZE];
        struct hostent *he;
        struct sockaddr_in their_addr; // connector's address information
        char msg[MAX_LENGTH];
        
        if (argc != 2) {
                printf("Usage: ./chatclient <IPADDRESS>\n");
                exit(1);
        }
        if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
                perror("gethostbyname");
                exit(1);
        }
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(1);
        }
        their_addr.sin_family = AF_INET; // host byte order
        their_addr.sin_port = htons(PORT); // short, network byte order
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct

        if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr)) == -1) { //And finally we connect
                perror("connect");
                exit(1);
        }
        else printf("Connected to %s\n\n", argv[1]);

        
        do {
                printf("Please Enter a message to send\n");
                scanf("%s", msg);
                len = strlen(msg);
                if(len > 1024) {
                        printf("You can only send a Maximum of 1024 characters\n");
                }
                else {
                        if(send(sockfd, msg, len, 0) == -1) {
                                perror("send");
                                exit(1);
                        }
                // break;
                }
                
                if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
                        perror("recv");
                        exit(1);
                }
                else printf("Message from %s:%s\n", argv[1], buf);

        } while (!strcmp(msg, "exit") == 0);
        

        return 0;
}



Relevant Pages

  • Re: Full sample for select()
    ... I guess you've never done networking programming with TCP sockets. ...
    (comp.unix.programmer)
  • Re: Full sample for select()
    ... I guess you've never done networking programming with TCP sockets. ... then after filling up your TCP buffers you won't be ...
    (comp.unix.programmer)
  • Re: Structures question.
    ... SOAP? ... TCP? ... Remoting? ... "Programming is an art form that fights back" Develop ASP.NET applications easier and in less time: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regarding "perils of java schools"
    ... design patterns. ... Beyond the C++ language itself, I expect a programmer to have more ... Your original message also mentioned "Network programming." ...
    (comp.programming)
  • Re: regarding "perils of java schools"
    ... design patterns. ... Beyond the C++ language itself, I expect a programmer to have more ... Your original message also mentioned "Network programming." ...
    (comp.programming)