1 / 24

Sockets

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.

fauve
Download Presentation

Sockets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sockets

  2. 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

  3. 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.

  4. 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.

  5. 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.

  6. 0x421 Socket Functions • Socket.h We usually use this prtocol If you have played starcraft, you know this protocol

  7. 0x421 Socket Functions • Socket.h almost always be 0

  8. 0x422 Socket Addresses • If you dedicate a Protocol family, Address family will automatically determined

  9. 0x422 Socket Addresses • sa = Socket Address

  10. 0x422 Socket Addresses • Internet

  11. 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.

  12. 0x424 Internet Address Conversion • xxx.xxx.xxx.xxx

  13. 0x425 A Simple Server Example • Hacking.h

  14. 0x425 A Simple Server Example • Simple_server.c

  15. 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 일 경우 옵션의 하나 - 이미 사용하고 있는 어드레스를 바인드 할 수 있도록 한다.

  16. 0x425 A Simple Server Example

  17. 0x426 A Web Client Example • HTTP • Get • Head

  18. 0x426 A Web Client Example • Hacking-network.h

  19. 0x426 A Web Client Example • Host_lookup.c

  20. 0x427 A Tinyweb Server • Tinyweb.c • 127.0.0.1

  21. 0x427 A Tinyweb Server • Tinyweb.c • 127.0.0.1

  22. 0x427 A Tinyweb Server

  23. 0x427 A Tinyweb Server

More Related