network programming by j. h. wang nov. 6, 2013. outline introduction to network programming socket...

31
Network Programming By J. H. Wang Nov. 6, 2013

Upload: lucinda-clark

Post on 02-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Network Programming

By J. H. WangNov. 6, 2013

Page 2: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Outline

• Introduction to network programming• Socket programming

– BSD Socket– WinSock– Java Socket

• Exercises

Page 3: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Introduction

• Internet– TCP/IP protocol stack

• Client-server model– What do you need: client or server?

• Programming experience– C, C++, Java, …

• Tools needed– C/C++/Java Compiler– Packet sniffers

Page 4: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

What’s a Socket

• Source IP, port• Destination IP, port• Protocol

– TCP– UDP

• Connection method– Connection-oriented: SOCK_STREAM– Connectionless: SOCK_DGRAM

Page 5: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Socket Programming

• UNIX BSD socket (in C)• Windows socket (in Dev-C++)• Java socket API• Perl, Python, …

Page 6: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Berkeley Socket

• General functions– open(), connect(), close(),

send(), recv(), …

• Functions for servers– bind(), listen(), accept()– select(), FD_SET, FD_CLR, FD_ISSET, …

Page 7: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example BSD Socket Program – a simple client (1/2)

• #include <sys/socket.h> #include <netinet/in.h> // for struct sockaddr_in#include <netdb.h> // for gethostbyname()

struct sockaddr_in dest;struct in_addr in;struct hostent *phe;char host[1000];int fd, port = 80;

dest.sin_family=AF_INET;dest.sin_port = htons(port);if (phe = gethostbyname(host)) bcopy(phe->h_addr, (char *)&dest.sin_addr, phe->h_length);

Page 8: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example BSD Socket Program – a simple client (2/2)

–fd = socket(PF_INET, SOCK_STREAM, 0);in.s_addr = dest.sin_addr.s_addr;connect(fd, (struct sockaddr *)&dest, sizeof(dest));send(fd, msg, strlen(msg), 0);…recv(fd, buf, sizeof(buf), 0);…close(fd);

Page 9: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Windows Socket

• WSAStartup(), WSACleanup()• socket(), connect(), close()• send(), recv()• …

Page 10: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example WinSock Client (1/2)

• #include <winsock.h>

SOCKET fd;SOCKADDR_IN dest;WSADATA WSAdata;struct hostent FAR *ent;int port = 80;

WSAStartup(0x1010, &WSAdata);fd = socket(PF_INET, SOCK_STREAM, 0);dest.sin_family = PF_INET;dest.sin_port = htons(port);ent = gethostbyname(hostname);

Page 11: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example WinSock Client (2/2)

–connect(fd, (LPSOCKADDR)&dest, sizeof(SOCKADDR));send(fd, msg, strlen(msg), 0);…recv(fd, buf, sizeof(buf), 0);…close(fd);WSACleanup();

Page 12: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Java Socket

• java.net.Socket– new– getInputStream– getOutputStream– close

• java.net.ServerSocket– new– accept– close

Page 13: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example Java Socket Client (1/2)

• import java.net.Socket;import java.net.InetSocketAddress;import java.io.BufferedOutputStream;import java.io.BufferedInputStream;public class SockClient { private String address = hostname; private int port = 80;

public SockClient() { Socket client = new Socket(); InetSocketAddress isa = new InetSocketAddress(this.address, this.port);

Page 14: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example Java Socket Client (2/2)

– try { client.connect(isa, 10000); BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream()); BufferedInputStream out = new BufferedInputStream(client.getInputStream());out.write(…);in.read(...);in.close();out.close();client.close();} catch () {}

Page 15: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Response Parsing

• Protocol dependent– browser

• HTTP over TCP: connection-oriented

• Byte ordering• Buffer management

Page 16: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Some Protocols in Web Programming

• HTTP • HTML• JSON (JavaScript Object Notation)• XML

Page 17: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

HTTP

• HyperText Transfer Protocol– The protocol standard for the World Wide

Web– Defined by IETF, W3C– RFC 2616, HTTP 1.1

• Overview– Web browser (the HTTP client) submits an

HTTP request to the HTTP server, which provides resources (such as HTML files) and returns a response

Page 18: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

HTTP Request Message

• Request line: – GET /images/logo.png HTTP/1.1

• Headers – Accept-Language: en

• Empty line• (Each line must end with

<CR><LF>)• Optional message body

Page 19: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Request Methods

• 9 methods indicating the action to be performed on the identified resource– GET– POST– PUT– DELETE– HEAD, TRACE, OPTIONS, CONNECT,

PATCH

Page 20: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Some HTTP Header Fields

• Requests– Accept-Charset– Connection– Content-Type– Content-Length– Date– From – Host

• Responses– Connection– Content-Type– Content-Length– Date

Page 21: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Response Message

• Status line– Status codes

• 404

– Reason phrase• Not found

• Types of status codes– 1xx informational– 2xx success– 3xx redirection– 4xx client error– 5xx server error

Page 22: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Example HTTP Session• Client request

– GET /index.html HTTP/1.1<CR><LF>– Host: www.example.com<CR><LF>– <CR><LF>

• Server response– HTTP/1.1 200 OK– Date: Mon, 23 May 2005 22:38:34 GMT– Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)– Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT– Etag: “3f80f-1b6-3e1cb03b”– Accept-Ranges: bytes– Content-Length: 438– Connection: close– Content-Type: text/html; charset=UTF-8

Page 23: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

HTML

• HyperText Markup Language: the language for Web pages– HTML 4.01 (W3C recommendation,

1999)– HTML 5 (W3C working draft, May 2011)

• Elements– Tags: <xxx> … </xxx> – Attributes – Ex: headings, paragraphs, tables, forms,

comments, …

Page 24: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example HTML File

• <html> <head> <title>Hello HTML</title> </head> <body> <p>Hello World!</p> </body> </html>

Page 25: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

JSON

• JavaScript Object Notation (JSON): a lightweight data-interchange format (http://www.json.org/ )– Easy for humans to read and write– Easy for machines to parse and generate– Based on a subset of JavaScript Programming

Language, but it’s completely language independent

• JSON is built on two structures– Name/value pairs: objects– Ordered list of values: arrays

• More details in RFC 4627

Page 26: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

JSON Examples

• { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": {"Url":"http://www.example.com/image/481989943", "Height": 125, "Width": "100" }, "IDs": [116, 943, 234, 38793] } }

Page 27: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

JSON Examples

• [ { "precision": "zip", "Latitude": 37.7668, "Longitude": -122.3959, "Address": "", "City": "SAN FRANCISCO", "State": "CA", "Zip": "94107", "Country": "US" }, { "precision": "zip", "Latitude": 37.371991, "Longitude": -122.026020, "Address": "", "City": "SUNNYVALE", "State": "CA", "Zip": "94085", "Country": "US" } ]

Page 28: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

XML

• eXtensible Markup Language: a set of rules for encoding documents in machine-readable form– XML 1.0 (W3C Recommendation)– An application profile of SGML (ISO

8879)

Page 29: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

An Example XML File

• <?xml version="1.0"?><note>    <to>Tove</to>    <from>Jani</from>    <heading>Reminder</heading>    <body>Don't forget me this weekend!</body></note>

Page 30: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Programming Exercises

• Packet sending/receiving– {Source, destination} (IP, port number)– Protocol

• A simple HTTP client– To get a Web page at any URL– To display the page content

Page 31: Network Programming By J. H. Wang Nov. 6, 2013. Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises

Thanks for Your Attention!