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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:12:59+00:00 2026-06-15T08:12:59+00:00

I have written this program: #include <sys/socket.h> #include <sys/types.h> #include <netdb.h> #include <string.h> #include

  • 0

I have written this program:

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int socket_desc;
struct sockaddr_in adress;
int addrlen;
int new_socket;
int bufsize = 1024;

char *you_sent = "You sent: ";

int main() {
    char *buffer = malloc(bufsize);
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    adress.sin_family = AF_INET;
    adress.sin_addr.s_addr = INADDR_ANY;
    adress.sin_port = htons(7000);
    bind(socket_desc, (struct sockaddr *)&adress, sizeof(adress));
    listen(socket_desc, 3);
    addrlen = sizeof(struct sockaddr_in);
    new_socket = accept(socket_desc, (struct sockaddr *)&adress, &addrlen);
    while(recv(new_socket,buffer,bufsize,0))
    {
        printf("I recieved: %s", buffer);
        send(new_socket, you_sent, strlen(you_sent), 0);
        send(new_socket, buffer, strlen(buffer), 0);
        memset(buffer, '\0', sizeof(buffer));
    }
}

I can connect to the server with a telnet. And write stuff to the application and recieve data from the application.
But i cannot get my head around how i can connect to this with another c program and send and recieve data from that program.
I have tried this:

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int socket_desc;
struct sockaddr_in adress;
int addrlen;
int new_socket;

char *message_to_send = "Hello world!";

int main() {
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    adress.sin_family = AF_INET;
    adress.sin_addr.s_addr = INADDR_ANY;
    adress.sin_port = htons(7000);
    bind(socket_desc, (struct sockaddr *)&adress, sizeof(adress));
    listen(socket_desc, 3);
    addrlen = sizeof(struct sockaddr_in);
    new_socket = accept(socket_desc, (struct sockaddr *)&adress, &addrlen);
    send(new_socket, message_to_send, strlen(message_to_send), 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-15T08:13:00+00:00Added an answer on June 15, 2026 at 8:13 am

    The sequence is the following:

    Server side:

    • Socket creation with the socket syscall;
    • Binding of the port with the bind syscall;
    • Listening with the listen syscall (this will enable the backlog queue);
    • Accepting the incoming connections with the accept syscall
      • This is a blocking operation: your thread will remain blocked until a connection comes in);
      • The accept function will return a new file descriptor representing the new connection. You will use this one to send/receive data with the other host, while the original file descriptor (from socket) will be used for new incoming connections.

    Client side:

    • Socket creation with socket;
    • Connection with connect.

    Here you may find some additional resources.

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

Sidebar

Related Questions

I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
#include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> struct person *create_node(char *, char *,int); void addnode(char *,char *,int,struct
i have written a c program for printing the arguments #include<stdio.h> void main( int
i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
This is the situation : I mostly program C# and have written types in
I have written a sample program : #include<iostream> #include<set> #include<conio.h> using namespace std; int
I have the following C program: #include <fcntl.h> #include <termios.h> #include <stdio.h> int main()
I have a question about buffer overflaw, in this program : #include <stdio.h> #include
#include<stdio.h> #include<string.h> #define USER_MEM (10*1024) typedef struct { unsigned short int vol_level; int mute_stat;

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.