1
Pertemuan 2Socket Introduction
Matakuliah : T0483 / Bahasa rakitan
Tahun : 2005
Versi : 1.0
2
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• menjelaskan konsep-konsep Sockets pada Unix, Linux dan Windows
3
Outline Materi
• Port
• IPv4 Socket address structure
• Generic Socket address structure
• Byte Ordering function
• Byte manipulation function.
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:
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
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
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
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 ?
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
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
11
<<Sockets>>
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);
13
<<Sockets>>
• Protocol Family Description
14
<<Sockets>>
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.
16
<< Sockets>>
POSIX Data Type
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];
};
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));
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.
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.
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)
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);
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]);}
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”.
Top Related