Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Socket

Similar presentations


Presentation on theme: "Introduction to Socket"— Presentation transcript:

1 Introduction to Socket
Programming

2 CONTENTS SOCKETS SOCKET ADDRESS SOCKET SYSTEM CALLS
CONNECTIONLESS ITERATIVE SERVER UDP CLIENT-SERVER PROGRAMS CONNECTION-ORIENTED CONCURRENT SERVER TCP CLIENT-SERVER PROGRAMS

3 CONTENTS (continued) BYTE ORDERING ADDRESS TRANSFORMATION
BYTE MANIPULATION FUNCTIONS INFORMATION ABOUT REMOTE HOST

4 SOCKETS

5 What is socket Unix programs do any sort of I/O by reading or writing to a file descriptor. This logical file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. When a program wants to communicate with another program over the Internet, also do it through a file descriptor-- socket descriptor.

6 Declaration for socket function

7 Socket types

8 SOCKETS ADDRESS

9 socket address structure
// General socket address structure struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; OR // Internet address: AF_INET struct sockaddr_in { short sin_family; // 2 bytes u_short sin_port; // 2 bytes struct in_addr sin_addr; // 4 bytes char sin_zero; // 8 bytes, unused, set to 0 }

10 Internet address structure
struct in_addr { unsigned long s_addr; // that's a 32-bit long, or 4 bytes }; OR struct in_addr { union { struct { u_char s_b1, s_b2, s_b3,s_b4; }; struct { u_short s_w1, s_w2; }; u_long S_addr; } S_un; } #define s_addr S_un.S_addr

11 Internet address structure
struct sockaddr_in my_addr; my_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address By setting my_addr.sin_addr.s_addr to INADDR_ANY, you are telling it to automatically fill in the IP address of the machine the process is running on.

12 SOCKET SYSTEM CALLS

13 Declaration for bind function
sockfd is the socket file descriptor returned by socket(). localaddr is a pointer to a struct sockaddr that contains information about your address, namely, port and IP address. localaddrlen can be set to sizeof(struct sockaddr). By setting localaddr.sin_port to zero, you are telling bind() to choose the port for you.

14 IANA Port Numbers Ranges
The Internet Assigned Numbers Authority(IANA)

15 Declaration for listen function
sockfd is the usual socket file descriptor from the socket() system call. backlog is the number of connections allowed on the incoming queue.

16 Declaration for connect function
sockfd is the socket file descriptor, as returned by the socket() call. But can be used to communicate with server after successfully called connect(). serveraddr is a struct sockaddr containing the destination port and IP address. serveraddrlen can be set to sizeof(struct sockaddr).

17 Declaration for accept function
sockfd is the listen()ing socket descriptor. Easy enough. clientaddr will usually be a pointer to a local struct sockaddr_in. This is where the information about the incoming connection will go (and with it you can determine which host is calling you from which port). clientaddrlen is a local integer variable that should be set to sizeof(struct sockaddr_in) before its address is passed to accept().

18 Declaration for close function

19 Declaration for write function
Used in connection-oriented (TCP) program. sockfd is the socket descriptor you want to send data to (whether it's the one returned by socket() or the one you got with accept().) buf is a pointer to the data you want to send, and buflen is the length of that data in bytes. If the value returned by write() doesn't match the value in buflen, it's up to you to send the rest of the string. Ssize_t send(int sockfd, const void *buf, int buflen, int flags); Just set flags to 0. Set flag=MSG_OOB to send out-of-band data.

20 Declaration for read function
Used in connection-oriented (TCP) program. A similar system call ssize_t recv(int sockfd, void *buf, int len, unsigned int flags); sockfd is the socket descriptor to read from, buf is the buffer to read the information into, buflen is the maximum length of the buffer, flags can again be set to 0. Set flag=MSG_OOB to read out-of-band data.

21 Declaration for sendto function
Used in connection-less (UDP) program. toaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the destination IP address and port). toaddrlen can simply be set to sizeof(struct sockaddr).

22 Declaration for recvfrom function
Used in connection-less (UDP) program. fromaddr is a pointer to a struct sockaddr (or struct sockaddr_in which contains the IP address and port of the sender). fromaddrlen can simply be set to sizeof(struct sockaddr).

23 CONNECTIONLESS ITERATIVE
SERVER & UDP CLIENT-SERVER PROGRAMS

24 Socket interface for connectionless iterative server

25 CONNECTION-ORIENTED CONCURRENT SERVER

26 for connection-oriented
Socket interface for connection-oriented concurrent server

27 Part I

28 Part II

29 Part I Client and Server

30 Part II

31 BYTE ORDERING

32 Big-endian byte order

33 Little-endian byte order

34 The byte order for the TCP/IP protocol suite is big endian.

35 Bite-order transformation

36 Declarations for byte-order transformation
Examples: my_addr.sin_port = htons(0); // choose an unused port at random my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // use my IP address

37 ADDRESS TRANSFORMATION

38 Address transformation
Internal data in socket_address structure Predefined in program or user input

39 Declarations for address transformation functions
Example: struct sockaddr_in dest_socket_add; dest_socket_add.sin_addr.s_addr = inet_addr(" ");

40 INFORMATION ABOUT REMOTE
HOST

41 Declaration for gethostbyname
When you know the remote site’s domain name, but you don’t know its IP address.

42 Hostent structure


Download ppt "Introduction to Socket"

Similar presentations


Ads by Google