티스토리 뷰

C, C++

C) basic Socket code (Client)

os94 2019. 9. 11. 20:37

code from Computer_Networks_Larry_Peterson_5ed

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

#define SERVER_PORT 47500
#define MAX_LINE 256

int main (int argc, char * argv[]) {
    FILE *fp;
    struct hostent *hp;
    struct sockaddr_in sin;
    char *host;
    char buf[MAX_LINE];
    int s;
    int len;

    if (argc==2) {
        host = argv[1];
    } else {
        fprintf(stderr, "usage: simplex-talk host\n");
        exit(1);
    }

    /* translate host name into peer's IP address */
    hp = gethostbyname(host);
    if (!hp) {
        fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
        exit(1);
    }

    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    sin.sin_port = htons(SERVER_PORT);

    /* active open */
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("simplex-talk: socket");
        exit(1);
    }
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        perror("simplex-talk: connect");
        close(s);
        exit(1);
    }

    /* main loop: get and send lines of text */
    while (fgets(buf, sizeof(buf), stdin)) {
        buf[MAX_LINE-1] = '\0';
        len = strlen(buf) + 1;
        send(s, buf, len, 0);
    }

}

argc, argv[]

https://msdn.microsoft.com/ko-kr/library/88w63h9k.aspx

ex: cmd> test.exe 123
argv[1] = 123

exit()

https://msdn.microsoft.com/ko-kr/library/k9dcesdd.aspx

'exit(0);' = 'return 0;'

hostent 구조체

[https://docs.microsoft.com/ko-kr/windows/desktop/api/winsock/ns-winsock-hostent](https://docs.microsoft.com/ko-kr/windows/desktop/api/winsock/ns-winsock-hostent)

gethostbyname()

[https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-gethostbyname](https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-gethostbyname)

bzero()

sockaddr_in 구조체

[https://msdn.microsoft.com/en-us/library/zx63b042.aspx](https://msdn.microsoft.com/en-us/library/zx63b042.aspx)

bcopy()

htons()

The**htons**function converts a**u\_short**from host to TCP/IP network byte order (which is big-endian).  
The**htons**function returns the value in TCP/IP network byte order.

socket()

https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-socket

perror()

perror function prints an error message to stderr.

connect()

https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-connect

send()

https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-send

'C, C++' 카테고리의 다른 글

C) Socket Connect : Client~Server  (0) 2019.09.12
C) basic Socket code (Server)  (0) 2019.09.12
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함