70 likes | 97 Views
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).
E N D
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 application socket controlled by app developer process process transport transport controlled by OS network network link Internet link physical physical
Networking • TCP/IP (Transmission Control Protocol/Internet Protocol) • reliable, byte stream-oriented (reliable transfer of bytesfrom 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
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: Socket programming with TCP TCP provides reliable, in-order byte-stream transfer (“pipe”) between client and server
TCP Socket Communications Client Server Listener Socket (Thread) Client Socket (Thread) Connect 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();
create socket, connect to hostid, port=x create listener socket, port=x, for incoming request: TcpClient ^client = gcnew TcpClient(); client->Connect(hostid, x); TcpListener ^listener = gcnew TcpListener(IPAddress::Any, x); listener->Start(); TCP connection setup wait for incoming connection request Socket connection = listener->AcceptSocket(); send request using Socket client read request from socket connection write reply to socket connection read reply from socket client Close socket connection Close socket client Client/server socket interaction: TCP Server (running on hostid) Client
An Example • System::Environment::Exit(System::Environment::ExitCode ); • http://msdn.microsoft.com/en-us/library/system.environment.exit%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1