DOC PREVIEW
Berkeley ELENG 122 - Communications Networks Socket Programming

This preview shows page 1-2 out of 6 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

UCB Lecture 1 Questions that will be Addressed During the Lecture EECS122 Communications Networks Socket Programming What mechanisms are available for a programmer who writes network applications How to write a network application that sends packets between hosts client and server across an IP network January 30th 2003 J rn Altmann Client IP Network Server EECS122 UCB EE122 Projects S03 Socket Programming Table of Contents First assignment Implement the client side of an client server architecture C programming Handle exceptions in the communication Measure performance of the network and the server EECS122 UCB 3 Layers of the IP Protocol Suite 1 Network Application Programming Interface 2 3 4 5 6 Sockets and Internet Sockets Network Programming Tips Client Server Architecture Example Client Programming Example Server Programming Network Programmer s Mistakes EECS122 UCB Transport Layer Network Layer e g ftp e g TCP UDP e g IP 4 Protocol Suite Location Internet Protocol Layer Application Layer 2 Application Layer Application Layer Transport Layer Transport Layer TCP UDP Location Applications e g browser game ftp Application Programming Interface API e g network API Operating System e g Unix Network Layer Network Layer IP Interface to the Network Card Link Layer EECS122 UCB EECS 122 Ethernet Link Layer Link Layer 5 EECS122 UCB Network Card Device Driver e g Ethernet card 6 UCB Lecture 1 Network API Sockets Operating system provides Application Programming Interface API for network application API is defined by a set of function types data structures and constants Desirable characteristics of the network interface Simple to use n Flexible n There are different kind of sockets n DARPA Internet addresses Internet Sockets interprocess communication Unix Sockets n CCITT X 25 addresses n and many others n Unix w independent from any application w allows program to use all functionality of the network n Sockets provide mechanisms to communicate between computers across a network Standardized w allows programmer to learn once write anywhere Berkeley sockets is the most popular Internet Socket Application Programming Interface for networks is called socket n n EECS122 UCB 7 Internet Sockets EECS122 UCB 8 Types of Internet Sockets Support stream and datagram packets e g TCP UDP IP Is Similar to UNIX file I O API provides a file descriptor Different types of sockets implement different communication types stream vs datagram Type of socket stream socket n n Based on C single thread model n does not require multiple threads n n runs on Linux FreeBSD OS X Windows fed by the popularity of TCP IP n connection oriented two way communication reliable error free in order delivery can use the Transmission Control Protocol TCP e g telnet ssh http Type of socket datagram socket n n n EECS122 UCB 9 Network Programming Tips connectionless does not maintain an open connection each packet is independent can use the User Datagram Protocol UDP e g IP telephony Other types exist similar to the one above10 EECS122 UCB Byte Ordering of Integers Different CPU architectures have different byte ordering Byte Ordering Naming Addressing memory address A 1 Stored at little endian computer high order byte Integer representation 2 byte D3 F2 low order byte high order byte Stored at big endian computer EECS122 UCB EECS 122 11 memory address A EECS122 UCB low order byte 12 UCB Lecture 1 Byte Ordering Problem Byte Ordering Solution Question What would happen if two computers with different integer byte ordering communicate nAnswer w Nothing if they do not exchange integers w But If they exchange integers they would get the wrong order of bytes therefore the wrong value nExample 48 45 4C 4C 6F 00 01 Message is Hello 512 Message is sent across Network Message in Memory of of big endian Computer Processing Message in Memory of little endian Computer Processing Message is Hello 1 48 45 4C 4C 6F 00 01 EECS122 UCB 13 Network Programming Tips There are two solutions if computers with different byte ordering system want to communicate They must know the kind of architecture of the sending computer bad solution it has not been implemented n Introduction of a network byte order The functions are n uint16 t uint32 t uint16 t uint32 t htons uint16 t htonl uint32 t ntohs uint16 t ntohs uint32 t host16bitvalue host32bitvalue net16bitvalue net32bitvalue Note use for all integers short and long which are sent across the network n Including EECS122 n But UCB not port numbers for IP addresses 14 Naming and Addressing Host name Byte Ordering Naming Addressing n identifies a single host see Domain Name System slides n variable length string e g www berkeley edu n is mapped to one or more IP addresses IP Address n 32 bits not a number as dotted octets e g 10 0 0 1 n written Port number n identifies n 16 EECS122 UCB 15 Client Server Architecture an application on a host bit number EECS122 UCB 16 Simple Client Server Example response Client response Client Server socket connect send request Client requests service from server Server responds with sending service or error message to client Example Remote Procedure Call EECS122 UCB EECS 122 Server request recv close 17 EECS122 UCB Connection establishment socket bind listen accept Data request recv Data response send End of file notification recv close 18 UCB Lecture 1 Example Client Programming Getting the file descriptor Create stream socket socket Connect to server connect While still connected n n Initializing Socket int chat sock if chat sock socket AF INET SOCK STREAM IPPROTO TCP 0 perror socket printf Failed to create socket n abort send message to server send receive recv data from server and process it 1 parameter specifies protocol address family 2 parameter specifies the communication type 3 parameter specifies the protocol EECS122 UCB 19 Connecting to Server EECS122 UCB 20 Sending Packets struct sockaddr in sin struct hostent host gethostbyname argv 1 unsigned int server address unsigned long host h addr list 0 unsigned short server port atoi argv 2 memset sin 0 sizeof sin sin sin family AF INET sin sin addr s addr server address sin sin port htons server port if connect chat sock struct sockaddr sin sizeof sin 0 perror connect printf Cannot connect to server n abort EECS122 UCB 21 Receiving Packets Separating Data in a Stream Fixed length record A 0 Fixed length record B 1 2 Variable length Variable length record record C3 3 Use records data structures to partition the data stream 4


View Full Document

Berkeley ELENG 122 - Communications Networks Socket Programming

Documents in this Course
Lecture 6

Lecture 6

22 pages

Wireless

Wireless

16 pages

Links

Links

21 pages

Ethernet

Ethernet

10 pages

routing

routing

11 pages

Links

Links

7 pages

Switches

Switches

30 pages

Multicast

Multicast

36 pages

Switches

Switches

18 pages

Security

Security

16 pages

Switches

Switches

18 pages

Lecture 1

Lecture 1

56 pages

OPNET

OPNET

5 pages

Lecture 4

Lecture 4

16 pages

Ethernet

Ethernet

65 pages

Models

Models

30 pages

TCP

TCP

16 pages

Wireless

Wireless

48 pages

Load more
Download Communications Networks Socket Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Communications Networks Socket Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Communications Networks Socket Programming and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?