250 likes | 449 Views
Sockets. contents. 0x421 Socket Functions 0x422 Socket Addresses 0x423 Network Byte Order 0x424 Internet Address Conversion 0x425 A Simple Server Example 0x426 A Web Client Example 0x427 A Tinyweb Server. 0x420 Sockets.
E N D
contents • 0x421 Socket Functions • 0x422 Socket Addresses • 0x423 Network Byte Order • 0x424 Internet Address Conversion • 0x425 A Simple Server Example • 0x426 A Web Client Example • 0x427 A TinywebServer
0x420 Sockets • A socket is a standard way to perform network communication through the OS. • There are two kinds of sockets. • Stream socket • Provide reliable two-way communication similar to when you call someone on the phone • Datagram socket • Communicating with a datagram socket is more like mailing a letter than making a phone call. The connection is one-way only and unreliable.
0x421 Socket Functions • socket(int domain, int type, int protocol) • Used to create a new socket, returns a file descriptor for the socket or -1 on error. • connect(intfd, structsockaddr *remote_host, socklen_taddr_length) • Connects a socket to a remote host. Returns 0 on success and -1 on error. • bind(intfd, structsockaddr *local_addr, socklen_taddr_length) • Binds a socket to a local address so it can listen for incoming connections. Returns 0 on success and -1 on error. • listen(intfd, intbacklog_queue_size) • Listens for incoming connections and queues connection requests up to backlog_queue_size. Returns 0 on success and -1 on error.
0x421 Socket Functions • accept(intfd, sockaddr *remote_host, socklen_t *addr_length) • Accepts an incoming connection on a bound socket. The address information from the remote host is written into the remote_host structure and the actual size of the address structure is written into *addr_length. This function returns a new socket file descriptor to identify the connected socket or -1 on error. • send(intfd, void *buffer, size_t n, int flags) • Sends n bytes from *buffer to socket fd; returns the number of bytes sent or -1 on error. • recv(intfd, void *buffer, size_t n, int flags) • Receives n bytes from socket fd into *buffer; returns the number of bytes received or -1 on error.
0x421 Socket Functions • Socket.h We usually use this prtocol If you have played starcraft, you know this protocol
0x421 Socket Functions • Socket.h almost always be 0
0x422 Socket Addresses • If you dedicate a Protocol family, Address family will automatically determined
0x422 Socket Addresses • sa = Socket Address
0x422 Socket Addresses • Internet
0x423 Network Byte Order • The port number and IP address used in the AF_INET socket address structure are expected to follow the network byte ordering, which is big-endian. This is the opposite of x86’s little-endian byte ordering, so these values must be converted.
0x424 Internet Address Conversion • xxx.xxx.xxx.xxx
0x425 A Simple Server Example • Hacking.h
0x425 A Simple Server Example • Simple_server.c
0x425 A Simple Server Example Sockfd – Socket file descriptor SOL_SOCKET - There are many different socket options defined in /usr/include/asm/socket.h SO_REUSEADDR - SOL_SOCKET 일 경우 옵션의 하나 - 이미 사용하고 있는 어드레스를 바인드 할 수 있도록 한다.
0x426 A Web Client Example • HTTP • Get • Head
0x426 A Web Client Example • Hacking-network.h
0x426 A Web Client Example • Host_lookup.c
0x427 A Tinyweb Server • Tinyweb.c • 127.0.0.1
0x427 A Tinyweb Server • Tinyweb.c • 127.0.0.1