Network Programming CSE 132
iClicker/WUTexter Question How many of the following statements are true? In the model-view-controller paradigm, a swing JSlider would be considered part of the model. A Java int is stored in 4 bytes. If the least significant bit of a two’s complement number is 1, then the value is negative. A Java double can be read atomically without the protection of a lock A: 0 B: 1 C: 2 D: 3 E: 4
Network Programming vs. Threads Threads Multiple independent execution sequences All on the same computer Utilize common memory for communication Read/write shared objects Network Programming Multiple independent execution sequences Across multiple computers Utilize network for communication Send/receive messages
Internet
Low-level Protocols Internet Protocol (IP) – Datagram transmission – Best effort delivery – Can be reordered Transmission Control Protocol (TCP) – Reliable stream built on IP – Uses sequence numbers for datagram ordering – Retransmits if necessary Combination known as TCP/IP
Addressing on the Internet Internet Protocol (IP) has “unique” ID for each machine on the network (at least in principal) IPv4 uses 32-bit (4-byte) address that is written as follows: a.b.c.d – where a, b, c, and d represent bytes with values between 0 and 255: IPv6 uses 128-bit addresses for greater addressing range
Domain Name Service (DNS) Lookup service for names – Translate domain name into IP address –
Internet
Sockets Stream abstraction for network communication Once established, use stream wrappers as with file I/O Client (sender) Server (receiver) socket TCP stream
Client Side Socket s = new Socket(“localhost”,10420); // “localhost” is short for this machine, alternative // is to provide IP address or domain name // Second parameter is port # (use 10,000 to 30,000) DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeInt(4); DataInputStream dis = new DataInputStream(s.getInputStream()); int inpValue = dis.readInt();
ServerSocket
Server Side ServerSocket ss = new ServerSocket(10420); // port while (----) { Socket s = ss.accept(); DataInputStream dis = new DataInputStream(s.getInputStream()); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); while (----) { int inputValue = dis.readInt(); dos.writeInt(inputValue + 1); }
Protocol Design Knock-knock Programmer’s responsibility for common interpretation of bytes at sender and receiver – writeInt() sends 4-byte integer – readInt() receives 4-byte integer – writeByte() send 1 byte – readByte() receives 1 byte Recall dumpster from studio 2 We’ll explore this in studio 6 (next week)
iClicker/WUTexter Question I can invoke writeInt() on what type of object? A.FileOutputStream B.OutputStream C.DataOutputStream D.FileInputStream E.DataInputStream