Transcript TCP Socket
Networking
• OSI (Open Systems Interconnection) model of computer
networking, seven layers (the Application, Presentation,
Session, Transport, Network, Data Link, and Physical
Layers)
• Internet Protocol Stack, five layers (the Application,
Transport, Network, Data Link, and Physical Layers)
application
process
socket
application
process
transport
transport
network
network
link
physical
Internet
link
physical
controlled by
app developer
controlled
by OS
Networking
• TCP/IP (Transmission Control Protocol/Internet Protocol)
– reliable, byte stream-oriented (reliable transfer of bytes from one
process to another)
• UDP/IP (User Datagram Protocol)
– unreliable datagram
• Connection model: connection-oriented and connectionless
communication
Socket : a host-local, application-created, OS-controlled interface
into which, application process can both send and receive messages
to/from another application process
Socket programming with TCP
client must contact server
• server process must first be
running
• server must have created
socket that welcomes
client’s contact
client contacts server by:
• Creating TCP socket,
specifying IP address, port
number of server process
• when client creates socket:
client TCP establishes
connection to server TCP
• when contacted by client,
server TCP creates new
socket for server process to
communicate with that
particular client
– allows server to talk with
multiple clients
– source port numbers used
to distinguish clients
application viewpoint:
TCP provides reliable, in-order
byte-stream transfer (“pipe”)
between client and server
TCP Socket Communications
Client
Server
Listener
Socket
(Thread)
Connect
Client
Socket
(Thread)
Accept
Communicate
Server
Socket 1
(Thread)
Server
Socket 2
(Thread)
Server
Socket i
(Thread)
TCP Sockets in C++/CLI
Client
Server
•
•
•
•
•
TcpListener ^listener = gcnew
TcpListener(IPAddress::Any, 12345);
listener->Start();
Socket connection = listener>AcceptSocket();
NetworkStream ^socketStream =
gcnew NetworkStream( connection );
BinaryWriter ^writer = gcnew
BinaryWriter( socketStream );
BinaryReader ^reader = gcnew
BinaryReader( socketStream );
reader->ReadString() and writer>Write();
writer->Close(); reader->Close();
socketStream->Close(); connection>Close();
•
•
•
•
•
TcpClient ^client = gcnew
TcpClient(); client->Connect(
L"localhost", 12345 )
NetworkStream ^clientStream =
gcnew NetworkStream( client );
BinaryWriter ^writer = gcnew
BinaryWriter( clientStream );
BinaryReader ^reader = gcnew
BinaryReader( clientStream );
reader->ReadString() and writer>Write();
writer->Close(); reader->Close();
clientStream->Close(); client>Close();
Client/server socket interaction: TCP
Server (running on hostid)
Client
create listener socket,
port=x, for
incoming request:
TcpListener ^listener = gcnew
TcpListener(IPAddress::Any, x);
listener->Start();
wait for incoming
TCP
connection request
connection setup
Socket connection =
listener->AcceptSocket();
read request from socket
connection
write reply to socket
connection
Close socket
connection
create socket,
connect to hostid, port=x
TcpClient ^client = gcnew TcpClient();
client->Connect(hostid, x);
send request using
Socket client
read reply from
socket client
Close socket
client
An Example