0% found this document useful (0 votes)
48 views14 pages

18BCE0452NETLAB

This document contains code for 5 Java network programming assignments: 1) A chat client and server for messaging between two devices. 2) A client and server for retrieving the current date from the server. 3) A grade calculation client and server where the client sends a test score and the server returns the corresponding letter grade. 4) A salary calculation client and server where the client sends an employee level and the server returns the corresponding salary. 5) An electricity bill calculator client and server where the client sends electricity usage and the server returns the calculated bill.

Uploaded by

sumanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

18BCE0452NETLAB

This document contains code for 5 Java network programming assignments: 1) A chat client and server for messaging between two devices. 2) A client and server for retrieving the current date from the server. 3) A grade calculation client and server where the client sends a test score and the server returns the corresponding letter grade. 4) A salary calculation client and server where the client sends an employee level and the server returns the corresponding salary. 5) An electricity bill calculator client and server where the client sends electricity usage and the server returns the calculated bill.

Uploaded by

sumanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

NETWORK LAB ASSIGNMENT

18BCE0452
SUMANTH KUMAR
1)CHATTING
2)DATE
3)GRADE CALCULATION
4)SALARY PROVIDER
5)ELECTRICITY BILL CALCULATION

CHATTING

CLIENT

import java.io.*;

import java.net.*;

public class Client

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

Socket sock = new Socket("10.30.157.44", 3000);

// reading from keyboard (keyRead object)

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

// sending to client (pwrite object)

OutputStreamostream = sock.getOutputStream();
PrintWriterpwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)

InputStreamistream = sock.getInputStream();

BufferedReaderreceiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;

while(true)

sendMessage = keyRead.readLine(); // keyboard reading

pwrite.println(sendMessage); // sending to server

pwrite.flush(); // flush the data

if((receiveMessage = receiveRead.readLine()) != null) //receive from server

System.out.println(receiveMessage); // displaying at DOS prompt

SERVER

import java.io.*;

import java.net.*;

public class Server


{

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

ServerSocketsersock = new ServerSocket(3000);

System.out.println("Server ready for chatting");

Socket sock = sersock.accept( );

// reading from keyboard (keyRead object)

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

// sending to client (pwrite object)

OutputStreamostream = sock.getOutputStream();

PrintWriterpwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)

InputStreamistream = sock.getInputStream();

BufferedReaderreceiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

if((receiveMessage = receiveRead.readLine()) != null)

System.out.println(receiveMessage);

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);
pwrite.flush();

OUTPUT

DATE CLIENT

import java.io.*;
import java.net.*;
class DateClient
{
    public static void main(String args[]) throws Exception
    {
        Socket soc=new Socket(InetAddress.getLocalHost(),5217);        
        BufferedReader in=new BufferedReader(new
InputStreamReader(soc.getInputStream()  ));
        System.out.println(in.readLine());
    }    
}

DATE SERVER

import java.net.*;
import java.io.*;
import java.util.*;
class DateServer
{
    public static void main(String args[]) throws Exception
    {
        ServerSocket s=new ServerSocket(5217);
        while(true)
        {
            System.out.println("Waiting For Connection ...");
            Socket soc=s.accept();
            DataOutputStream out=new DataOutputStream(soc.getOutputStream());
            out.writeBytes("Server Date: " + (new Date()).toString() + "\n");
            out.close();
            soc.close();
        }
    }
}

OUTPUT
GRADE CALCULATION

SERVER

import java.io.*;

importjava.net.ServerSocket;

importjava.net.Socket;

