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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:24:40+00:00 2026-06-03T23:24:40+00:00

I am trying to implement a TCP echo server and client program in Ubuntu

  • 0

I am trying to implement a TCP echo server and client program in Ubuntu 11.04 on my local host.However,I am getting stuck in the bind function.
here is my code.

TCPSERVER

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

struct sockaddr_in serv_addr,cli_addr;
int listenfd,r,w,connfd,val,cli_addr_len;
unsigned short serv_port=30000;
char serv_ip[]="127.0.0.1";
char rbuff[128];
char sbuff[128];

int main()
{
    bzero(&serv_addr,sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_port=htons(serv_port);
    inet_aton(serv_ip,(&serv_addr.sin_addr));
    printf("\nTcp Echo server.\n");
    //creating socket

    if(listenfd=socket(AF_INET,SOCK_STREAM,0)<0)
    {
        printf("\nServer Error:Cannot create socket.\n");
        exit(1);
    }
    if(bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
    {
        printf("\nServer Error:cannot Bind.\n");
        close(listenfd);
        exit(1);
    }
    if((listen(listenfd,5))<0)
    {
        printf("\nServer Error:Cannot listen.\n");
        close(listenfd);
        exit(1);
    }
    cli_addr_len=sizeof(cli_addr);
    for(;;)
    {
        printf("\nServer:listening for clients.... Press Control +C to stpo echo server :\n");
        if((connfd=accept(listenfd,(struct sockaddr*)&cli_addr,&cli_addr_len))<0)
        {
            printf("\nServer Error:Cannot accept client connections.\n");
            close(listenfd);
            exit(1);
        }
        printf("\nServer:connection from client %s accepted.\n",inet_ntoa(cli_addr.sin_addr));
        if((r=read(connfd,rbuff,128))<0)
            printf("\nServer Error:Cannot receive message from client.\n");
        else
        {
            rbuff[r]='\0';
            if((w=write(connfd,rbuff,128))<0)
                printf("\nServer Error:cannot send message to the client.\n");
            else
                printf("Echoed back %s to %s.\n",rbuff,inet_ntoa(cli_addr.sin_addr));
        }
    }
}

after compiling in GCC in ubuntu I am getting bind error..I uploaded the whole code only since I am not getting any error myself and need help.

  • 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-03T23:24:41+00:00Added an answer on June 3, 2026 at 11:24 pm

    just below code’s error:

    if(listenfd=socket(AF_INET,SOCK_STREAM,0)<0)
    {
        printf("\nServer Error:Cannot create socket.\n");
        exit(1);
    }
    

    “=”‘s priority is lower than “<“, modify as follows, you program will be ok:

    if( (listenfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf("\nServer Error:Cannot create socket.\n");
        exit(1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a TCP server which is a part of a
I am trying to implement a minimal chat server in java over regular TCP
I'm trying to implement a TCP connection, everything works fine from the server's side
I'm trying to implement a timeout for a Boost.Asio read on a TCP socket.
I'm trying to implement TCP hole punching with windows socket using mingw toolchain. I
Ok well I'm trying implement something similar to the 'undo' function in many image
After painstakingly trying to implement TCP file transfer in my conference chat application using
I am trying to implement a (T)LV protocol over TCP sending protocol buffers from
I'm just trying to implement a really really small RTSP Client with Boost Asio.
I've a webbrowser control and I'm trying implement IDocHostUIHandler in the container. However since

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.