1. Trang chủ
  2. » Công Nghệ Thông Tin

Socket Programming in C/C++ ppt

40 512 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Socket Programming in C/C++
Tác giả Mani Radhakrishnan, Jon Solworth
Trường học University of Illinois at Chicago
Chuyên ngành Computer Science
Thể loại Ppt
Năm xuất bản 2004
Thành phố Chicago
Định dạng
Số trang 40
Dung lượng 351,49 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

accept stylesThere are basically three styles of using accept: Iterating server: Only one socket is opened at a time.. Forking server: After an accept, a child process is forked off to h

Trang 1

Socket Programming in C/C++c

September 24, 2004

Trang 3

Sockets are a protocol independent method of creating a

connection between processes Sockets can be either

I connection based or connectionless: Is a connection

established before communication or does each packet

describe the destination?

I packet based or streams based: Are there message boundaries

or is it one stream?

I reliable or unreliable Can messages be lost, duplicated,reordered, or corrupted?

Trang 4

Socket characteristics

Socket are characterized by their domain, type and transportprotocol Common domains are:

Common types are:

virtual circuit: received in order transmitted and reliablydatagram: arbitrary order, unreliable

Trang 5

Socket characteristics (cont’d)

Each socket type has one or more protocols Ex:

I TCP/IP (virtual circuits)

Trang 6

Socket APIs

(buy a phone)

can be queued for a server socket (call waiting allowance)

(answer phone)

(call)

Trang 7

Connection-based communication

Server performs the following actions

requests that can be pending for this process

(repeated)

Trang 8

TCP client

Client performs the following actions

Trang 10

is the same as a file descriptor.

two way communication with maximum message size (This isnot available on most machines.)

withindomain

Trang 11

address structure

Associates a socket id with an address to which other processescan connect In internet protocol the address is [ipNumber,portNumber]

Trang 12

When using internet sockets, the second parameter of bind (of

Trang 14

#i n c l u d e <s y s / t y p e s h>

2 #i n c l u d e <s y s / s o c k e t h>

4 i n t a c c e p t ( i n t s i d , s t r u c t s o c k a d d r ∗ a d d r P t r , i n t ∗ l e n P t r )

Returns the socketId and address of client connecting to socket

called, returns the actual value

Waits for an incoming request, and when received creates a socketfor it

Trang 15

accept styles

There are basically three styles of using accept:

Iterating server: Only one socket is opened at a time When the

processing on that connection is completed, thesocket is closed, and next connection can beaccepted

Forking server: After an accept, a child process is forked off to

handle the connection Variation: the child processesare preforked and are passed the socketId

Concurrent single server: use selectto simultaneously wait on all

open socketIds, and waking up the process only whennew data arrives

Trang 16

Pro and Con of Accept styles

I Iterating server is basically a low performance technique sinceonly one connection is open at a time

I Forking servers enable using multiple processors But theymake sharing state difficult, unless performed with threads.Threads, however present a very fragile programming

environment

I Concurrent single server: reduces context switches relative toforking processes and complexity relative to threads But doesnot benefit from multiprocessors

Trang 22

I 0-1023: These ports can only be binded to by root

I 1024-5000: well known ports

I 5001-64K-1: ephemeral ports

Trang 23

APIs for managing names and IP addresses

We next consider a number of auxiliary APIs:

presentation and strings

Trang 25

Error is return throughh errorwhich can be:

Trang 27

Network byte ordering

Network ordering in big endian (Sparc is big endian, Intel is littleendian)

Trang 28

IP Number translation

IP address strings to 32 bit number

In what follows, ’p’ stands for presentation

Hence, these routines translate between the address as a string andthe address as the number

Hence, we have 4 representations:

I IP number in host order

I IP number in network order

I Presentation (eg dotted decimal)

I Fully qualified domain name

Only the last needs an outside lookup to convert to one of theother formats

Trang 29

returns 1 if OK, 0 if presentation error, -1 error

Wherefamilyis either AF INETorAF INET6

Finally,addrPtr points to either the 32 bit result (AF INET) or

128 bit result (AF INET6)

Trang 30

returns 1 if OK, 0 if presentation error, -1 error

Where family is eitherAF INETor AF INET6

Finally,addrPtr points to either the 32 bit (AF INET) or 128 bit(AF INET6)

Length is the size of destination

Trang 31

Example: TCP/IP Server Code

Without error checking

Trang 32

Concurrent Server

To build a concurrent server:

I a fork is performed after the accept

I The child process closes listenFd, and communicates usingconnectFd

I The parent process closses connectFd, and then loops back tothe accept to wait for another connection request

Trang 33

Example: TCP/IP Client code

Trang 34

Connectionless communication

Communication is symmetric (peer-to-peer)

Trang 36

UDP variations

It is not necessary for both sockets tobind

I The receiver gets the address of the sender

It is possible for a UDP socket toconnect

I In this case,send/recv (or write/read) must be used instead

I Asynchronous errors can be returned (using ICMP)

Trang 38

from an unspecified sender.

Sender address returned inaddrPtr, of size *addrLengthPtr.Returns number of bytes receive or -1 on error

Ngày đăng: 15/03/2014, 17:20

TỪ KHÓA LIÊN QUAN

w