java socket tutorial rina patel

Upload: anonymous-4wg5pyu

Post on 03-Mar-2016

226 views

Category:

Documents


0 download

DESCRIPTION

java socket programming tutorial

TRANSCRIPT

1 st phase of Semester Progamming Project

1 st phase of Semester Programming ProjectBy Rina PatelDetail of Final Project The final project will require four programs running separately over a network, emulating four routers with predefined connections and route costs, that communicate with each other to update Distance Vector Routing Tables at each router, while displaying changes and resulting tables for Router 0. Options for implementationYou may choose c,c++ or javaFunctionality of 1st stage :Create a client program and a server program, communicating over a network, and passing the information.Client displays information received from server.

Resouces of Technical support -1You need to install VPN and establish a VPN connection from off-campus.Mac OS X's built-in command-line SSH and sFTP work just fine with NJITs UCS serversFor Windows use SSH Secure Shell, both in terminal mode to run commands and in file transfer mode to upload files.

5Resouces of Technical support -2Preferred Systems: afsaccess1, afsaccess2, afsaccess3 and afsaccess4Alternates: afsconnect1 and afsconnect2 These systems are heavily loaded and busy.Some pages at http://ist.njit.edu/ suggest that you use afs1 thru afs33. All the odd numbers are now redirected to afsconnect1 and all the even numbers to afsconnect2.

6Using java To compile a Java program : javac yourFilename.java To run it :java yourFilename.class

Client and server :

Client end - TCPClient.java - at the local machine Server end - TCPServer.java - on the njit afs account

Code for client.javaimport java.io.*;import java.net.*;class TCPClient {public static void main(String argv[]) throws Exception{String request;String response;BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));Socket clientSocket = new Socket("afsaccess1.njit.edu",6699);DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));request = inFromUser.readLine();outToServer.writeBytes(request + '\n');response = inFromServer.readLine();System.out.println("FROM SERVER : " + response);clientSocket.close();}}Code for Server.java import java.io.*;import java.net.*;class TCPServer {public static void main(String agrv[]) throws Exception { String clientrequest;String returnresponse;ServerSocket welcomeSocket = new ServerSocket(6699);while(true) {Socket connectionSocket = welcomeSocket.accept();BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());clientrequest = inFromClient.readLine();returnresponse = clientrequest + " : Response Returned" + '\n';outToClient.writeBytes(returnresponse);}}}

Varification of client file complied :

Verification of uploaded and complied server file :

Client server comminication -1

Client server comminication - 2