Socket Programming Part 1 ZhuZhu 07302010096@fudan.edu.cn Reference: Daniel Spangenberger 15-441 Computer Networks, Fall 2007 PPT-4 Socket Programming
Zhu Zhu 07302010096@fudan.edu.cn Reference: Daniel Spangenberger 15-441 Computer Networks, Fall 2007 PPT-4 Socket Programming
Why Do I Want Networking? Goal of Networking: Communication Share data Pass Messages Say I want to talk to a friend in Singapore How can i do this? What applications and services must I use Where can I access them? How will the data get there? Will it be reliable?
Goal of Networking: Communication Share data Pass Messages Say I want to talk to a friend in Singapore… How can I do this? What applications and services must I use? Where can I access them? How will the data get there? Will it be reliable?
Lecture Today. o Motivations for sockets o What's in a socket? o Working with Sockets Concurrent Network Applications
Motivations for Sockets What’s in a Socket? Working with Sockets Concurrent Network Applications
Layered Commuication Application Application Application Presentation Presentation End-to-end Transparency Session Session CPI Transport UDP Transport Network P Network Network Data Link 8023 Data Link WiFi Data Link sica Physical Physical Core Network
Application Presentation Session Transport Network Data Link Physical Application Presentation Session Transport Network Data Link Physical Network Data Link Physical Core Network End-to-end Transparency Application IP IP TCP/ UDP 802.3 WiFi
What's really going on.. Lets consider project one. Client(mIRO) (IRO 128.2194242:6262 TCP 128.2237.25:6667 Which Is IP Packet IP Packet IP Packet IP Packet 1 # Which Is Ethernet Ethernet Ethe Ethe Frame #1 Frame #2 Frame Frame#4
Let’s consider project one… Client (mIRC) 128.2.194.242:6262 Server (IRC) 128.2.237.25:6667 TCP IP Packet #1 IP Packet #2 IP Packet #3 IP Packet #4 Ethernet Frame #1 Ethernet Frame #2 Ethernet Frame #3 Ethernet Frame #4 Which Is also… Which Is also…
Which is easier? An application programmer(writing an IRC server) Doesn't need to send iP packets Doesn't need to send ethernet frames Doesn't need to worry about reliability Shouldn't have to Sockets do this! TCP streams UDP packetized service(Project 2) You'll be doing this! (using sockets) To share data To pass messages
An application programmer (writing an IRC server) Doesn’t need to send IP packets Doesn’t need to send Ethernet frames Doesn’t need to worry about reliability Shouldn’t have to! Sockets do this! TCP streams UDP packetized service (Project 2) You’ll be doing this! (using sockets) To share data To pass messages
What's in a Socket Some information needed Where is the remote machine? IP Address Hostname (resolved to IP) Which service do i want? Port ● After that You get a file! A plain old file As simple as other Unix I/O Don't forget to close it when you're done
Some information needed… Where is the remote machine? IP Address Hostname (resolved to IP) Which service do I want? Port After that… You get a file! A plain old file! As simple as other Unix I/O Don’t forget to close it when you’re done!
How do i do it? Request a socket descriptor Both the client and the server need to Bunch of kernel allocations e And the server Binds to a port I am offering a service on port x. hear me roar Listens to the socket a“Hey! Say something: Accepts the incoming connection Good, you spoke up!” ● And the client Connects “ I'm interested!
Request a socket descriptor Both the client and the server need to Bunch of kernel allocations… And the server… Binds to a port “I am offering a service on port x. Hear me roar” Listens to the socket “Hey! Say something!” Accepts the incoming connection “Good, you spoke up!” And the client… Connects “I’m interested!
Sockets: The lifecycle Client Server socket socket o bind( listen( connect( Connection Request accept( write( read( Client/ Server Session read( write() close() EOF reado close()
Client Server socket() connect() write() read() close() socket() bind() listen() accept() write() read() close() EOF read() Connection Request Client / Server Session
Step One: Socket-time Both the client and server need to setup the socket int socket (int domain, int type, int protocol) ● Domain AF INET(IPv4, also IPv6 available) Type SOCK STREAM TCP SOCK DGRAM UDP ● Protoco 0 int sockfd socket(AF INET, SOCK STREAM, 0)i
Both the client and server need to setup the socket int socket(int domain, int type, int protocol) Domain AF_INET (IPv4, also IPv6 available) Type SOCK_STREAM TCP SOCK_DGRAM UDP Protocol 0 int sockfd = socket(AF_INET, SOCK_STREAM, 0);