public class Ser {

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

ServerSocketserverSocket = new ServerSocket(3000);

Socket socket = serverSocket.accept();

DataInputStream din = new DataInputStream(socket.getInputStream());

OutputStreamoutputStream = socket.getOutputStream();

DataOutputStreamdataOutputStream = new DataOutputStream(outputStream);

int a=din.readInt();

if(a>=90){

System.out.println("S grade");

dataOutputStream.writeUTF("S grade");

else if(a>=80 && a<90){

System.out.println("A grade");

dataOutputStream.writeUTF("A grade");

}
else if(a>=70 && a<80){

System.out.println("B grade");

dataOutputStream.writeUTF("B grade");

else if(a>=60 && a<70){

System.out.println("C grade");

dataOutputStream.writeUTF("C grade");

else {

System.out.println("F grade");

dataOutputStream.writeUTF("F grade");

socket.close();

CLIENT

importjava.util.Scanner;

import java.io.*;

importjava.net.Socket;

public class Cli {

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

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


DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Scanner in=new Scanner(System.in);

System.out.println("Enter your marks:");

int n=in.nextInt();

out.writeInt(n);

InputStreaminputStream = socket.getInputStream();

DataInputStreamdataInputStream = new DataInputStream(inputStream);

String message = dataInputStream.readUTF();

System.out.println(message);

socket.close();

OUTPUT:-

SALARY CALCULATION

SERVER

import java.io.*;
importjava.net.ServerSocket;

importjava.net.Socket;

public class Ser {

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

ServerSocketserverSocket = new ServerSocket(3000);

Socket socket = serverSocket.accept();

DataInputStream din = new DataInputStream(socket.getInputStream());

OutputStreamoutputStream = socket.getOutputStream();

DataOutputStreamdataOutputStream = new DataOutputStream(outputStream);

int a=din.readInt();

if(a==1){

System.out.println("Highest salary");

dataOutputStream.writeUTF("9,00,000");

else if(a==2){

System.out.println(" Second level salary");

dataOutputStream.writeUTF("6,00,000");

else if(a==3){

System.out.println("Third level salary");

dataOutputStream.writeUTF("3,00,000");

else if(a==4){

System.out.println("Fourth level salary");


dataOutputStream.writeUTF("2,50,000");

else {

System.out.println("Fifth level salary");

dataOutputStream.writeUTF("1,00,000");

socket.close();

CLIENT

importjava.util.Scanner;

import java.io.*;

importjava.net.Socket;

public class Cli {

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

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

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Scanner in=new Scanner(System.in);

System.out.println("Enter your POSTION in company:");

int n=in.nextInt();

out.writeInt(n);

InputStreaminputStream = socket.getInputStream();
DataInputStreamdataInputStream = new DataInputStream(inputStream);

String message = dataInputStream.readUTF();

System.out.println(message);

socket.close();

OUTPUT

ELECTRICITY BILL CALCULATOR

SERVER

import java.io.*;

importjava.net.ServerSocket;

importjava.net.Socket;

public class Ser {

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


ServerSocketserverSocket = new ServerSocket(3000);

Socket socket = serverSocket.accept();

DataInputStream din = new DataInputStream(socket.getInputStream());

OutputStreamoutputStream = socket.getOutputStream();

DataOutputStreamdataOutputStream = new DataOutputStream(outputStream);

int a=din.readInt();

if(a<=100){

System.out.println("basic level usage");

int t=a*5;

dataOutputStream.writeInt(t);

else if(a>100 && a<=200){

System.out.println(" primary level usage");

dataOutputStream.writeInt(500+(a-100)*6);

else if(a>200 && a<=300){

System.out.println("Secondary level usage");

dataOutputStream.writeInt(1100+(a-200)*7);

else {

System.out.println("high level usage");

dataOutputStream.writeInt(1800+(a-300)*10);

}
socket.close();

CLIENT

importjava.util.Scanner;

import java.io.*;

importjava.net.Socket;

public class Cli {

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

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

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Scanner in=new Scanner(System.in);

System.out.println("Enter number of units");

int n=in.nextInt();

System.out.println("Electricity bill");

out.writeInt(n);

DataInputStream din = new DataInputStream(socket.getInputStream());

System.out.println(din.readInt());

//System.out.println(message);

socket.close();

}
OUTPUT

You might also like