33
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Embed Size (px)

Citation preview

Page 1: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Dr. John P. AbrahamProfessor

University of Texas Pan American

Internet Applications and Network Programming

Page 2: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Chapter Covers

Paradigm that applications follow when communicating over the internet

Socket application programming interface – Socket API

Page 3: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Two basic Internet Communication Pardigms Stream oriented (connection oriented) Message Oriented Differentiate between the two

See p 28. fig 3.1 1 to 1 vs many to many comm Sequence of individual bytes vs.

sequence of packets Arbitrary length vs. max limit to 64k Most applications vs. multimedia Tcp vs. udp

Page 4: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Stream transport in the Internet Sequence of bytes that flows from one application

program to another. Browser uses the stream service.

browser requests to the webserver, which responds by sending the page.

Stream mechanism transfers a sequence of bytes without attaching meaning or inserting boundaries. The sending application may send one byte or a block of bytes.

Although it delivers all bytes in sequence, the steram paradigm does not guarantee that the chunks of bytes passed to a receiving application correspond to the chunks of bytes transferred by the sending application.

Page 5: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Message Transport in the Internet

The network accepts and delivers messages The network never delivers part of the

message nor joins multiple messages together.

Messages can be 1 to 1, 1 to many, or many to 1 (application on many computers can send messages to a given application).

Messages can be lost, duplicated or delivered out of order. It is up to the programmer to ensure that the application operates correctly.

Page 6: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Connection-oriented communication

Similar to a telephone call. Establish connection, talk, terminate

connection See algorithm on page 30.

Page 7: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Client Server Model of Interation

How can a pair of applications that run on two different computers coordinate? One application known as the server starts first

and awaits contact from any computer. The other application known as the client starts

second and initiate the connection. Must know which server to contact.

When the client terminates, the server waits for another connection.

See figure 3.2 p.30

Page 8: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Characteristics of clients and servers

Client – user invokes, application program through the underlying OS initiates contact with a server

Can access multiple services as needed. Keeps track of various connections through ports.

Server – handles multiple remote sessions simultaneously, therefore needs a powerful machine.

Waits passively for contact from remote clients

Page 9: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Server application model Server application starts first Does not need to know which client will

contact it Waits passively and arbitrarily long for

contact from a client Communicates with a client by both

sending and receiving data Stays running after servicing one client,

and waits of another.Requires a server class machine. It can

accept many connections.

Page 10: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Client application model

Starts after the server has started Must know which server to contact Initiate contact and then sends and

receives data May terminate after interacting with

the serverAny computer can be a client. It can do

other tasks such as computation.

Page 11: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Server Identification and Memutliplexing A client sends request to a server, the server

sends response to the client. The internet protocols divide identification

into two parts: an identifier for the computer (IP address) on which the server runs and the identifier for a particular service (protocol port number – 16bits such as 80 for web and 25 for email) on the computer.

The DNS is used to convert the computer name to an IP address.

Server uses threading to accept concurrent requests.

Page 12: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Circular Dependencies Among servers

A server can become a client for a different server (think of 3 tier systems).

Programmers must be careful to avoid circular references.

Page 13: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Network Programming and the Socket API

Interface: set of instructions designed for interaction between two entities. (You already know about file interfaces)

There are three common interfaces: Socket interface, transport layer interface and stream interface.

Socket interface is a set of instructions located between the operating system and application programs(to access TCP/IP)

Page 14: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Socket data structure

In C, a socket is defined as five-field struct (or record)

Family: IF_NET (for IPv4), IF_NET6 Type: SOCK_STREAM(FOR TCP),

SOCK_DGRAM (FOR UDP), etc. Protocol 0 is for TCP/IP Local Socket Address Remote socket Address

Page 15: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

SOCKETS PRIMER

ByDr. John P. AbrahamUniversity of Texas Pan American

Page 16: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

CLIENT/SERVER

Server side Client side Request and Reply The client and server communicate

with each other through something called Berkley socket or winsock

Socket API is a de facto standard for Internet communication

Page 17: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

SOCKET

APPLICATION PROGRAMMER’S INTERFACE (API) TO THE NETWORK (TRANSPORT LAYER)

The socket API is integrated with I/OWhen an application creates a socket to use

for Internet communication, the OS returns a small integer descriptor that identifies the socket

The application then passes the descriptor as an argument when it calls functions to perform an operation on the socket

Page 18: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

TCP or UDP

THE TRANSPORT PROTOCOL CAN USE EITHER TCP OR UDP

PROGRAMMER NEEDS TO SPECIFY WHICH IS BEING USED

Page 19: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

ADDRESSING

An application must specify address of the remote computer, the protocol port number and whether the application will act as a client or server.

SENDER AND RECEIVER MUST KNOW EACH OTHER’S ADDRESS AND PORT. IP ADDRESS OR NAME OF HOSTS PORT NUMBER

Page 20: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Summary of socket functions

See page 37 figure 3.7 This is an important figure I have given some properties and

methods two slides later

Page 21: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

PORT NUMBER

