Thread: TCP/IP client & echo server

  1. #1
    Registered User
    Join Date
    Jun 2005

    Question TCP/IP client & echo server

    I am creating a simple chat program as my final project for a self taught c++ programming class.

    Heres what i want to accomplish:
    client: you type something in the client and push enter and it sends that to the server. the client also prints out anything the server echos back to the client.

    server: the server accepts connections from the client(s) and then the server takes any text it gets from the clients and echos it back out to all the clients. Making a simple chat program.

    This is a fairly simple process but im finding it fairly difficult to work with multiple clients.

    What i need is help figuring out the process to do this. Here is my understanding of what should happen.

    client / server:
    create socket
    make socket NON-blocking
    bind to it
    loop select and if read or write flags are triggured read or write

    im not sure how the sockets work. i have a listens socket and i guess im suppost to make a new connection for every client but once my code gets past the accepting of the new clients and i go to write it just quits. and the write flag of select never even gets triggured. This is just something to think about, as soon as i get home ill post the actual code i have so far.


    if you have any other ideas for how i could accomplish this please post (besides using fork etc)

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Check out the link in my sig ( and take a look at my multithreaded socket library and the multi-client chat sample apps. Internally it differs from the method you're proposing, but the interface is similar however simpler.

    You derive a Client socket class and override OnReceive to handle all data reception. You start your server and it listens in a separate thread for clients. When one connects a new Client object is created and it will have OnReceive called on the server side whenever the client sends data.

    Give it a look and let me know what you think.

    [ps. the library is now using CodePlug's very well-written thread class]

  3. #3
    Registered User
    Join Date
    Jun 2005
    // includes
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <sys/wait.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <strings.h>
    #include <unistd.h>
    #include <time.h>
    #include <fcntl.h>
    // define constants
    #define SA struct sockaddr
    #define BUFFERSIZE 1024
    // main program
    int main(int argc, char **argv)
    printf("Server started\n");
    // define variables
    fd_set read_flags,write_flags;
    int listens, work, x, checking,i,wx;
    struct sockaddr_in servaddr;
    char buff[1024];
    int clients[26];
    struct timeval timer; 
    // creates socket
    listens = socket(AF_INET, SOCK_STREAM, 0);
    // sets servaddr to all zeros
    bzero(&servaddr, sizeof(servaddr));
    // sets servaddr
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(2007);
    // making listens socket non-blocking
    fcntl(listens,F_SETFL,x | O_NONBLOCK);
    // bind
    if(bind(listens, (SA *) &servaddr, sizeof(servaddr))<0){
    printf("Bind Failed!\n");
    // listening
    if (listen(listens, BUFFERSIZE)<0){
    printf("Listen Failed!\n");
    snprintf(buff, sizeof(buff), "Hello people, i came from a server woot");
    for ( ; ; ) {
    timer.tv_sec = 1;
    timer.tv_usec = 0;
    FD_SET(listens, &read_flags);
    FD_SET(listens, &write_flags);
    select(listens+1, &read_flags,&write_flags,(fd_set*)0,&timer);
    if (FD_ISSET(listens, &read_flags)) {
        FD_CLR(listens, &read_flags);
        if(clients[i]=accept(listens, (SA *) NULL, NULL)>0){
        std::cout << "New client " << i << ": Return: " << clients[i] << " \n";
        wx=write( listens, buff, strlen(buff));
        std::cout << "Returned: " << wx;
    if (FD_ISSET(listens, &read_flags)) {
     FD_CLR(listens, &write_flags);
     std::cout << "go ahead and write\n";
    Thats what i got so far

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  3. Server and Client process
    By wise_ron in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-07-2006, 01:11 AM
  4. Client - Server TCP/IP MFC app..... Help!
    By amedinadiaz in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2005, 11:57 AM
  5. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM