Sie sind auf Seite 1von 22

Exp No: 10

HOW TO EXECUTE

For Server:

​Open the command prompt

​Compile the java file using the command javac<file name>.java

​Run the server program using the command java <classname>

​The server will start and wait for the clients request to come

For Client:

​Open the command prompt

​Compile the java file using the command javac<file name>.java

​Run the client program using the command java <classname>

​Input the number for which factorial needs to be calculated

​Response from the server will be displayed as an echo

SOURCE CODE:

TCP FACTORIAL SERVER:

import java.io.*;

import java.net.*;

public class TCPFactorialServer {

public static void main(String[] args) throws Exception{

//Create server socket listening on port 8888

ServerSocket server = new ServerSocket(8888);

while(true){

System.out.println("Waiting for client to connect...");

Socket socket = server.accept();

//Create read/write from socket

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in =

newBufferedReader(

newInputStreamReader(socket.getInputStream()));

//client address

InetAddressremoteIp = socket.getInetAddress();

//Receiving from client

String msg = in.readLine();

System.out.println("Client " + remoteIp + " : " + msg);

int fact= factorial(msg);


66
out.println(fact);

private static int factorial(String input) {

int n = Integer.parseInt(input);

int f = 1;

for (int i=1; i<=n; i++)

f *= i;

return f;

TCP FACTORIAL CLIENT:

import java.net.*;

import java.io.*;

public class TCPFactorialClient {

public static void main(String[] args){

try{

Socket socket = new Socket("localhost", 8888);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(

newInputStreamReader(socket.getInputStream()));

System.out.println("Enter the Data To Be Sent : ");

BufferedReaderinFromUser =

newBufferedReader(new InputStreamReader(System.in));

String msg = inFromUser.readLine();

out.println(msg);

msg = in.readLine();

System.out.println("Factorial of number is: " + msg);

}catch(IOExceptionioe){

ioe.printStackTrace();

}
OUTPUTS:

For Server
For Client:
Exp No: 11

HOW TO EXECUTE:

For Server:

1. Open the command prompt

2. Compile the java file using the command javac<file name>.java

3. Run the server program using the command java <classname>

4. The server will start and wait for the clients request to come

For Client:

1. Open the command prompt

2. Compile the java file using the command javac<file name>.java

3. Run the client program using the command java <classname>

4. Response from the server will be displayed as an echo


SOURCE CODE:-

TCPHelloClient:-

import java.net.*;

import java.io.*;

public class TCPHelloClient

public static void main (String[] args) throws Exception

try

Socket socket = new Socket("localhost",8888);

BufferedReader in = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

String msg = in.readLine();

System.out.println("server:"+msg);

catch(IOExceptionioe)

ioe.printStackTrace();

}
73

TCPHelloServer:-

import java.io.*;

import java.net.*;

classTCPEchoServer

public static void main(String[]args)throws Exception

ServerSocket server = new ServerSocket(8888);

while(true)

System.out.println("Waiting for client to connect...");

Socket socket = server.accept();

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

BufferedReader in =new BufferedReader(new

InputStreamReader(socket.getInputStream()));

InetAddressremoteIp = socket.getInetAddress();

out.println("hello Welecome");
}

OUTPUTS:-

Server:-

Client:-
Exp No: 12

HOW TO EXECUTE:

For Server:

1. Open the command prompt

2. Compile the java file using the command javac<file name>.java

3. Run the server program using the command java <classname>

4. The server will start and wait for the clients request to come

For Client

1. Open the command prompt

2. Compile the java file using the command javac<file name>.java

3. Run the client program using the command java <classname>

4. Type any text on the client window

5. Response from the server will be displayed as an echo


Source Code

TCPEchoServer:-

import java.io.*;

import java.net.*;

classTCPEchoServer {

public static void main(String[]args)throws Exception

ServerSocket server = new ServerSocket(8888);

while(true)

System.out.println("Waiting for client to connect...");

Socket socket = server.accept();

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

BufferedReader in =new BufferedReader(new

InputStreamReader(socket.getInputStream()));

InetAddressremoteIp = socket.getInetAddress();

String msg =in.readLine();

System.out.println("Client"+remoteIp+":"+msg);

out.println(msg);

}
}

TCPEchoClient:-

import java.net.*;

import java.io.*;

public class TCPEchoClient

public static void main (String[] args) throws Exception

Socket socket = new Socket("localhost",8888);

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

BufferedReader in = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

System.out.println("Enter the Data To Be Sent:");

BufferedReaderFrmUsr = new BufferedReader(new InputStreamReader(System.in));

String msg = FrmUsr.readLine();

out.println(msg);

msg = in.readLine();

System.out.println("server:"+msg);

}}

OUTPUT:-

Server:-
Client:-

Exp No: 13

HOW TO EXECUTE:

1. Right Click on the HTML File and Choose the Debug or Run Option.

2. The HTML file will open automatically in the default browser

3. When the user selects the required option and clicks the submit

button, the servlet will be invoked .

4. The Servlet will process the request and send the response back to the

browser.

5. The output will be displayed in the browser.


Source Code :

TimeZone HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

Transitional//EN">

<html>

<head>

<title></title>

</head>

<body>

<form method="get" action="http://localhost:8084/bcaex10/tzservelet">

<select name="tzone" size="1">

<option value="EST">EST</option>

<option value="IST">IST</option>

<option value="GMT">GMT</option>

<option value="PST">PST</option>

</select>

<input type="submit" value="submit">

</form>
</body>

</html>

Servlet :

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.text.*;

import java.util.*;

public class tzservelet extends HttpServlet {

/** Processes requests for both HTTP <code>GET</code> and

<code>POST</code> methods.

* @param request servlet request

* @param response servlet response

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet tzservelet</title>");

out.println("</head>");
86

out.println("<body>");

Date d = new Date();

DateFormat df = DateFormat.getInstance();

String zone = request.getParameter("tzone");

if (zone != null) {

TimeZone tz = TimeZone.getTimeZone(zone);

df.setTimeZone(tz);

if (zone.trim().toUpperCase().equals("GMT"))

out.println("<h1>Time in London is " + "</h1>");

else if (zone.trim().toUpperCase().equals("EST"))

out.println("<h1>Time in NewYork is " + "</h1>");

else if (zone.trim().toUpperCase().equals("PST"))

out.println("<h1>Time in Los Angeles is " + "</h1>");

else
out.println("<h1>Time in India is " + "</h1>");

out.println(df.format(d));

out.close();

out.println("</body>");

out.println("</html>");

out.close();

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the

+ sign on the left to edit the code.">

/** Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

*/

protected void doGet(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

processRequest(request, response);

/** Handles the HTTP <code>POST</code> method.

* @param request servlet request

* @param response servlet response

*/

protected void doPost(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

processRequest(request, response);

/**Returns a short description of the servlet.

*/
88

public String getServletInfo() {

return "Short description";

}
Output:
Exp No: 14

HOW TO EXECUTE:

1. Right Click on the Servlet File and Choose the Debug or Run Option.

2. This will automatically open the default browser and display the headers

and values in a tabular format.


Source Code:

Servlet:

import java.io.*;

import java.net.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;
public class requestheader extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet requestheader</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1><U>HTTP REQUEST HEADER</title>");

out.println("<table border =1 cellpadding =4 cellspacing =0>");

Enumeration eNames = request.getHeaderNames();

while(eNames.hasMoreElements())

String name=(String)eNames.nextElement();

String value= request.getHeader(name);

out.println("<tr><td>"+name+"</td><td>"+value+"</td></tr>");

out.println("</table>");
92

out.println("</body>");

out.println("</html>");

out.close();

OUTPUT :
Exp No: 15

HOW TO EXECUTE:

For Server:
​Open the command prompt

​Compile the java file using the command javac <file name>.java

​Run the server program using the command java <classname>

​The server will start and wait for the clients request to come
TCP STRING UPPER CASE CONVERTER SERVER AND CLIENT 

Exp No: 15
Date :30/09/14
96

For Client:

​Open the command prompt

​Compile the java file using the command javac <file name>.java

​Run the client program using the command java <classname>

​Input the String that needs to be converted to Upper case

​Response from the server will be displayed .

SOURCE CODE

SERVER:
import java.io.*;

import java.net.*;

public class toupperServer {

public static void main(String[] args) throws Exception{

ServerSocket server = new ServerSocket(8888);

while(true){

System.out.println("Waiting for client to connect...");

Socket socket = server.accept();

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in =new BufferedReader(new

InputStreamReader(socket.getInputStream()));

InetAddress remoteIp = socket.getInetAddress();

String msg = in.readLine();

System.out.println("Client " + remoteIp + " : " + msg.toUpperCase());

out.println(msg.toUpperCase());

}
98

CLIENT:

import java.net.*;

import java.io.*;

public class toupperClient {

public static void main(String[] args) throws Exception{

Socket socket = new Socket("localhost", 8888);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

System.out.println("Enter the Data To Be Sent : ");

BufferedReader FrmUsr =

new BufferedReader(new InputStreamReader(System.in));

String msg = FrmUsr.readLine();

out.println(msg);

msg = in.readLine();

System.out.println("Server : " + msg);

}
Output:

SERVER:

CLIENT:

Das könnte Ihnen auch gefallen