Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:57:13+00:00 2026-06-18T21:57:13+00:00

I am trying to create a threaded chat server in C, but cant figure

  • 0

I am trying to create a threaded chat server in C, but cant figure out how to not echo the message back to the user. I want to be able to connect two or more clients and have the chat to each other through my server.

Where am I going wrong:

#include<stdio.h>
#include<string.h>  //strlen
#include<stdlib.h>  //strlen
#include<sys/socket.h>
#include<arpa/inet.h>   //inet_addr
#include<unistd.h>  //write
#include<pthread.h> //for threading , link with lpthread
#include<errno.h>

void *connectionHandler(void * soc_desc);

typedef struct { 
    int reader;
    int writer;
}args;

int main(int arc, char* argv[]){

    int *new_socket;
    int socFD;
    int client_sock;
    struct sockaddr_in serveraddr;
    struct sockaddr_in clientaddr;
    args t1args, t2args;
    int listenFd;
    //socklen_t clientSize= size(struct sockaddr_in);
    int c;
    int acceptFD;
    int client_sockets[2];

    socFD=socket(AF_INET, SOCK_STREAM, 0);

    puts("Socket created");

    bzero((char*) &serveraddr, sizeof(serveraddr));

    serveraddr.sin_family= AF_INET;

    serveraddr.sin_addr.s_addr=INADDR_ANY;

    serveraddr.sin_port= htons(2000);

    bind(socFD, (struct sockaddr*) &serveraddr, sizeof(serveraddr));

    puts("bind done");

    listen(socFD, 3);

    puts("Waiting for incoming connections...");
    c= sizeof(struct sockaddr_in);

    while((client_sock=accept(socFD, (struct sockaddr *)&clientaddr,(socklen_t *)&c)))
    {

        printf("%d", client_sock);
        puts("IN WHILE LOOP");

        pthread_t t1;
        new_socket= malloc(1);
        *new_socket=client_sock;

        if(pthread_create(&t1, NULL, connectionHandler, (void *)new_socket)<0){
                perror("Could not create thread");
                return -1;
        }
    }

    if(client_sock < 0)
    {
        perror("accept failed");
        return -1;
    }

        return 0;
}

void *connectionHandler(void * soc_desc)
{
    //Get the socket descriptor
//      args *temp= (args*)soc_desc; 
        int sock= *(int*)soc_desc;
        int read_size;
        char *message , client_message[2000];
        char buff[80];
/*
        while(1){
             bzero(buff,sizeof(buff));
             recv(temp->reader,buff,sizeof(buff),0);
             send(temp->writer,buff,sizeof(buff),0);

        }*/
    //Receive a message from client
        while( (read_size = recv(sock , client_message , 2000 , 0)) >0 )
        {
            //Send the message back to client
            write(sock, client_message ,strlen(client_message));
        }

        if(read_size == 0)
        {
            puts("Client disconnected");
            fflush(stdout);
        }
        else if(read_size == -1)
        {
            perror("recv failed");
        }

        //Free the socket pointer
        free(soc_desc);
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:57:14+00:00Added an answer on June 18, 2026 at 9:57 pm

    http://beej.us/guide/bgnet/html/single/bgnet.html

    I did socket programming abt 3 yrs ago with C, this was what i followed. Helped me a lot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a listview with balloons. Its like a threaded chat
Trying to create a black line in my view to separate text blocks but
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
So I'm trying to create a simple multi-threaded game engine for the game I
I found a pre-written script of a class to create threaded comments, but after
I'm trying to set up a basic threaded class in C++, but I'm getting
I've got some code that is trying to create 100 threaded http calls. It
I am trying to create a multi threaded PHP application right now. I have
I've been trying to create a runnable class in order to interface multi-threaded classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.