Computer network communication

Posted by Pedro Sim on Thu, 24 Feb 2022 06:57:27 +0100

OSI network seven layer model: (Open System Interconnection), translated as "Open System Interconnection"
Physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer

TCP/IP simplifies OSI, merges some layers, and finally retains only four layers, including interface layer, network layer, transport layer and application layer from bottom to top. This is the famous TCP/IP model


socket stands on the basis of transport layer

Local process communication
Network process communication: the protocol + port of the transport layer of the triple (IP address, protocol and port) represents the process in the host, and the IP can represent the network process, which can further facilitate network interaction
Sock_Stream: connection (transmission + verification) http
sock_Dgram: no connection (just transmission, no verification) qq video voice. The loss will not affect the normal analysis, but only noise

1. Division of main communication levels

Application layer: HTTP, FTP, SMTP, DNS, Telnet
Transport layer: socket is based on three data packets (handshake) of TCP / UDP (transmission control protocol: providing point-to-point connection: encapsulation, addressing, transmission, routing, and how the destination accepts)
Network layer: IP, ICMP, OSPF
Interface layer: SLIP, PPP, MTU
Each abstraction layer is based on the services provided by the lower layer and provides services for the higher layer

TCP (Transmission Control Protocol) is a connection oriented, reliable and byte stream based communication protocol. The data should be connected before transmission and disconnected after transmission. The client should use the connect() function to establish a connection with the server before sending and receiving data. The purpose of establishing connection is to ensure the correctness of IP address, port and physical link, and open up a channel for data transmission

2 socket common function interface and principle


MAC

MAC address is the abbreviation of Media Access Control Address, which is directly translated as "Media Access Control Address", also known as LAN Address, Ethernet Address or Physical Address.

socket server client

server

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

int main(){
    //Create socket
    int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    //Bind socket to IP and port
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //Each byte is filled with 0
    serv_addr.sin_family = AF_INET;  //Use IPv4 address
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //Specific IP address
    serv_addr.sin_port = htons(1234);  //port
    bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    //Enter the listening state and wait for the user to initiate a request
    listen(serv_sock, 20);

    //Receive client requests
    struct sockaddr_in clnt_addr;
    socklen_t clnt_addr_size = sizeof(clnt_addr);
    int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);

    //Send data to client
    char str[] = "http://c.biancheng.net/socket/";
    write(clnt_sock, str, sizeof(str));
   
    //Close socket
    close(clnt_sock);
    close(serv_sock);

    return 0;
}

client

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main(){
    //Create socket
    int sock = socket(AF_INET, SOCK_STREAM, 0);

    //Make a request to the server (specific IP and port)
    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));  //Each byte is filled with 0
    serv_addr.sin_family = AF_INET;  //Use IPv4 address
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");  //Specific IP address
    serv_addr.sin_port = htons(1234);  //port
    connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
   
    //Read the data returned by the server
    char buffer[40];
    read(sock, buffer, sizeof(buffer)-1);
   
    printf("Message form server: %s\n", buffer);
   
    //Close socket
    close(sock);

    return 0;
}

Topics: network Network Protocol TCP/IP