CNExp-8 - Understanding Socket Programming of TCP/IP Layer in java PDF

Title CNExp-8 - Understanding Socket Programming of TCP/IP Layer in java
Author SAGAR Gupta
Course Computer Network
Institution University of Mumbai
Pages 9
File Size 426.6 KB
File Type PDF
Total Downloads 55
Total Views 138

Summary

Understanding Socket Programming of TCP/IP Layer in java...


Description

Experiment No : 8 Aim: Write a Programme to implement Socket Programming using TCP or UDP. Theory: Sock etpr ogr ammi ngboi l sdownt ot wos ys t emscommuni cat i ng wi t honeanot her .Gener al l y ,net wor kcommuni cat i oncomesi nt wo flav or s:Tr ans por tCont r ol Pr ot ocol( TCP)andUserDat agr am Pr ot ocol ( UDP) .TCPandUDPar eusedf ordi ffer entpur posesandbot hhav e uni queconst r ai nt s: TCPi sr el at i v el ysi mpl eandr el i abl epr ot ocol t hatenabl esacl i ent t omak eaconnect i ont oaser v erandt het wos y st emst ocommuni cat e. I nTCP,eachent i t yknowst hati t scommuni cat i onpayl oadshav ebeen r ecei v ed.  UDPi saconnect i onl es spr ot ocolandi sgoodf orscenar i oswher e y oudonotnecess ar i l yneedev er ypack ett oar r i v eati t sdest i nat i on, suc hasmedi as t r eami ng.



Toappr eci at et hedi ffer encebet weenTCPandUDP,consi derwhat woul dhappeni fy ouwer est r eami ngv i deof r om y ourf avor i t ewebsi t eand i tdr oppedf r ames .Woul dy oupr ef ert hatt hec l i ents l owdowny ourmovi e t or ecei v et hemi ssi ngf r amesorwoul dy oupr ef ert hatt hevi deocont i nue pl ayi ng?Vi deost r eami ngpr ot ocol st ypi cal l yl ev er ageUDP.Because TCPguar ant eesdel i v er y ,i ti st hepr ot ocolofchoi cef orHTTP,FTP, SMTP,POP3,andsof or t h. I nt hi st ut or i al ,Ii nt r oducey out osock etpr ogr ammi ngi nJ av a.Ipr esenta ser i esofc l i ent s er v erex ampl est hatdemonst r at ef eat ur esf r om t he or i gi nalJ avaI / Of r amewor k,t hengr adual l yadvancet ousi ngf eat ur es i nt r oducedi nNI O. 2. [Al soonI nf oWor l d:Whati sKot l i n?TheJav aal t er nat i v eexpl ai ned] Ol dschoolJ av as ock et s I ni mpl ement at i onspr i ort oNI O,JavaTCPcl i entsock etcodei shandl ed byt hejava.net.Socket c l ass .Thef ol l owi ngcodeopensa connect i ont oaser v er : Socket socket = new Socket( server, port );

Onceoursocket i nst ancei sconnect edt ot heser v erwecanst ar t obt ai ni ngi nputandout putst r eamst ot hesev er .I nputs t r eamsar eused t or eaddat af r om t heser v erwhi l eout putst r eamsar eusedt owr i t edat a t ot heser v er .Wecanex ecut et hef ol l owi ngmet hodst oobt ai ni nputand out puts t r eams : InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); Becauset hes ear eor di nar yst r eams,t hesamest r eamst hatwewoul d uset or eadf r om andwr i t et oafil e,wecanc onv er tt hem t ot hef or mt hat bes tser v esourusecase.Forex ampl e,wec oul dwr ap t heOutputStream wi t haPrintStream sot hatwecaneasi l ywr i t e t extwi t hmet hodsl i k eprintln().Foranot herex ampl e,wec oul dwr ap t heInputStream wi t haBufferedReader,vi a anInputStreamReader,i nor dert oeasi l yr eadt extwi t hmet hods l i k ereadLine().

Figure 2.6-1: Processes communicating through TCP sockets. Now let's to a little closer look at the interaction of the client and server programs. The client has the job of initiating contact with the server. In order for the server to be able to react to the client's initial contact, the server has to be ready. This implies two things. First, the server program can not be dormant; it must be running as a process before the client attempts to initiate contact. Second, the server program must have some sort of door (i.e., socket) that welcomes some initial contact from a client (running on an arbitrary machine). Using our house/door analogy for a process/socket, we will sometimes refer to the client's initial contact as "knocking on the door".

