Socket and Network Programming

30
1 Tehran Polytechnic University Socket and Network Programming By : Amir Hossein Payberah [email protected]

Transcript of Socket and Network Programming

Page 1: Socket and Network Programming

1

Tehran

Polytechnic

University

Socketand

Network Programming

By : Amir Hossein [email protected]

Page 2: Socket and Network Programming

2

Tehran

Polytechnic

University

Contents

Network Concepts Socket Related Data Structures Related System Calls and Commands

Page 3: Socket and Network Programming

3

Tehran

Polytechnic

University

Network Layers

Data Link

Network

Transport

Application

Physical Physical topology

Framing

Routing

End-to-End Connection

Page 4: Socket and Network Programming

4

Tehran

Polytechnic

University

Physical Layer It sends bits and receives bits.

Page 5: Socket and Network Programming

5

Tehran

Polytechnic

University

Data Link Layer It ensures that messages are delivered

to the proper device. It translates messages from the

Network layer into bits for physical layer to transmit. It formats the message into data frames. It adds a header containing the hardware

destination and source address.

Page 6: Socket and Network Programming

6

Tehran

Polytechnic

University

Network Layer It is responsible for routing through an

internetwork and for network addressing. It is responsible for transporting traffic between

devices that are not locally attached.

It uses software address.

netid hostid

32 bits

Page 7: Socket and Network Programming

7

Tehran

Polytechnic

University

IP Addresses

netid hostid

netid hostid

netid hostid

8 24

16 16

24 8

Class A

Class B

Class C

Page 8: Socket and Network Programming

8

Tehran

Polytechnic

University

Transport Layer Flow control

It prevents a sending host on one side of connection from overflowing the buffers in the receiving host.

Acknowledgment It guarantees the data won’t be duplicated or

lost. Windowing

It controls how much information is transferred from one end to the other.

Page 9: Socket and Network Programming

9

Tehran

Polytechnic

University

Network Connections The Transport layer provide two types

of connection: Connection-less (UDP)

It is an unreliable connection. Connection-oriented (TCP)

It handshakes before transfers information.

Page 10: Socket and Network Programming

10

Tehran

Polytechnic

University

Connection-orientedCreate

endpoint

Bind addr.

Specify queue

Wait for queue

read

write

Createendpoint

Connect toserver

write

read

Page 11: Socket and Network Programming

11

Tehran

Polytechnic

University

Connection-less

Read

Write

Createendpoint

Bind addr.

Write

Read

Createendpoint

Bind addr

Page 12: Socket and Network Programming

12

Tehran

Polytechnic

University

Port Numbers It is possible for more than one user

process at a time to be using either TCP or UDP.

This requires some method for identifying the data associated with each user process.

port

application

TCP-UDP

IP

Data Link

physical

Page 13: Socket and Network Programming

13

Tehran

Polytechnic

University

5-Tuple Association

{ protocol, src port, src addr, dst port, dst addr }

Page 14: Socket and Network Programming

14

Tehran

Polytechnic

University

Contents

Network Concepts Socket Related Data Structures Related System Calls and Commands

Page 15: Socket and Network Programming

15

Tehran

Polytechnic

University

Socket It is an interface between the

application layer and other layers.main(){

FILE *fd;fd = fopen (…);process (fd);fclose (fd);

}

main(){

int sockfd;sockfd = socket (…);process (sockfd);close (sockfd);

}

Page 16: Socket and Network Programming

16

Tehran

Polytechnic

University

Type of Sockets Stream Socket

Provide a reliable, sequenced, two-way connection.

This is use TCP Socket. Datagram Socket

A connection-less and unreliable connection. This is use UDP Socket.

Raw Socket Used for internal network protocols.

Page 17: Socket and Network Programming

17

Tehran

Polytechnic

University

Contents

Network Concepts Socket Related Data Structures Related System Calls and Commands

Page 18: Socket and Network Programming

18

Tehran

Polytechnic

University

Data Structures

Page 19: Socket and Network Programming

19

Tehran

Polytechnic

University

Contents

Network Concepts Socket Related Data Structures Related System Calls and Commands

Page 20: Socket and Network Programming

20

Tehran

Polytechnic

University

Byte Ordering Routines htons() // "Host to Network Short" htonl() // "Host to Network Long" ntohs() // "Network to Host Short" ntohl() // "Network to Host Long"

Page 21: Socket and Network Programming

21

Tehran

Polytechnic

University

Address Conversion Routines inet_addr_t inet_addr (char *cp);

Converts the Internet host address cp from numbers-and-dots notation into binary data in network byte order.

int inet_aton (char *cp, struct in_addr *inp); Converts the Internet host address cp from

numbers-and-dots notation into binary data. char *inet_ntoa (struct in_addr in);

Converts the Internet host address given in network byte order to a string in standard numbers-and-dots notation.

Page 22: Socket and Network Programming

22

Tehran

Polytechnic

University

socket System Call int socket (int family, int type, int

protocol); It creates the end point. Family:

AF_INET, AF_UNIX, … Type:

SOCK_STREAM SOCK_DGRAM SOCK_RAW

Page 23: Socket and Network Programming

23

Tehran

Polytechnic

University

bind System Call int bind (int sockfd, struct sockaddr

*addr, int addrlen); It assigns a name to an unnamed

socket.

Page 24: Socket and Network Programming

24

Tehran

Polytechnic

University

connect System Call int connect (int sockfd, struct sockaddr

*addr, int addrlen); A client use it to establish a connection

with a server.

Page 25: Socket and Network Programming

25

Tehran

Polytechnic

University

listen System Call int listen (int sockfd, int backlog); This system call is used by connection-

oriented to indicate that it is willing to receive connections.

Page 26: Socket and Network Programming

26

Tehran

Polytechnic

University

accept System Call int accept (int sockfd, struct sockaddr

*addr, int *len); An incoming calls arrive at a listening

socket, they will be queued until the server program ready to process them.

Page 27: Socket and Network Programming

27

Tehran

Polytechnic

University

send and recv System Calls

int send (int sockfd, char *buff, int len., int flag);

int sendto (int sockfd, char *buff, int len., int flag, struct sockaddr *to, int addrlen);

int recv (int sockfd, char *buff, int len., int flag);

int recvfrom (int sockfd, char *buff, int len., int flag, struct sockaddr *from, int *addrlen);

Page 28: Socket and Network Programming

28

Tehran

Polytechnic

University

Connection-orientedsocket()

bind()

listen()

accept()

recv()

send()

socket()

connect()

send()

recv()

Page 29: Socket and Network Programming

29

Tehran

Polytechnic

University

Connection-less

recvfrom()

sendto()

socket()

bind()

sendto()

recvfrom()

socket()

bind()

Page 30: Socket and Network Programming

30

Tehran

Polytechnic

University

Question?