简体   繁体   中英

Send / receive data over network in C

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);
}

A server is like a telephone operator on a switch board. That person does the following:

  • Sits in front of a phone (ie bind to a number)
  • Waits for it to ring (ie listen )
  • Picks up the phone (ie accept )

The person at the other end just wants to make a call to that person. (ie connect ). The person only needs to go to the phone when a call needs to be made. Therefore not bound to the phone or has to listen for it to ring.

I hope this metaphor helps in your understanding.

PS: The socket part is the phone socket on the wall.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM