Network Programming Advanced Computer Programming ClientServer Application Mc

  • Slides: 22
Download presentation
Network Programming Advanced Computer Programming Client/Server Application Mc. Graw-Hill Technology Education Copyright © 2006

Network Programming Advanced Computer Programming Client/Server Application Mc. Graw-Hill Technology Education Copyright © 2006 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Lecture Objective • After completing this Lecture: – Students will be able to understand

Lecture Objective • After completing this Lecture: – Students will be able to understand how Client/Server Applications are built – Understanding Ports – Understand how Socket Programming is done – Creating Console Application for showing debug messages – Sending and Receiving Message Streams using TCP Protocol 2

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Creating Server Console Application

Running Server Console Application

Running Server Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Creating Client Console Application

Running Client Console Application

Running Client Console Application

Running Client Console Application

Running Client Console Application

Running Server Console Application

Running Server Console Application

VB. NET TCP Client - Server Socket Communication • Here's a sample TCP Client

VB. NET TCP Client - Server Socket Communication • Here's a sample TCP Client and associated server "listener" to illustrate how easy socket programming has become in. NET. • We are keeping this synchronous and very basic; the idea is that a client accepts an input message, makes a connection to the listener on a specific address and port, sends the message, and retrieves the response. • The sockets are then closed. • These are console apps so you can easily see what's going back and forth, but it is trivial to compile the client code into a class library to allow multithreaded socket messaging, assuming that a listener exists that is equipped to handle the incoming requests correctly without serializing them. • Typically a server - listener of this type would listen on a single port but spin off separate sockets for each received message that comes in. • First, lets look at the code for a client: 17

Client Code Imports System. Net. Sockets Imports System. Text Module 1 Sub Main() Dim

Client Code Imports System. Net. Sockets Imports System. Text Module 1 Sub Main() Dim tcp. Client As New System. Net. Sockets. Tcp. Client() tcp. Client. Connect("127. 0. 0. 1", 8000) Dim network. Stream As Network. Stream = tcp. Client. Get. Stream() If network. Stream. Can. Write And network. Stream. Can. Read Then ' Do a simple write. Dim send. Bytes As [Byte]() = Encoding. ASCII. Get. Bytes("Is anybody there") network. Stream. Write(send. Bytes, 0, send. Bytes. Length) ' Read the Network. Stream into a byte buffer. Dim bytes(tcp. Client. Receive. Buffer. Size) As Byte network. Stream. Read(bytes, 0, CInt(tcp. Client. Receive. Buffer. Size))

VB. NET TCP Client - Server Socket Communication • Here we are creating a

VB. NET TCP Client - Server Socket Communication • Here we are creating a new Tcp. Client, calling its Connect method, and then getting access to its underlying Network. Stream via the Get. Stream() method. • We Write our message into the stream (converted to a byte array first) and then Read the response from the server. • When done, we close the socket. • Now lets take a look at the server side: 19

Server Code Imports System. Net. Sockets Imports System. Text Module 1 Sub Main() '

Server Code Imports System. Net. Sockets Imports System. Text Module 1 Sub Main() ' Must listen on correct port- must be same as port client wants to connect on. Const port. Number As Integer = 8000 Dim tcp. Listener As New Tcp. Listener(port. Number) tcp. Listener. Start() Console. Write. Line("Waiting for connection. . . ") Try 'Accept the pending client connection and return an initialized Tcp. Client. Dim tcp. Client As Tcp. Client = tcp. Listener. Accept. Tcp. Client() Console. Write. Line("Connection accepted. ") ' Get the stream

VB. NET TCP Client - Server Socket Communication • As it can be seen,

VB. NET TCP Client - Server Socket Communication • As it can be seen, that the server creates a new instance of the Tcp. Listener class on the port, and calls the Start() method. • It then calls the Accept. Tcp. Client() method which returns a Tcp. Client that you can use to send and receive data. • Use Tcp. Client. Get. Stream to obtain the underlying Network. Stream of the Tcp. Client. • Network. Stream inherits from Stream, which provides a rich collection of methods and properties for network communications. • We do a Read to get the sent data, and we can also call network. Stream. Write() to send back a response. • That's pretty much it for sending data over TCP sockets in. NET! Its easy, simple, and very extensible. • You can download the VB. NET solution with projects for both the Client and the Server (listener) at the CMS website. 21

The End Questions? Mc. Graw-Hill Technology Education Copyright © 2006 by The Mc. Graw-Hill

The End Questions? Mc. Graw-Hill Technology Education Copyright © 2006 by The Mc. Graw-Hill Companies, Inc. All rights reserved.