internet applications and network programming dr. abraham professor utpa
TRANSCRIPT
Server Program
• Create a socket with the socket() system call• Bind the socket to an address using the bind() system
call. For a server socket on the Internet, an address consists of a port number on the host machine. Each application should use a unique port.
• Listen for connections with the listen() system call• Accept a connection with the accept() system call. This
call typically blocks until a client connects with the server.
• Send and receive data
Client Program
• Create a socket with the socket() system call• Connect the socket to the address of the
server using the connect() system call• Send and receive data. There are a number of
ways to do this, but the simplest is to use the read() and write() system calls.
Connection of Oriented Communication (TCP)
• Stream service• First connection must be established• Data may be send in either direction• Finish communication• Applications request connection terminated.
Client-server model of interaction
• The server application starts first and waits for client to connect
• Client starts next and initiates connection
Server application
• Starts first• Does not need to know which client will
contact it (so need to know client IP)• Waits passively• Communicates with a client by sending and
receiving data• When one client finished, waits for another.
Client application
• Starts second• Must know the IP of the intended server. • Initiates contact with the server when
communication is needed.• Sends and receives data between server and
the client• May terminate after interacting with the
server or contact another server
Server Identification
• Identify Computer– IP address, or use DNS to obtain IP
• Identify Process– 16 bit protocol port number.– Well known or ephemeral
SOME WELL KNOWN PORTS
SERVICE PORT
HTTP 80
POP3 110
SMTP 25
TELNET 23
FTP 21,20
FINGER 79
LOCAL LOOPS 0
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
TCP or UDP
• THE TRANSPORT PROTOCOL CAN USE EITHER TCP OR UDP
• PROGRAMMER NEEDS TO SPECIFY WHICH IS BEING USED
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.
.
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
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(); }}
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.
Another Program
using 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(); } }
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