DOC PREVIEW
Stanford CS 144 - Sockets
 Programming


This preview shows page 1-2-3-4-5 out of 14 pages.

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

Unformatted text preview:

Sockets(Programming(CS144(Review(Session(1(April(4,(2008(Ben(Nham(Announcements(• New(classroom(for(lecture(– 200‐034,(MW(4:15‐5:30(• Simple(client(assignment(due(Wednesday,(4/9(• Use(newsgroup(for(general(quesNons(– su.class.cs144(– Refresh(your(newsgroup(list(from(the(server(if(you(can’t(find(it(Simple(Client(Overview(• Opens(TCP(socket(to(other(host(– Does(DNS(lookup(if(necessary(• Reads(request(from(command(line,(appending("\r\n",(and(sends(it(through(the(socket(• Echoes(response(to(stdout(• Demo:(Sending(HTTP(request(to(SCS(homepage(– ./sc(www.scs.stanford.edu(80("GET(/"(Sockets(and(TCP/IP(• In(TCP/IP:(– Endpoint(has(unique((TCP(port,(IP(address)(pair(– ConnecNon(between(two(endpoints(is(idenNfied(by(the(pair([(IP,(port)src,((IP,(port)dst](• All(Unix(I/O(streams(are(referenced(by(descriptors((– Socket(maps(a(descriptor(to(an(endpoint(– ConnecNng(sockets(allows(us(to(connect(endpoints(and(do(I/O(Process( Process(ApplicaNon(TCP/UDP(Transport(IP(Network(Hardware(Interface(Link(Sockets(API(IP(Address(Port( Port(Port(sd( sd(sd(Socket(API(for(Client(int$socket(int$domain,$int$type,$int$protocol)$• Returns(a(descriptor(associated(with(a(new(endpoint(int$bind(int$sd,$struct$sockaddr$*addr,$$u_int$addr_len)$• Set(addr/port(of(endpoint(for(socket(descriptor(sd(• OpNonal(for(client((lets(the(kernel(choose(some(available(port(with(the(default(IP(address)(int$connect(int$sd,$struct$sockaddr$*addr,$$u_int$addr_len)$• Connect(to(desNnaNon(address(+(port(endpoint(int$send(int$sd,$void$*buf,$int$len,$int$flags)$int$recv(int$sd,$void$*buf,$int$len,$int$flags)$• Two‐way(communicaNon(int$shutdown(int$sd,$int$how)$• ParNal(or(complete(connecNon(teardown(socket(bind(connect(send/recv(shutdown(Sockets(API(for(Server(int$socket(int,$int,$int)$int$bind(int,$struct$sockaddr$*,$u_int)$int$listen(int$sd,$int$backlog)$• Wait(for(a(client(to(connect(to(this(port(int$accept(int$sd,$struct$sockaddr$*addr,$u_int$*addr_len)$• Accept(connecNon,(returning(a(new(descriptor(for(this((IP,(port)src(–((IP,(port)dst(pair(int$send(int,$void$*,$int)$int$recv(int,$void$*,$int)$int$shutdown(int,$int)$socket(bind(send/recv(shutdown(listen(accept(Typical(Request‐Response(Client‐Server(Pair(socket(bind(connect(send/recv(socket(bind(send/recv(shutdown( shutdown(listen(accept(Client(Server("Can(I(connect?"("Yes.(Can(I(connect?"(Request:("GET(/"(One(side(starts(shutdown("Yes."(Typical(Request‐Response(Client‐Server(Pair(socket(bind(connect(send/recv(socket(bind(send/recv(shutdown( shutdown(listen(accept(Client(Server(SYN(SYN/ACK(Request:("GET(/"(One(side(starts(shutdown(ACK(Example(DayNme(Server(• See(the(posted(dayNme.c(• Problems(– Doesn’t(check(return(values(of(system(calls(• You(should(check(the(return(value(and(use(perror()(or(fpring(stderr,(strerror(errno))(to(print(out(an(informaNve(error(message(– Doesn’t(handle(mulNple(clients(simultaneously(– Problems(with(re‐using(the(same(port((use(setsockopt—covered(in(next(review(session)(Main(Structures(• Generic(socket(address($ struct$sockaddr${$$$u_short$sa_family;$$ $$char$sa_data[14];$$ };$• TCP/UDP(+(IPv4(specific(address(–(convenience(parallel(struct(for(sockaddr($ struct$sockaddr_in${$$ $$u_short$sa_family;$$$$$$$//$usually$AF_INET$$ $$u_short$sin_port;$$ $$struct$in_addr$sin_addr;$//$see$below$$ $$char$sin_zero[8];$$$$$$$$//$zero$out$$ };$• IP(Address(($ struct$in_addr${$$ $$u_long$s_addr;$$$$$$$$$$$//$Network$byte$order$$ };$$Next(two(slides(by(Clay(Collier(Useful(FuncNons(struct$hostent$*gethostbyname(const$char$*name)$• Converts(domain(names(and(dojed‐quad(IP(addresses(into(numerical(IP(addresses(via(DNS(struct$servent$*getservbyname(const$char$*name,$const$char$*proto)$• Query(/etc/services(to(find(expected(protocol(and(port(for(a(service(• Example:(getservbyname("hjp",(NULL)(to(find(it(resides(on(tcp(port(80(getsockname(...),$getpeername(...)$$• Gets(IP(address(and(port(for(source/desNnaNon(More(Notes(• ParNal(sends(and(receives(– Receive(might(not(return(with(all(the(bytes(requested(• Endian(issues(– All(shorts/ints(going(over(the(wire(must(be(encoded(using(htons/htonl(– All(shorts/ints(being(read(from(the(wire(must(be(decoded(using(ntohs/ntohl(– Why(don’t(we(have(to(worry(about(endianness(for(text?(For(arbitrary(binary(data?(Grading(• No(set(rubric(yet(• Mostly(funcNonality(– Test(yourself(by(sending((for(example)(hjp(requests(• Some(style(points(– Don’t(write(everything(in(main(– Handle(parNal(receives(and(other(edge(cases(– Check(return(values(of(system(calls(Resources(• IPC(tutorial(• Man(pages(• Outside(references(– Beej’s(Sockets(Tutorial:((hjp://beej.us/guide/bgnet/(– Unix%Network%Programming(by(Stevens(• Newsgroup(•


View Full Document

Stanford CS 144 - Sockets
 Programming


Documents in this Course
IP Review

IP Review

22 pages

Load more
Download Sockets
 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 Sockets
 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 Sockets
 Programming
 2 2 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?