Sockets Programming - Stanford University

2y ago
2 Views
1 Downloads
235.18 KB
14 Pages
Last View : 1d ago
Last Download : 3m ago
Upload by : Mia Martinelli
Transcription

Sockets ProgrammingCS144 Review Session 1April 4, 2008Ben 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 youcan’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 SCShomepage– ./sc www.scs.stanford.edu 80 "GET /"

Sockets and TCP/IPApplicaNonSockets PortPortIPIP AddressLinkHardware Interface In TCP/IP:– Endpoint has unique (TCPport, IP address) pair– ConnecNon between twoendpoints is idenNfied bythe pair [(IP, port)src, (IP,port)dst] All Unix I/O streams arereferenced by descriptors– Socket maps a descriptorto an endpoint– ConnecNng sockets allowsus to connect endpointsand do I/O

Socket API for Clientsocketbindconnectint socket(int domain, int type, int protocol) Returns a descriptor associated with a new endpointint bind(int sd, struct sockaddr *addr,u int addr len) Set addr/port of endpoint for socket descriptor sdOpNonal for client (lets the kernel choose some availableport with the default IP address)int connect(int sd, struct sockaddr *addr,u int addr len) send/recvint send(int sd, void *buf, int len, int flags)int recv(int sd, void *buf, int len, int flags) shutdownConnect to desNnaNon address port endpointTwo‐way communicaNonint shutdown(int sd, int how) ParNal or complete connecNon teardown

Sockets API for Serverint socket(int, int, int)int bind(int, struct sockaddr *,u int)socketbindint listen(int sd, int backlog) Wait for a client to connect to this portlistenint accept(int sd, struct sockaddr*addr, u int *addr len) Accept connecNon, returning a new descriptorfor this (IP, port)src – (IP, port)dst pairacceptint send(int, void *, int)int recv(int, void *, int)send/recvint shutdown(int, int)shutdown

ClientTypical Request‐ResponseClient‐Server PairServersocketsocketbindbind"Can I connect?""Yes. Can I connect?"listenconnect"Yes."acceptRequest: "GET /"send/recvshutdownsend/recvOne side starts shutdownshutdown

ClientTypical Request‐ResponseClient‐Server ectACKacceptRequest: "GET /"send/recvshutdownsend/recvOne side starts shutdownshutdown

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() orfpring(stderr, strerror(errno)) to print out aninformaNve error message– Doesn’t handle mulNple clients simultaneously– Problems with re‐using the same port (usesetsockopt—covered in next review session)

Main Structures Generic socket addressstruct sockaddr {u short sa family;char sa data[14];}; TCP/UDP IPv4 specific address – convenience parallel struct for sockaddrstruct sockaddr in {u short sa family;// usually AF INETu short sin port;struct in addr sin addr; // see belowchar sin zero[8];// zero out}; IP Addressstruct in addr {u long s addr;};// Network byte orderNext two slides by Clay Collier

Useful FuncNonsstruct hostent *gethostbyname(const char *name) Converts domain names and dojed‐quad IP addresses intonumerical IP addresses via DNSstruct servent *getservbyname(const char *name,const char *proto) Query /etc/services to find expected protocol and port for aservice Example: getservbyname("hjp", NULL) to find it resides ontcp port 80getsockname(.), 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 encodedusing htons/htonl– All shorts/ints being read from the wire must bedecoded using ntohs/ntohl– Why don’t we have to worry about endianness fortext? For arbitrary binary data?

Grading No set rubric yet Mostly funcNonality– Test yourself by sending (for example) hjprequests 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 Office hours

Sockets and TCP/IP In TCP/IP: – Endpoint

Related Documents:

SEISMIC: A Self-Exciting Point Process Model for Predicting Tweet Popularity Qingyuan Zhao Stanford University qyzhao@stanford.edu Murat A. Erdogdu Stanford University erdogdu@stanford.edu Hera Y. He Stanford University yhe1@stanford.edu Anand Rajaraman Stanford University anand@cs.stanford.edu Jure Leskovec Stanford University jure@cs.stanford .

Computer Science Stanford University ymaniyar@stanford.edu Madhu Karra Computer Science Stanford University mkarra@stanford.edu Arvind Subramanian Computer Science Stanford University arvindvs@stanford.edu 1 Problem Description Most existing COVID-19 tests use nasal swabs and a polymerase chain reaction to detect the virus in a sample. We aim to

Domain Adversarial Training for QA Systems Stanford CS224N Default Project Mentor: Gita Krishna Danny Schwartz Brynne Hurst Grace Wang Stanford University Stanford University Stanford University deschwa2@stanford.edu brynnemh@stanford.edu gracenol@stanford.edu Abstract In this project, we exa

Stanford University Stanford, CA 94305 bowang@stanford.edu Min Liu Department of Statistics Stanford University Stanford, CA 94305 liumin@stanford.edu Abstract Sentiment analysis is an important task in natural language understanding and has a wide range of real-world applications. The typical sentiment analysis focus on

Shunted sockets. SuperDim ballasts require non-shunted sockets, also known as rapid-start or program-start sockets. Inspect or test lamp holders (sockets). With lamp removed and ballast disconnected, a shunted socket will always present a short circuit (zero ohms) between connections. Non

Beej’s Guide to Network Programming Using Internet Sockets 4 2. What is a socket? You hear talk of “sockets” all the time, and perhaps you are wondering just what they are exactly. Well, they’re this: a way to speak to other programs using standard Unix file descriptors.

Stanford Health Care Organizational Overview 3 Contract Administration is a Shared Service of Stanford Health Care to Eight Other Stanford Medicine Entities Stanford Health are ("SH")is the flagship academic medical center associated with the Stanford University School of Medicine. SHC has 15,232 employees and volunteers, 613 licensed

Mar 16, 2021 · undergraduate and graduate students, faculty, staff, and members of the community. Anyone interested in auditioning for the Stanford Philharmonia, Stanford Symphony Orchestra, or Stanford Summer Symphony should contact Orchestra Administrator Adriana Ramírez Mirabal at orchestra@stanford.edu. For further information, visit orchestra.stanford.edu.