[Week07] Socket Programming in C๐ŸŒ

ellaยท2023๋…„ 4์›” 18์ผ
0

๐ŸŒณ์ •๊ธ€ 6๊ธฐ๐ŸŒณ

๋ชฉ๋ก ๋ณด๊ธฐ
16/39

์˜ค๋Š˜์€ ์ฝ”์น˜๋‹˜์ด ์˜ฌ๋ ค์ค€ https://youtu.be/LtXEMwSG5-8 ์˜์ƒ์„ ๋ณด๊ณ  ๋”ฐ๋ผํ•ด๋ณด๊ณ , ์ •๋ฆฌํ•ด์„œ ์ฑ…๊ณผ ์กฐ๊ธˆ ๋‹ค๋ฅธ ์Šคํƒ€์ผ์˜ ์ฝ”๋“œ๋ฅผ ๊ณต๋ถ€ํ•ด ๋ณด์•˜๋‹ค.

ํ•ด๋‹น ๊ฐ•์˜๋Š” ์•„๋ž˜ ์ฝ”๋“œ์— include๋˜์–ด์žˆ๋Š” ํ—ค๋”ํŒŒ์ผ ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ•ด socket ํ†ต์‹ ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์‚ฌ์‹ค ์šฐ๋ฆฌ๊ฐ€ proxy.lab์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋Šฅ๋“ค๋„ csapp.hํŒŒ์ผ์— ๊ฐ€๋ณด๋ฉด ๋ชจ๋‘ include๋˜์–ด ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฉด ๋ฐ”๋กœ ์„œ๋ฒ„๊ฐ€ ๊บผ์ง€๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ์œ„ํ•ด ํ•จ์ˆ˜๋กœ ํ•œ์ธต ๊ฐ์•„๋†“๊ณ  ์‚ฌ์šฉํ•˜๋„๋ก ๋˜์–ด์žˆ๋‹ค.

ํ•ด๋‹น๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๋“ค์ด ์ง๊ด€์ ์ด๋งŒ, ์ด์ •๋„๊นŒ์ง€๋งŒ ๊ณต๋ถ€ํ•˜๊ณ , ๋‹ค์‹œ proxy.c๋ฅผ ๊ตฌํ˜„ํ•˜๋Ÿฌ๊ฐ€์•ผ๊ฒ ๋‹ค.


clinet part

logic

code

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

int main(){

    // create a socket
    int network_socket;
    /* socket์˜ 
    param1: domain of the socket, header file์„ ๋ญ๋ผ๊ณ  ๋ถ€๋ฅผ ๊ฑด์ง€ ,
    param2: type of the socket(TCP or UDP)
    param3: ํŠน๋ณ„ํ•œ ํ”„๋กœํ† ์ฝœ์„ ์“ฐ๊ณ ์‹ถ์„ ๋•Œ, ์šฐ๋ฆฌ๋Š” TCP default๋ฅผ ์“ธ๊ฑฐ๋ผ 0*/
    network_socket = socket(AF_INET, SOCK_STREAM, 0);

    // specify an address for the socket
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9002); //์šฐ๋ฆฌ๊ฐ€ ์—ฐ๊ฒฐํ•ด์ค˜์•ผํ•  ํŠน์ • ํฌํŠธ specify. htons()๋Š” conversion funt: intger port-> network byte order
    server_address.sin_addr.s_addr = INADDR_ANY; // ์„œ๋ฒ„๊ฐ€ ๋ฐ”์ธ๋”ฉํ•  IP ์ฃผ์†Œ. ์ด ๊ฐ’์€ 0, ์„œ๋ฒ„๊ฐ€ ๋ชจ๋“  IP ์ฃผ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋“ค์–ด์˜ค๋Š” ์—ฐ๊ฒฐ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๋„๋ก.
    
    // 0์ด๋ฉด ok
    int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
    
    //check for error with the connection
    if (connection_status == -1){
        printf("There was an error making a connection to the remote socket \n\n");
    }

    //receive data from the server
    char server_response[256];
    recv(network_socket, &server_response, sizeof(server_response), 0);

    //print out the server's response
    printf("The server sent the data: %s\n", server_response);

    //and then close the socket
    close(network_socket);
    return 0;
}

server

logic

code

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

int main(){

    char server_message[256] = "You have reached the server!";

    //socket ์ƒ์„ฑ
    int server_socket;
    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    //address struct ์ •์˜
    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9002);
    server_address.sin_addr.s_addr = INADDR_ANY;

    //socket์„ ํŠน์ • IP์™€ Port์— binding
    bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));

    //listen ์†Œ์ผ“ ๋งŒ๋“ค๊ธฐ
    listen(server_socket, 5); 

    //accept ์†Œ์ผ“ ๋งŒ๋“ค๊ธฐ data ์™”๋‹ค๊ฐ”๋‹คํ•˜๋Š” ์†Œ์ผ“
    int client_socket;
    client_socket = accept(server_socket, NULL, NULL);

    //๋ฉ”์„ธ์ง€ ๋ณด๋‚ด๊ธฐ
    send(client_socket, server_message, sizeof(server_message), 0);

    //์†Œ์ผ“ ๋‹ซ๊ธฐ
    close(server_socket);

    return 0;
}
profile
^^*

0๊ฐœ์˜ ๋Œ“๊ธ€