170 likes | 335 Vues
Network Programming. By J. H. Wang Nov. 21, 2011. 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
E N D
Network Programming By J. H. Wang Nov. 21, 2011
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
What’s a Socket • Source IP, port • Destination IP, port • Protocol • TCP • UDP • Connection method • Connection-oriented: SOCK_STREAM • Connectionless: SOCK_DGRAM
Socket Programming • UNIX BSD socket (in C) • Windows socket (in Dev-C++) • Java socket API • Perl, Python, …
Berkeley Socket • General functions • open(), connect(), close(), send(), recv(), … • Functions for servers • bind(), listen(), accept() • select(), FD_SET, FD_CLR, FD_ISSET, …
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);
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);
Windows Socket • WSAStartup(), WSACleanup() • socket(), connect(), close() • send(), recv() • …
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);
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();
Java Socket • java.net.Socket • new • getInputStream • getOutputStream • close • java.net.ServerSocket • new • accept • close
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);
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 () {}
Response Parsing • Protocol dependent • browser • HTTP over TCP: connection-oriented • Byte ordering • Buffer management
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