Internet Applications and Network Programming Dr. Abraham

39 Slides1.64 MB

Internet Applications and Network Programming Dr. Abraham Professor UTPA

Basics of socket programming Server program Client Program

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.

Communication Paradigms

Stream

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/O When 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

More on Socket Sockets are endpoints of bidirectional communication channel between processes. SOCK-STREAM is used for connection oriented protocols and SOCK-DGRAM for connectionless protocols Socket family (SF) is either IP version 4 or IP version 6

Methods Server.bind() binds IP and portnumber to socket. Server.listen() setup TCP listner Server.accept() accepts incoming connection client.connect() initiates a TCP server connection Sc.recv() receives TCP message Sc.send() transmits TCP messate Socket.gethostname() returns hostname Sendto and recvfrom are UDP methods

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 streambased 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. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Private Sub Form Load() ' Set the LocalPort property to an integer. ‘ Then invoke the Listen method. tcpServer.LocalPort 1001 tcpServer.Listen frmClient.Show ' Show the client form. End Sub Private Sub tcpServer ConnectionRequest (ByVal requestID As Long) ' Check if the control's State is closed. If not, ' close the connection before accepting the new ' connection. If tcpServer.State sckClosed Then tcpServer.Close ' Accept the request with the requestID ' parameter. tcpServer.Accept requestID End Sub Private Sub txtSendData Change() ' 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. tcpServer.SendData txtSendData.Text End Sub Private Sub tcpServer DataArrival (ByVal bytesTotal As Long) ' Declare a variable for the incoming data. ' Invoke the GetData method and set the Text ' property of a TextBox named txtOutput to ' the data. Dim strData As String tcpServer.GetData strData txtOutput.Text strData End Sub

Python program to contact Yahoo.com import socket TCP PORT 80 BUFFER SIZE 1024 client socket.socket(socket.AF INET, socket.SOCK STREAM) TCP IP socket.gethostbyname("WWW.yahoo.com") print (TCP IP) client.connect((TCP IP, TCP PORT)) client.sendall(b"GET / HTTP/1.1\r\nHost: yahoo.com\r\n\r\n") data client.recv(BUFFER SIZE) client.close() print ("RECEIVED FROM YAHOO:", data)

Output from Python program above C:\Users\nef512\PycharmProjects\test\venv\Scripts\python.exe C:/Users/nef512/PycharmProjects/test/venv/client.py 98.137.246.7 RECEIVED FROM YAHOO: b'HTTP/1.1 301 Moved Permanently\r\nDate: Wed, 06 Feb 2019 23:08:18 GMT\r\nConnection: keep-alive\r\nVia: http/1.1 media-router-fp1008.prod.media.gq1.yahoo.com (ApacheTrafficServer [c s f ])\r\nServer: ATS\r\nCache-Control: no-store, no-cache\r\nContent-Type: text/html\r\nContent-Language: en\r\nXFrame-Options: SAMEORIGIN\r\nLocation: https://yahoo.com/\r\ nContent-Length: 8\r\n\r\nredirect' Process finished with exit code 0 .

Back to top button