socketProgramming1

2020. 10. 31. 23:41컴퓨터 네트워크

Network Programming Interface(API)

API: Application Callable Service, 시스템에서 어플리케이션에 제공하는 인터페이스와 abstraction들

 

API for TCP/IP

TCP/IP는 API의 정의를 포함하지 않음

 

TCP/IP를 위한 API들: socket, TLI, XTI, Winsock, MacTCP

Socket API

 

 

 

 

 

 

 

creating a socket

#include <sys/types.h>
#include <sys/socket.h>
int socket(int family, int type, int protocol)

PF_INET: internet protocol (TCP/IP)

PF_INET6: IPv6

PF_LOCAL: for local communication

PF_UNIX: UNIX system internal protocol

 

SOCK_STREAM

SOCK_DGRAM

SOCK_RAW: raw IP

 

프로토콜은 특정 프로토콜을 특정합니다.

 

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

int sockfd;

if((sockfd = socket(PF_INET, SOCK_STREAM, 0))<0){
	/*print "socket error + the error + the error message */
    perror("socket error"); exit(1);
}

//Print error: perror()
#include <stdio.h>
void perror(const char *s)

Bind the local address

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

int bind(int sockfd, struct sockaddr *addr, int addr_len)

Bind는 성공시 0을 실패시 -1을 리턴합니다.

Address Structure

definded in <netinet/in.h>

struct socketaddr{
	u_char	sa_len;
    u_short	sa_family;
    char	sa_data[14];
}
struct sockaddr_in{
	u_char	sin_len;
    u_short sin_family;
    u_short sin_port;
    struct_in_addr	sin_addr;
    char	sin_zero[8];
}
struct in_addr{
	u_long	s_addr;
}
   

Byte Order Conversion

#include<sys/types.h>
#include<netinet/in.h>

/*byte swapping host byte order <-> network */
/*for long and short integer */

u_long	ntohl(u_long hostlong);
u_long ntohl(u_long netlong);
u_short htons(u_short hostshort);
u_short ntohs(u_short netshort);

Name-to-Address Conversion

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

/*return host information by taking host name */
struct hostent *gethostbyname(const char *name)

/*return host information by taking network byte order address */
struct hostent *gethostbyaddr(const char *addr, int length, int type);

Looking up a domain name

#define h_addr haddr_list[0]
struct hostent{
	char 	*h_name;
    char 	**h_alias;
    int		h_addrtype;
    int		h_length;
    char	**h_addr_list;;
};

looking  up a well-known port by name

struct servent{
	char	*s_name;
    char	**s_alias;
    int		s_port;
    int		*s_proto;
};

IP Address Manupulation

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

/*IP address to dotted decimal */
char inet_ntoa(struct in_addr address);

/*dotted decimal to IP address */
u_long inet_addr(char *dottedAddress);

Int inet_aton(const char *cp, struct in_addr *inp);

Synopsis

#include<sys/type.h>
#include<sys/socket.h>
#include<netinet.h/in.h>
#include<arpa.h>

struct socketaddr_in my_addr;

memset($my_addr,0,sizeof(my_addr));
my_addr.sin_family = AF _INET;
my_addr.sin_port = htons(MYPORT);
inet_aton("10.12.110.57",&(my_addr.sin_addr));

printf("%s",inet_ntoa(my_addr.sin_addr))

Address Format Conversion Summary

 

IP Address Manupulation

 

'컴퓨터 네트워크' 카테고리의 다른 글

챕터 3 4-1 정리  (0) 2020.11.25
2장 요약  (0) 2020.10.25
1장 정리  (0) 2020.10.25
3장 정리  (0) 2020.10.25
3장 1~3 요약(미완성)  (0) 2020.10.21