New version page

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

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

View Full Document
View Full Document

End of preview. Want to read all 6 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

UCB Lecture 1EECS 122EECS122Communications NetworksSocket ProgrammingJanuary 30th, 2003Jörn AltmannEECS122 - UCB 2Questions that will be Addressed During the LectureWhat 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?ClientServerIP NetworkEECS122 - UCB 3EE122 Projects – S03First assignment:– Implement the client side of an client-server architecture (C programming)– Handle exceptions in the communication– Measure performance of the network and the serverEECS122 - UCB 4Socket ProgrammingTable of Contents1. Network Application Programming Interface: Sockets and Internet Sockets2. Network Programming Tips3. Client-Server Architecture4. Example: Client Programming5. Example: Server Programming6. Network Programmer’s MistakesEECS122 - UCB 5Layers of the IP Protocol SuiteLink LayerTransport LayerNetwork LayerApplication LayerLink LayerTransport LayerNetwork LayerApplication LayerEthernete.g. ftpe.g. TCP, UDPe.g. IPEECS122 - UCB 6Protocol Suite Location Internet Protocol LayerLink LayerTransport Layer (TCP, UDP)Network Layer (IP)Application LayerNetwork Card &Device Driver(e.g. Ethernet card)Operating System(e.g. Unix)Applications(e.g. browser, game, ftp)Application ProgrammingInterface (API)(e.g. network API)Interface to the Network CardLocationUCB Lecture 1EECS 122EECS122 - UCB 7Network APIOperating system provides Application Programming Interface (API) for network applicationAPI is defined by a set of function types, data structures, and constantsDesirable characteristicsof the network interfacen Simple to usen Flexiblew independent from any applicationw allows program to use all functionality of the networkn Standardizedw allows programmer to learn once, write anywhereApplication Programming Interface for networks is called socketEECS122 - UCB 8SocketsSockets provide mechanisms to communicate between computers across a networkThere are different kind of socketsn DARPA Internet addresses (Internet Sockets)n Unix interprocess communication (Unix Sockets)n CCITT X.25 addressesn and many othersBerkeley sockets is the most popular Internet Socketn runs on Linux, FreeBSD, OS X, Windowsn fed by the popularity of TCP/IPEECS122 - UCB 9Internet SocketsSupport stream and datagram packets (e.g. TCP, UDP, IP)Is Similar to UNIX file I/O API (provides a file descriptor)Based on C, single thread modeln does not require multiple threadsEECS122 - UCB 10Types of Internet SocketsDifferent types of sockets implement different communication types (stream vs. datagram)Type of socket: stream socketn connection-orientedn two way communicationn reliable (error free), in order deliveryn can use the Transmission Control Protocol (TCP)n e.g. telnet, ssh, httpType of socket: datagramsocketn connectionless, does not maintain an open connection, each packet is independentn can use the User Datagram Protocol (UDP)n e.g. IP telephonyOther types exist: similar to the one aboveEECS122 - UCB 11Network Programming TipsByte Ordering NamingAddressingEECS122 - UCB 12Byte Ordering of IntegersDifferent CPU architectures have different byte orderingD3high-order byte low-order bytememoryaddress Amemoryaddress A +1Stored at little-endian computerStored at big-endian computerlow-order byte high-order byteF2Integer representation (2 byte)UCB Lecture 1EECS 122EECS122 - UCB 13Message in Memory ofof big-endian ComputerMessage is sent across Network48 45 4C 4C 6F 00 01Byte Ordering ProblemQuestion: What would happen if two computers with different integer byte ordering communicate?Message is: [Hello,1] Message is: [Hello,512] Processing48 45 4C 4C 6F 00 01Message in Memory oflittle-endian ComputerProcessingnAnswer:wNothing if they do not exchange integers!wBut: If they exchange integers, they would get the wrong order of bytes, therefore, the wrong value!nExample:EECS122 - UCB 14Byte Ordering SolutionThere are two solutions if computers with different byte ordering system want to communicaten 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:uint16_t htons(uint16_t host16bitvalue)uint32_t htonl(uint32_t host32bitvalue)uint16_t ntohs(uint16_t net16bitvalue)uint32_t ntohs(uint32_t net32bitvalue)Note: use for all integers (short and long), which are sent across the networkn Including port numbersn But not for IP addressesEECS122 - UCB 15Network Programming TipsByte OrderingNamingAddressingEECS122 - UCB 16Naming and AddressingHost namen 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 addressesIP Addressn 32 bits (not a number!)n written as dotted octets (e.g. 10.0.0.1)Port numbern identifies an application on a hostn 16 bit numberEECS122 - UCB 17Client-Server ArchitectureClient requests service from serverServer responds with sending service or error message to clientExample: Remote Procedure CallClient ServerrequestresponseEECS122 - UCB 18Simple Client-Server ExampleClient Serverrequestresponsesocket()connect()send()recv()close()socket()bind()listen()accept()recv()send()recv()close()ConnectionestablishmentData responseData requestEnd-of-file notificationUCB Lecture 1EECS 122EECS122 - UCB 19Example: Client ProgrammingCreate stream socket (socket() )Connect to server (connect() )While still connected:n send message to server (send() )n receive (recv () ) data from server and process itEECS122 - UCB 20Initializing SocketGetting the file descriptorint chat_sock;if ((chat_sock = socket( AF_INET, SOCK_STREAM,IPPROTO_TCP)) < 0) {perror("socket");printf("Failed to create socket\n");abort ();}1.parameter specifies protocol/address family2.parameter specifies the communication type3.parameter specifies the protocolEECS122 - UCB 21Connecting to Serverstruct 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 22Sending Packetsint send_packets(char *buffer, int buffer_len){sent_bytes = send(chat_sock, buffer,


View Full Document
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?