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 8727457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:26:16+00:00 2026-06-13T08:26:16+00:00

The server has to echo the message sent by the client using C program

  • 0

The server has to echo the message sent by the client using C program in Linux.I’m using Ubuntu OS (I don’t know whether this information is useful or not!). It worked for the first time. But for the second time, it gave ‘Error Connection’. I tried changing port numbers. But still it didn’t work. Kindly guide me. I’m a beginner.

server.c

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

int main()
{
    int sd, sd1, len, confd, n;
    struct sockaddr_in ser, cli;
    char msg[50];

    if((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
        printf("\nSocket creation error\n");

    bzero(&ser, sizeof(ser));
    ser.sin_family = cli.sin_family = PF_INET;
    ser.sin_port = htons(10000);
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    len = sizeof(ser);

    if ((bind(sd, (struct sockaddr*)&ser, len)) < 0) {
        printf("\nBind Error");
        exit(0);
    }
    if (listen(sd, 2) == 0) {
        if ((sd1 = accept(sd, (struct sockaddr*)&ser, &len)) > 0) {
            do {
                bzero(&msg, 50);
                read(sd1, msg, 50);
                //int m=(int)msg;
                printf("\nMessage from client:%s\n", msg);
                write(sd1, msg, strlen(msg));
                if(strcmp(msg, "exit") == 0)
                    break;
            } while(strcmp(msg, "exit") != 0);
        }
    }
}

*strong text*client.c

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

int main()
{
    int sd, n, len;
    struct sockaddr_in ser, cli;
    char text[50];

    if ((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
        printf("\nSocket creation error\n");

    bzero(&ser, sizeof(ser));
    ser.sin_family = cli.sin_family = PF_INET;
    ser.sin_port = htons(10000);
    ser.sin_addr.s_addr = htonl(INADDR_ANY);
    len = sizeof(ser);

    if ((connect(sd, (struct sockaddr*)&ser, len)) < 0) {
        printf("\nError connection");
        exit(0);
    }
    while(1) {
        strcpy(text, " ");
        printf("\nEnter data which is to be sent:");
        scanf("%s", text);
        write(sd, text, strlen(text));
        read(sd, text, 50);
        printf("\nEcho msg from server:%s", text);
        if (strcmp(text, "exit") == 0)
            break;
    }
    close(sd);
}
  • 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-13T08:26:17+00:00Added an answer on June 13, 2026 at 8:26 am

    Can your client really connect to any address?

    ser.sin_addr.s_addr=htonl(INADDR_ANY);
    

    Most likely you meant to connect to a specific server:

    ser.sin_addr.s_addr=inet_addr("127.0.0.1");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a multi-cllient client-server program in which the server has separate
I have a linux server has an ad-hoc wireless network for clients to connect
If your server has SSL enabled, and you're using YUI uploader, is it possible
I have a com server running as an .exe . This COM server has
Using this hook with VisualSVN Server, added to the Repository/hooks folder as pre-commit.bat. My
I know that this has been asked many times before, but I believe my
I have a TCP server developed on x86 architecture using C under Linux using
Our customers run our php program on their own server. Some are Linux, some
Server has created a socket and bound to a port and started a thread
My Hyper-V server has vms divided in different logical drives. E.g.: D drive of

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.