THERRE ARE 65535 TOTAL PORTS (16 BIT UNSIGNED)

PORTS BELOW 1024 ARE CALLED WELL KNOWN PORTS. YOU SHOULD STAY AWAY FROM THE

WELL KNOWN PORTS WHEN YOU WRITE APPLICATIONS UNLESS YOU ARE PROGRAMMING FOR A STANDARD SERVICE.

Page 22: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

SOME WELL KNOWN PORTS

SERVICE PORT

HTTP 80

POP3 110

SMTP 25

TELNET 23

FTP 21,20

FINGER 79

LOCAL LOOPS 0

Page 23: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Winsock propertiesBytes Received

Returns the number (long integer) of bytes currently in the receive buffer. Read-only.

LocalHost Name

Returns the name of the local host. Read only (Available at run time).

Local IP Returns IP assigned to the local machine. You may use the name of the machine.

Local Port Returns the local port number. You may set the port number with this property. Long integer.

Protocol Either TCP or UDP. You can set this.

Remote Port Set what port the remote program is using.

Page 24: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Winsock MethodsAccept Server accepts requests for connection from

the client.The listen must be running.

Close Terminates a connection.

Get Data Retrieves current block of data from the buffer places in a variable. Removes from the queue.

Peek Data Same as get Data. Does not remove data from the queue.

Listen Server side. Waits for a connection from client.

Send Data Dispatches data to remote computer.

Connect Requests a connection to the remote computer

Page 25: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Socket calls in a client and server

Client Socketconnectsendrcv (repeat

sendrcv)close

Server Socketbindlistenacceptrcvsend

(repeat send and rcv)close

Following are some description about these functions

Page 26: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Read and write with sockets

You can use read and write instead of recv and send with some os.

The only way to learn it is to do some programming

Page 27: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

C# (.NET) The .NET framework provides two

namespaces, System.Net and System.Net.Sockets for socket programming.

The communication can be either connection oriented or connectionless. They can also be either stream oriented or data-gram based.

The most widely used protocol TCP is used for stream-based communication and UDP is used for data-grams based applications. 

.

Page 28: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Discovering IP address System.Net contains the Dns class. Dns class can be used to query

information about various things including the IP addresses

Dns.GetHostByName can be used to return DNS host name of the local machine.

Here is an example of this program. You will have to write this program yourself, so I am only showing the executable program.

SocketDiscoverDnsIP - Shortcut.lnk

Package

Page 29: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Sample program in c# to resolve address given a host name

using System;using System.Net;using System.Net.Sockets;class SocketAddress

{ public static void Main() { IPHostEntry IPHost = Dns.Resolve("www.utpa.edu"); Console.WriteLine(IPHost.HostName); string []aliases = IPHost.Aliases; IPAddress[] addr = IPHost.AddressList; for(int i= 0; i < addr.Length ; i++) { Console.WriteLine(addr[i]); } Console.ReadKey(); }}

Page 30: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Explanation

IPHostEntry IPHost = Dns.Resolve("www.utpa.edu");

The Resolve method queries a DNS server for the IP address associated with a host name or IP address.

IPHost.Aliases gives any aliases associated with that host name. This can be stored in an array.

IPHost.AddressList will provide addresses associated with the hostname. They can be stored in an array.

Page 31: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Another Programusing System;using System.Net;using System.Net.Sockets;class MyClient{ public static void Main() { IPHostEntry IPHost = Dns.Resolve("www.ebay.com"); Console.WriteLine(IPHost.HostName); string[] aliases = IPHost.Aliases; Console.WriteLine(aliases.Length); IPAddress[] addr = IPHost.AddressList; Console.WriteLine(addr.Length); for (int i = 0; i < addr.Length; i++) { Console.WriteLine(addr[i]); } Console.ReadKey(); } }

Page 32: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming

Sample program (in VB)

1. Private Sub Form_Load()2. ' Set the LocalPort property to an integer. ‘ Then invoke the Listen method.3. tcpServer.LocalPort = 10014. tcpServer.Listen frmClient.Show ' Show the client form. 5. End Sub Private Sub 6. tcpServer_ConnectionRequest _ (ByVal requestID As Long)7. ' Check if the control's State is closed. If not, ' close the connection before accepting the new ' connection.8. If tcpServer.State <> sckClosed Then _ tcpServer.Close ' Accept the request with the requestID ' 9. parameter. tcpServer.Accept requestID End Sub Private Sub txtSendData_Change() 10. ' The TextBox control named txtSendData ' contains the data to be sent. Whenever the user ' types into

the  textbox, the  string is sent ' using the SendData method.11. tcpServer.SendData txtSendData.Text 12. End Sub 13. Private Sub tcpServer_DataArrival _ (ByVal bytesTotal As Long)14. ' Declare a variable for the incoming data. ' Invoke the GetData method and set the Text 15. ' property of a TextBox named txtOutput to ' the data. 16. Dim strData As String tcpServer.GetData strData txtOutput.Text = strData 17. End Sub

.

Page 33: Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming