Coding Corner Networking Edition - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Coding Corner Networking Edition

Description:

... developed the OSI (Open Systems Interconnection) abstraction ... 'Socket' abstraction. Connection metaphor: Create/destroy steps. Succeed 100% or fail ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 20
Provided by: law82
Category:

less

Transcript and Presenter's Notes

Title: Coding Corner Networking Edition


1
Coding Corner Networking Edition
  • with Luke Wagner
  • Your mom circulates like a public key,Servicing
    more requests than HTTP.She keeps all her ports
    open like Windows ME,Oh, there's so much drama
    in the PhD
  • So Much Drama in the PhD by Monzy

2
Overview
  • Goal
  • send Hello, World! from one computer to another
  • Communication protocols
  • Socket abstraction
  • WinSock2 API
  • NetUtil abstraction
  • HelloWorld.exe

3
Connections and protocol
  • Need a protocol both computers understand
  • In the field of telecommunications, a
    communications protocol is the set of standard
    rules for data representation, signalling,
    authentication, and error detection required to
    send information over a communications channel.
  • Communications protocol, Wikipedia
  • We could reinvent the wheel
  • So many protocols
  • Null modem
  • IPX
  • TCP/IP
  • HTTP
  • What is the difference?

4
The OSI Stack
  • The ISO (International Organization for
    Standardization) developed the OSI (Open Systems
    Interconnection) abstraction

HelloWorld.exe
WAN, connection, errors from Network
WAN, single packet
LAN, catch/correct Physical errors
On the wire
5
The OSI Stack
abstracted below the line
Hello, World!
Hello, World!
Hello, World!
2 packets 1Hello, 2World!
127.0.0.12 packets 127.0.0.11Hello,
127.0.0.12World!
0011ff127.0.0.12 packets 111ccf127.0.0.11Hello
, ffcc100127.0.0.12World!
raise voltage, wait 10 nanoseconds, lower
voltage...
6
TCP/IP with sockets
  • Socket abstraction
  • Connection metaphor
  • Create/destroy steps
  • Succeed 100 or fail
  • String of bytes go in, string of bytes come out
  • WinSock2
  • Microsoft Windows network stack C API
  • implements Berkeley Socket API (1983), with
    non-standard extensions of course

7
Creating a connection
  • (1969-2005) Make the internet
  • main() run A and B, create a socket on each

A
B
internet
8
Creating a connection
  • (1969-2005) Make the internet
  • main() run A and B, create a socket on each
  • B binds its socket to a port, and listens on it

listening
A
B
internet
1
9
Creating a connection
  • (1969-2005) Make the internet
  • main() run A and B, create a socket on each
  • B binds its socket to a port, and listens on it
  • A connects, using Bs address, and the same port

connecting
listening
A
B
internet
1
10
Creating a connection
  • (1969-2005) Make the internet
  • main() run A and B, create a socket on each
  • B binds its socket to a port, and listens on it
  • A connects, using Bs address, and the same port
  • B observes a pending connection and accepts.This
    creates a new socket on B.The listening socket
    keeps listening for new connections.

connected
listening
B
A
internet
1
connected
11
Using a connection
  • Send a sequence of bytes
  • send(SOCKET, const char send_buf, int len,
    int flags)
  • Receive a sequence of bytes
  • recv(SOCKET, char recv_buf, int len, int
    flags)
  • Receive in packets, not entire send() message.

12
Berkeley Sockets / WinSock2
  • 11 mapping from socket conceptual operation to
    function call
  • socket(), bind(), listen(), connect()
  • accept(), send(), recv()
  • C API
  • Direct memory manipulation, seg faults abound
  • Every function has like 10 error return values

13
NetUtil toolkit
  • Object oriented socket wrapper
  • http//students.cs.tamu.edu/edu/law1521/StuffFolde
    r/Libraries/NetUtil
  • 3 classes
  • ListenerSocket
  • MessageSocket
  • Message

14
Server, part 1
  • include "NetUtil.h"
  • include ltiostreamgt
  • int main() try
  • // bind a socket on port 10001, and start
    listening
  • NetUtilListenerSocket listener(10001)
  • catch(stdexception e)
  • // catch any errors that may occur (e.g. someone
    already listening)
  • stdcout ltlt e.what() ltlt "\nProgram exiting" ltlt
    stdendl

15
Client, part 1
  • include "NetUtil.h"
  • include ltiostreamgt
  • int main() try
  • stdcout ltlt "Enter address (form x.x.x.x
    port) "
  • stdstring addr
  • int port 0
  • stdcin gtgt addr gtgt port
  • // connect to addrport and wait until accepted
  • NetUtilMessageSocket sock(addr, port, true)
  • catch(stdexception e)
  • stdcout ltlt e.what() ltlt "\nProgram exiting" ltlt
    stdendl

16
Server, part 2
  • include "NetUtil.h"
  • include ltiostreamgt
  • int main() try
  • NetUtilListenerSocket listener(10001)
  • // create a socket ready to receive the incoming
    connection
  • NetUtilMessageSocket sock
  • // wait for the connection to arrive, and then
    accept it
  • listener.accept_connection(sock, true)
  • // wait for a message to arrive
  • while(!sock.message_remain())
  • sock.process()
  • Sleep(100)
  • catch(stdexception e)

17
Client, part 2
  • include "NetUtil.h"
  • include ltiostreamgt
  • int main() try
  • stdcout ltlt "Enter IP address (form x.x.x.x)
    "
  • string s
  • stdcin gtgt s
  • NetUtilMessageSocket sock(s.c_str(), 10001,
    true)
  • // create message to send
  • NetUtilMessage outgoing
  • outgoing.msg() ltlt Hello, World!"
  • // place it in the outgoing queue
  • sock.send_message(outgoing)
  • // perform send/receive
  • sock.process()
  • catch(stdexception e)

18
Server, part 3
  • include "NetUtil.h"
  • include ltiostreamgt
  • int main() try
  • NetUtilListenerSocket listener(10001)
  • NetUtilMessageSocket sock
  • listener.accept_connection(sock, true)
  • while(!sock.message_remain())
  • sock.process()
  • Sleep(100)
  • // create an empty message
  • NetUtilMessage incoming
  • // fill the message with whatever we just
    received
  • sock.take_message(incoming)
  • stdcout ltlt incoming.msg().str()

19
Moving forward
  • Blocking vs. Non-blocking
  • Connectionless UDP
  • Good for low latency, high bandwidth games
  • Beejs Guide to Network Programming
  • http//beej.us/guide/bgnet/
  • Lightly funny. fork() takes no arguments, and
    CreateProcess() takes about 48 billion arguments
Write a Comment
User Comments (0)
About PowerShow.com