With the server process running, the client process can initiate a TCP connection to the server. This is done in the client program by creating a socket object. When the client creates its socket object, it specifies the address of the server process, namely, the IP address of the server and the port number of the process. Upon creation of the socket object, TCP in the client initiates a three-way handshake and establishes a TCP connection with the server. The three-way handshake is completely transparent to the client and server programs. During the three-way handshake, the client process knocks on the welcoming door of the server process. When the server "hears" the knocking, it creates a new door (i.e., a new socket) that is dedicated to that particular client. In our example below, the welcoming door is a ServerSocket object that we call the welcomeSocket. When a client knocks on this door, the program invokes welcomeSocket's accept() method, which creates a new door for the client. At the end of the handshaking phase, a TCP connection exists between the client's socket and the server's new socket. Henceforth, we refer to the new socket as the server's "connection socket". From the application's perspective, the TCP connection is a direct virtual pipe between the client's socket and the server's connection socket. The client process can send arbitrary bytes into its socket; TCP guarantees that the server process will receive (through the connection socket) each byte in the order sent. Furthermore, just as people can go in and out the same door, the client process can also receive bytes from its socket and the server process can also send bytes into its connection socket. This is illustrated in Figure 2.6.2.

Figure 2.6-2: Client socket, welcoming socket and connection socket.

Because sockets play a central role in client-server applications, clientserver application development is also referred to as socket programming. Before providing our example client-server application, it is useful to discuss the notion of a stream. A stream is a flowing sequence of characters that flow into or out of a process. Each stream is either an input stream for the process or an output stream for the process. If the stream is an input stream, then it is attached to some input source for the process, such as standard input (the keyboard) or a socket into which characters flow from the Internet. If the stream is an output stream, then it is attached to some output source for the process, such as standard output (the monitor) or a socket out of which characters flow into the Internet.

Addi t i onalLear ni ng: Sockets are the way in which you can make applications communicate across network. Your browser uses socket programming. It establishes a connection with the server on port 80(only for HTTP requests) and gets data from the server and displays it according to the markup. If you had played any online games like, Counter-Strike for example, where you had been a host then you would have understood its usage. You establish yourself as a host with certain port allocated to communicate. The game then passes data using that socket to other computers. socket programming libraries gives all the utilities you need for programming a network application, without you having to reinvent the wheel. Basically you set a port on which communication should take place and then you establish the connection. After that, using I/O is almost similar to the one of standard I/O. You must adhere to the protocols if you are using a reserved port number. For example, let's assume you are building your own server and browser. For server code, you program such that it keeps a port open and takes incoming connections. These incoming connections are run as threads so that you can serve multiple

clients at a time. After sending data, you close the connection. In the client side(browser), you establish the connection to the server. You request the HTML file and display it according to the mark-up. The HTML file you requested is sent from server via sockets. You close the connection after getting the file. In server as well as client, you can keep the connection open, if more data is to be sent. Out putAnal y si s: This paper presents the source code analysis of a file reader server socket program (connection-oriented sockets) developed in Java, to illustrate the identification, impact analysis and solutions to remove important software security vulnerabilities, which if left unattended could severely impact the server running the software and also the network hosting the server. The vulnerabilities studied are: (1) Resource Injection, (2) Path Manipulation, (3) System Information Leak, and (4) Denial of Service vulnerabilities. We analyze the reason for these vulnerabilities to occur in the program, discuss the impact of leaving them unattended, and propose solutions to remove each of these vulnerabilities from the program. We also analyze any potential performance tradeoffs (such as increase in code size and loss of features) that could arise while incorporating the proposed solutions on the server program. The proposed solutions are very generic in nature, and can be suitably modified to correct any such vulnerabilities in software developed in any other programming language.

Conclusion : Network programming makes use of socket for interprocess communication between hosts where sockets act as the endpoint of the interprocess communication. Here sockets can also be termed as network socket or Internet socket since communication between computers is based on Internet protocol.

Output : Server Program : import java.net.*;

import java.io.*; public class Server { private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; public Server(int port) { try { server = new ServerSocket(port); System.out.println("Server started"); System.out.println("Waiting for a client ..."); socket = server.accept(); System.out.println("Client accepted"); in = new DataInputStream( new BufferedInputStream(socket.getInputStream())); String line = ""; while (!line.equals("Over")) { try { line = in.readUTF(); System.out.println(line); } catch(IOException i) { System.out.println(i); } } System.out.println("Closing connection"); socket.close(); in.close(); } catch(IOException i) { System.out.println(i); } }

public static void main(String args[]) { Server server = new Server(5000); } }

Client Program : import java.net.*; import java.io.*; public class Client { private Socket socket = null; private DataInputStream input = null; private DataOutputStream out = null; public Client(String address, int port) { try { socket = new Socket(address, port); System.out.println("Connected"); input = new DataInputStream(System.in); out = new DataOutputStream(socket.getOutputStream()); } catch(UnknownHostException u) { System.out.println(u); } catch(IOException i) { System.out.println(i); } String line = ""; while (!line.equals("Over")) { try { line = input.readLine(); out.writeUTF(line); } catch(IOException i) {

System.out.println(i); } } try { input.close(); out.close(); socket.close(); } catch(IOException i) { System.out.println(i); } } public static void main(String args[]) { Client client = new Client("127.0.0.1", 5000); } }

Ser v erWi ndow:

Cl i entWi ndow:...


Similar Free PDFs