[linux] C language Linux system programming socket development

struct sockaddr_in serv_addr;

1. Define the structure variable. The structure is a data type, so you can use it to define variables

2.struct structure name variable name;

 

(struct sockaddr*)&serv_addr

1. Structure pointer, which can also point to a structure

2. The format of casts is: (type ﹐ name) expression

3. Address character &, get the address of a variable

4. The number of bytes in memory is called Address or Pointer. Generally speaking, variable name represents the data itself, while function name, string name and array name represent the first Address of code block or data block

 

serv_addr.sin_family = AF_INET;

1. Assign values to structural members

 

char buffer[40];

1. Define a character array. The character array is actually a collection of a series of characters, that is, string. In C language, there is no special string variable

2. When assigning a value, you can directly assign a string to a character array or not specify the array length

3. Strings always end with '\ 0'. The length of the array is greater than the length of the string (string length does not include '\ 0') by 1

server.c

 

#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);
    //Put socket and IP,Port binding
    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");  //concrete 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 the 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[] = "Hello World!";
    write(clnt_sock, str, sizeof(str));
   
    //Close socket
    close(clnt_sock);
    close(serv_sock);
    return 0;
}

 

client.c

#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); 
    //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");  //concrete 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;
}

Tags: C socket

Posted on Sun, 17 May 2020 09:04:27 -0700 by billspeg