Vous êtes sur la page 1sur 4

EXPERIMENT -2

AIM: Write a program to implement client server application using TCP connection, i.e. Socket and ServerSocket class (for only one client). Requirements: Computer running the Windows operating system(Window Xp,Vista or Seven) A proper connectivity of local area network(LAN)

Theory: When we write our program we are communicating with the application layer.We dont need to worry about TCP and UDP. To concern with these protocols, we can use the classes provided in the java.net package. To decide which class to use, it is important to understand difference between both type of services. TCP: It is reliable service protocol. In this system a connection is extablished before the data can be sent. In this protocol the data is guaranteed to be received at the receivers end. Example:Telephone call setup can be compared to TCP. Call(connection) is established and they voice(data) is transferred. TCP can be used in the applications where the accuracy of the data matters.Also the order or the sequence of the data matters. Example: The Services which use TCP are HTTP FTP TELNET etc. HTTP is used to read data form URL. The data must be received in the order in which it was sent or we may end up receiving jumbled HTML file or corrupted zip files or invalid data. UDP: UDP in contrast is a unreliable service protocol.It does not establish any connection. It sends independent packets of data called datagrams. Sending datagrams is much like sending letters through a postal service. In some application the strictness of reliable service is not needed. The addition of reliability may increase the over head or may invalidate the service all together. For exa: Time of day service: In this service even when the packet is lost.It makes no sense to resend that packet.Because it may contain wrong information at the time when it is being resent. ServerSocket: ServerSocket class implements server socket. This class waits for requests to come in, on the network. When request comes, it performs some specific operations and returns the result to the requestors.

Socket: Socket is sending or receiving point for datagram packets. All the datagram packets sent or received are addressed individually and may arrive in different order. UDP broadcasts are always enabled on DatagramSockets. Working: Normally a server runs on a specific computer and has a socket bound to a specific port. Server keeps on listening to that specific port for any connection requests. On client side: Client knows the address or hostname of the machine on which server is running and the port no to which the server is listening. To make a request client requests to the server on the port. For doing so client also needs an identification, so it binds itself to a local port no usually assigned by system. On server side: If everything goes well, the server accepts the connection and creates a new socket bound the port no on which the server keeps on listening further for new connections while serving the requests of the currently connected client. This way a client and server can communicate by writing to or reading from their sockets. An endpoint or a socket is a combination of a inetAddress and a port number. This way any tcp connection has two endpoints. For this working java provides class called ServerSocket and socket in java.net package

Client side:
import java.net.*; import java.io.*; public class Fileclient{ public static void main (String [] args ) throws IOException { int filesize=6022386; // filesize temporary hardcoded long start = System.currentTimeMillis(); int bytesRead; int current = 0; // localhost for testing Socket sock = new Socket("127.0.0.1",13267); System.out.println("Connecting..."); // receive file byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("amita1.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; // thanks to A. Cdiz for the bug fix do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1);

bos.write(mybytearray, 0 , current); bos.flush(); long end = System.currentTimeMillis(); System.out.println(end-start); bos.close(); sock.close(); } }

Server side:
import java.net.*; import java.io.*; public class Fileserver { public static void main (String [] args ) throws IOException { // create socket ServerSocket servsock = new ServerSocket(13267); while (true) { System.out.println("Waiting..."); Socket sock = servsock.accept(); System.out.println("Accepted connection : " + sock); // sendfile File myFile = new File ("Amit.txt"); byte [] mybytearray = new byte [(int)myFile.length()]; FileInputStream fis = new FileInputStream(myFile); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); OutputStream os = sock.getOutputStream(); System.out.println("Sending..."); os.write(mybytearray,0,mybytearray.length); os.flush(); sock.close(); } } }

Vous aimerez peut-être aussi