Pertemuan 2 Socket Introduction
description
Transcript of Pertemuan 2 Socket Introduction
![Page 1: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/1.jpg)
1
Pertemuan 2Socket Introduction
Matakuliah : T0483 / Bahasa rakitan
Tahun : 2005
Versi : 1.0
![Page 2: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/2.jpg)
2
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• menjelaskan konsep-konsep Sockets pada Unix, Linux dan Windows
![Page 3: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/3.jpg)
3
Outline Materi
• Port
• IPv4 Socket address structure
• Generic Socket address structure
• Byte Ordering function
• Byte manipulation function.
![Page 4: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/4.jpg)
4
<<PORT>>
• PORT Numbers– Each TCP/IP machine has multiple logical
communication channels called ports– Setiap saat banyak proses bisa menggunakan UDP
atau TCP. – TCP atau UDP menggunakan 16-bit positip integer
Port Number untuk proses-proses yg berbeda tsb.– Nomor Ports mulai dari 0 – 65536.– Well known Port : 0 s/d 1023 dikontrol oleh IANA
(Internet Assigned Numbers Authority) . Contoh:
![Page 5: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/5.jpg)
5
<<Port>>
• Contoh:– Port 21 untuk FTP– Port 69 untuk TFTP– Port 7 untuk echo server– Port 9 untuk discard server– Port 13 untuk day time server– Port 19 untuk chargen server
![Page 6: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/6.jpg)
6
<<Port>>
• A connection between two machines is uniquely defined by:– the protocol (TCP or UDP) – the IP address of local machine – the port number used on the local
machine – the IP address of remote machine – the port number used on the remote
machine
![Page 7: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/7.jpg)
7
<<Network API >>
• Network Application Programming Interface (Network API).– Network Application Programming Interface
(Network API) : The services provided by the operating system that provide the interface between application and protocol software
![Page 8: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/8.jpg)
8
<< Network API >>
• Network API– Generic Programming Interface.– Support for message oriented and connection
oriented communication.– Work with existing I/O services – Operating System independence.
• Generic Programming Interface– Support multiple communication protocol suites
(families).– Address (endpoint) representation
independence.– Provide special services for Client and Server ?
![Page 9: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/9.jpg)
9
<<Network API>>
• TCP/IP– TCP/IP does not include an API definition.– There are a variety of APIs for use with TCP/IP:
• Sockets• TLI, XTI• Winsock• MacTCP
Functions Needes– Specify local and remote communication endpoints– Initiate a connection– Wait for incoming connection– Send and receive data– Terminate a connection – Error handling
![Page 10: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/10.jpg)
10
<<Sockets>>
• SOCKETS– A socket is an abstract representation of a
communication endpoint.– Sockets work with Unix I/O services just like
files, pipes & FIFOs.– Sockets have special needs:
• establishing a connection• specifying communication endpoint addresses
![Page 11: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/11.jpg)
11
<<Sockets>>
![Page 12: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/12.jpg)
12
<<Sockets>>
• Creating a Socket– family specifies the protocol family (PF_INET
for TCP/IP).– type specifies the type of service
(SOCK_STREAM, SOCK_DGRAM).– protocol specifies the specific protocol
(usually 0, which means the default). Protocol yang digunakan seperti tabel dibawah.
– Menggunakan System call Socket(). Contoh :– fd = socket(AF_INET, SOCK_STREAM, 0);
![Page 13: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/13.jpg)
13
<<Sockets>>
• Protocol Family Description
![Page 14: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/14.jpg)
14
<<Sockets>>
![Page 15: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/15.jpg)
15
<< Sockets>>
• Socket() system call– The socket() system call returns a socket
descriptor (small integer) or a -1 on error.– socket() allocates resources needed for a
communication endpoint – TCP/IP requires an IP address and a port
number for each endpoint address.
![Page 16: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/16.jpg)
16
<< Sockets>>
POSIX Data Type
![Page 17: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/17.jpg)
17
<< Sockets>>
Generic Socket Address Structure• Serupa dgn void * (generic pointer type pada
ANSI C)
<sys/socket.h>
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};
![Page 18: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/18.jpg)
18
<< Sockets>>
Dimana:• sa_family specifies the address type.• sa_data specifies the address value.• socket address structure selalu di-pass “by
reference” jika sebagai argument pada socket functions.
• Contoh: int bind(int, struct sockaddr*, socklen_t)
struct sockaddr_in server; /* IP4 */
bind(sockfd, (struct sockaddr *) &server, sizeof(server));
![Page 19: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/19.jpg)
19
<< Sockets>>
• IP4 Socket Address Structurestruct sockaddr_in {
uint8_t sin_len;sa_family_t sin_family;in_port_t sin_port;
struct in_addr sin_addr; char sin_zero[8];
};struct in_addr {
in_addr_t s_addr; };– Alamat IP4 dan Nomor Port TCP atau UDP selalu
disimpan dalam stuktur network byte order.
![Page 20: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/20.jpg)
20
<< Sockets>>
Network Byte Order– Nilai yg disimpan pada “sockaddr_in” harus
dalam “network byte order”, seperti :
– sin_port : untuk menyimpan TCP atau UDP port number.
– sin_addr : untuk menyimpan 32-bit IP4 address.
![Page 21: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/21.jpg)
21
<< Sockets>>
Network Byte Order Function– Ada dua cara untuk menyimpan 2-byte data di
memori: little-endian and big-endian.– Fungsi-fungsi untuk meng-konversi dari host
byte order ke network byte order umumnya diawali dengan huruf:
• ‘h’ : host byte order • ‘n’ : network byte order• ‘s’ : short (16bit)• ‘l’ : long (32bit)
![Page 22: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/22.jpg)
22
<< Sockets>>
• Contoh Fungsi yg mengembalikan Network Byte Order :– uint16_t htons(uint16_t);– uint32_t htonl(uint32_t);
• Contoh Fungsi yg mengembalikan Host Byte Order :- uint16_t ntohs(uint16_t);
- uint32_t ntohl(uint32_t);
![Page 23: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/23.jpg)
23
<< Sockets>>
Byte Manipulation Functions#include <string.h>void bzero(void *dst, size_t nbytes);void bcopy(const void *src, void *dest, size_t nbytes);int bcmp(const void *ptr1, const void *ptr2, size_t nbytes);
Contoh:#include <string.h>#include <sys/errno.h>main(){
char x[10];int i;
for(i=0; i<10;i++) x[i]=65+i; bzero(x,4);
for(i=0;i<10;i++) printf("%c\n",x[i]);}
![Page 24: Pertemuan 2 Socket Introduction](https://reader035.fdokumen.com/reader035/viewer/2022081506/568150ee550346895dbf07b3/html5/thumbnails/24.jpg)
24
<< Sockets>>
IPv4 Address Conversion
int inet_aton( char *, struct in_addr *);
• Fungsi inet_aton() berfungsi untuk mengkonversi “ASCII dotted-decimal IP address” menjadi nilai 32-bit dalam format “network byte order”. Fungsi ini mengembalikan nilai 1 jika sukses, dan mengembalikan nilai 0 jika ada error.
char *inet_ntoa(struct in_addr);
• Fungsi inet_ntoa() berfungsi untuk mengkonversi nilai “network byte ordered” menjadi string yg berisi “ASCII dotted-decimal IP address”.