Professional Documents
Culture Documents
네트워크프로그래밍2-1 16년 2학기.eng
네트워크프로그래밍2-1 16년 2학기.eng
4
3 TCP/IP
2
Network driver
1
Socket number (1)
local port :
..
. remote IP :
remote port :
..
Socket number (2)
Connection-oriented
연결형 서비스 services connectionless
비연결형 서비스 services
응용 1 응용 2 응용 3 응용 4
응용 프로그램
fd=3 sd=4 sd=3 sd=3 sd=3 (소켓번호)
소 켓
IP 인터넷 계층
TCP/IP
192.203.144.11 (IP 주소)
• IP address
– Used to identify a host and deliver IP datagrams to destinations
• Port number
– 16 bit number used to identify a process on a host
– Two different protocols (TCP, UDP) can share a port number
• Well-known ports
– Port numbers under 1024
– Predefined
– ex) ftp, telnet, mail, http, etc.
• /etc/services
– defines network (or TCP/IP) services and their port numbers
Socket creation
• types available
SOCK_STREAM // TCP socket
SOCK_DGRAM // UDP socket
SOCK_RAW // Raw socket
Socket examples
• To do
– Open two files (/etc/passwd, /etc/hosts) and two sockets.
Then, prints their descriptor numbers
• man page
– Use to retrieve information about APIs/functions
$ man socket
Network Functions socket(3N)
NAME
socket - create an endpoint for communication
SYNOPSIS
cc [ flag ... ] file ... -lsocket -lnsl [ library ... ]
#include <sys/types.h>
#include <sys/socket.h>
DESCRIPTION
socket() creates an endpoint for communication .....
Structure for socket address - sockaddr
struct sockaddr {
u_short sa_family; // address family
char sa_data[14]; // address
};
struct in_addr {
u_long s_addr; // used to hold a 32bit IP address
};
struct sockaddr_in {
short sin_family; // address familty
u_short sin_port; // 16bit port
struct in_addr sin_addr; // 32bit IP address
char sin_zero[8]; // padding (to make the total size 16byte)
};
• sin_family
• TCP(connection-oriented) socket
서 버
Server 클라이언트
Client
socket() socket()
bind()
listen()
Connection connect()
연결요청
request
accept()
데이터 송수신
Exchange of data
send() recv()
recv() send()
Connection
termination
종료
close()
2.2 Internet address translation
Byte order
주 소: n n+1 n n+1
데이터 : E2 C3 C3 E2
– servent structure
struct servent {
char *s_name;
char **s_aliases;
int s_port;
char *s_proto;
};
byte_order.c (2)
• Srouce code
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main(void) {
struct servent *serv = getservbyname("echo", "udp");
if (serv == NULL) {printf( “Information unavailable.\n"); exit(0);}
return 0;
}
• Results
// On SPARC systems
$ byte_order
UDP echo port no.(network byte order) : 7
UDP echo port no.(host byte order) : 7
const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
int inet_pton(int af, const char *src, void *dst);
gethostbyname() inet_pton()
gethostbyaddr() inet_ntop()
ascii_ip.c
14
15 int main(int argc, char *argv[]) {
16 struct in_addr inaddr; 17 char buf[20];
18
23 printf("* Input dotted decimal address: %s\n", argv[1]);
24
25 inet_pton(AF_INET, argv[1], &inaddr.s_addr);
26 printf(" inet_pton(%s) = 0x%X\n", argv[1], inaddr.s_addr);
27 inet_ntop(AF_INET, &inaddr.s_addr, buf, sizeof(buf));
28 printf(" inet_ntop(0x%X) = %s\n", inaddr.s_addr,buf);
29
30 return 0;
31 }
• Results
$ ascii_ip 210.15.36.231
* Input dotted decimal address : 210.15.36.231
inet_pton(210.115.36.231) = 0xE72473D2
inet_ntop(0xE72473D2) = 210.115.36.231
Translating domain name
• Translation functions
#include <netdb.h>
struct hostent *gethostbyname(const char *hname);
struct hostent *gethostbyaddr(const char *in_addr, int len, int family);
gethostbyname() inet_pton()
gethostbyaddr() inet_ntop()
Translating domain name (cont.)
• gethostbyname()
– returns a ponter of hostent structure containing host
information corresponding to hname
• gethostbyaddr()
– returns a pointer of hostent structure based on in_addr, length,
address type arguments
• hostent structure
struct hostent {
char* h_name;
char** h_aliases;
int h_addrtype; // e.g., AF_INET=2
int h_length; // address length
char** h_addr_list; // list of addresses
};
if (myhost == NULL) {
printf("Error at gethostbyaddr() \n");
exit(0);
}
printf(“host name : %s\n", myhost->h_name);
return 0;
}