Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ques 1 : Server's date and time at client side
- client:
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class DateClient
- {
- public static void main(String[] arga) throws Exception
- {
- Socket cs = new Socket("localhost",9999);
- DataInputStream in = new DataInputStream(cs.getInputStream());
- System.out.println(in.readLine());
- in.close();
- }
- }
- Server:
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class DateServer
- {
- public static void main(String[] arga) throws Exception
- {
- ServerSocket ss = new ServerSocket(9999);
- String line;
- Socket cs = ss.accept();
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream os = new PrintStream(cs.getOutputStream());
- os.println("Server Date" + (new Date()).toString() + "\n");
- in.close();
- os.close();
- }
- }
- Ques 2: Client address at server
- Client::
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ClientSecond
- {
- public static void main(String[] arga) throws Exception
- {
- Socket cs = new Socket("localhost",9999);
- DataInputStream in = new DataInputStream(cs.getInputStream());
- System.out.println(in.readLine());
- in.close();
- }
- }
- Server::
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ServerSecond
- {
- public static void main(String[] arga) throws Exception
- {
- ServerSocket ss = new ServerSocket(9999);
- String line;
- Socket cs = ss.accept();
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream os = new PrintStream(cs.getOutputStream());
- os.println("Server Date" + (new Date()).toString() + "\n");
- InetAddress addr = ss.getInetAddress();
- System.out.println(addr);
- in.close();
- os.close();
- }
- }
- Ques 3:Echo UDP Server
- Cient::
- import java.io.*;
- import java.net.*;
- public class ClientThree
- {
- public static void main(String args[])
- {
- DatagramSocket sock = null;
- int port = 7777;
- String s;
- BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
- try
- {
- sock = new DatagramSocket();
- InetAddress host = InetAddress.getByName("localhost");
- while(true)
- {
- //take input and send the packet
- echo("Enter message to send : ");
- s = (String)cin.readLine();
- byte[] b = s.getBytes();
- DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
- sock.send(dp);
- //now receive reply
- //buffer to receive incoming data
- byte[] buffer = new byte[65536];
- DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
- sock.receive(reply);
- byte[] data = reply.getData();
- s = new String(data, 0, reply.getLength());
- //echo the details of incoming data - client ip : client port - client message
- echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
- }
- }
- catch(IOException e)
- {
- System.err.println("IOException " + e);
- }
- }
- //simple function to echo data to terminal
- public static void echo(String msg)
- {
- System.out.println(msg);
- }
- }
- Server::
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ServerThree
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(7777);
- byte[] buffer = new byte[65536];
- DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
- while(true)
- {
- ds.receive(packet);
- byte[] data = packet.getData();
- String s = new String(data, 0, packet.getLength());
- echo(packet.getAddress().getHostAddress() + " : " + packet.getPort() + " - " + s);
- s = "OK : " + s;
- DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , packet.getAddress() , packet.getPort());
- ds.send(dp);
- }
- }
- public static void echo(String msg)
- {
- System.out.println(msg);
- }
- }
- ques4:: simple chat app
- client ::
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ClientFour
- {
- public static void main(String[] arga) throws Exception
- {
- Socket cs = new Socket("localhost",8888);
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream out = new PrintStream(cs.getOutputStream());
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- String s;
- while ( true )
- {
- System.out.print("Client : ");
- s=stdin.readLine();
- out.println(s);
- s=in.readLine();
- System.out.print("Server : "+s+"\n");
- if ( s.equalsIgnoreCase("BYE") )
- break;
- }
- out.close();
- in.close();
- cs.close();
- stdin.close();
- }
- }
- Server::
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ServerFour
- {
- public static void main(String[] arga) throws Exception
- {
- ServerSocket ss = new ServerSocket(8888);
- Socket cs = ss.accept();
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream out = new PrintStream(cs.getOutputStream());
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- String s;
- while ( true )
- {
- s=in.readLine();
- if (s.equalsIgnoreCase("END"))
- {
- out.println("BYE");
- break;
- }
- System. out.print("Client : "+s+"\n");
- System.out.print("Server : ");
- s=stdin.readLine();
- out.println(s);
- }
- cs.close();
- ss.close();
- in.close();
- out.close();
- }
- }
- 4. Write a Java program to develop a simple Chat application.
- Server
- import java.io.*;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.*;
- public class GossipServer
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket sersock = new ServerSocket(3000);
- Socket sock = sersock.accept( );
- // reading from keyboard (keyRead object)
- BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
- // sending to client (pwrite object)
- OutputStream ostream = sock.getOutputStream();
- PrintWriter pwrite = new PrintWriter(ostream, true);
- // receiving from server ( receiveRead object)
- InputStream istream = sock.getInputStream();
- BufferedReader receiveRead = 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();
- }
- }
- }
- Client
- import java.io.*;
- import java.net.*;
- public class GossipClient
- {
- public static void main(String[] args) throws Exception
- {
- Socket sock = new Socket("127.0.0.1", 3000);
- // reading from keyboard (keyRead object)
- BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
- // sending to client (pwrite object)
- OutputStream ostream = sock.getOutputStream();
- PrintWriter pwrite = new PrintWriter(ostream, true);
- // receiving from server ( receiveRead object)
- InputStream istream = sock.getInputStream();
- BufferedReader receiveRead = 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
- }
- }
- }
- }
- 5. The message entered in the client is sent to the server and the server encodes the message and
- returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
- b as c …z as a. This process is done using the TCP/IP protocol. Write a Java program for the above
- client
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ClientFive
- {
- public static void main(String[] arga) throws Exception
- {
- Socket cs = new Socket("localhost",8888);
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream out = new PrintStream(cs.getOutputStream());
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- String s;
- while ( true )
- {
- System.out.print("Client : ");
- s=stdin.readLine();
- out.println(s);
- s=in.readLine();
- System.out.print("Server : "+s+"\n");
- if ( s.equalsIgnoreCase("BYE") )
- break;
- }
- out.close();
- in.close();
- cs.close();
- stdin.close();
- }
- }
- Server
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ServerFive
- {
- public static void main(String[] arga) throws Exception
- {
- ServerSocket ss = new ServerSocket(8888);
- Socket cs = ss.accept();
- DataInputStream in = new DataInputStream(cs.getInputStream());
- PrintStream out = new PrintStream(cs.getOutputStream());
- BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
- String s;
- while ( true )
- {
- s=in.readLine();
- if (s.equalsIgnoreCase("END"))
- {
- out.println("BYE");
- break;
- }
- String s1="";
- for(char c : s.toCharArray())
- {
- int x = (int)c;
- if(x>=65 && x<=90)
- {
- x=(((x+1)-65)%26)+65;
- s1 += Character.toString((char)x);
- }
- else if(x>=97 && x<=122)
- {
- x=(((x+1)-97)%26)+97;
- s1 += Character.toString((char)x);
- }
- else
- {
- s1 += c;
- }
- }
- System. out.print("Client : "+s+"\n");
- out.println(s1);
- }
- cs.close();
- ss.close();
- in.close();
- out.close();
- }
- }
- 6. The message entered in the client is sent to the server and the server encodes the message and
- returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
- b as c …z as a. This process is done using UDP. Write a Java program for the above
- Client
- import java.io.*;
- import java.net.*;
- public class ClientSix
- {
- public static void main(String args[])
- {
- DatagramSocket sock = null;
- int port = 7777;
- String s;
- BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
- try
- {
- sock = new DatagramSocket();
- InetAddress host = InetAddress.getByName("localhost");
- while(true)
- {
- //take input and send the packet
- echo("Enter message to send : ");
- s = (String)cin.readLine();
- byte[] b = s.getBytes();
- DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
- sock.send(dp);
- //now receive reply
- //buffer to receive incoming data
- byte[] buffer = new byte[65536];
- DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
- sock.receive(reply);
- byte[] data = reply.getData();
- s = new String(data, 0, reply.getLength());
- //echo the details of incoming data - client ip : client port - client message
- echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
- }
- }
- catch(IOException e)
- {
- System.err.println("IOException " + e);
- }
- }
- //simple function to echo data to terminal
- public static void echo(String msg)
- {
- System.out.println(msg);
- }
- }
- Server
- import java.io.*;
- import java.net.*;
- import java.util.*;
- class ServerSix
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(7777);
- byte[] buffer = new byte[65536];
- DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
- while(true)
- {
- ds.receive(packet);
- byte[] data = packet.getData();
- String s = new String(data, 0, packet.getLength());
- String s1="";
- for(char c : s.toCharArray())
- {
- int x = (int)c;
- if(x>=65 && x<=90)
- {
- x=(((x+1)-65)%26)+65;
- s1 += Character.toString((char)x);
- }
- else if(x>=97 && x<=122)
- {
- x=(((x+1)-97)%26)+97;
- s1 += Character.toString((char)x);
- }
- else
- {
- s1 += c;
- }
- }
- echo(packet.getAddress().getHostAddress() + " : " + packet.getPort() + " - " + s1);
- s1 = "OK : " + s1;
- DatagramPacket dp = new DatagramPacket(s1.getBytes() , s1.getBytes().length , packet.getAddress() , packet.getPort());
- ds.send(dp);
- }
- }
- public static void echo(String msg)
- {
- System.out.println(msg);
- }
- }
- 7. Write a Java program to display the name and address of the computer that we are currently
- working on.
- import java.net.*;
- import java.io.*;
- public class cm
- {
- public static void main(String args[])
- {
- try
- {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter the url which you want to find ip address and host name");
- String str=br.readLine();
- InetAddress add=InetAddress.getByName(str);
- System.out.println("Local Host Information");
- System.out.println("Host Name:"+add.getHostName());
- System.out.println("IP address:"+add.getHostAddress());
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- 9. Using threading concepts, write a Java program to create a daemon process.
- import java.util.*;
- import java.io.*;
- import java.net.*;
- public class Back extends Thread
- {
- public void run()
- {
- if(Thread.currentThread().isDaemon())
- {//checking for daemon thread Â
- System.out.println("daemon thread work");
- } else{
- System.out.println("user thread work");
- }
- }
- }
- class Daemon{
- public static void main(String[]Â args){
- Back t1=new Back();//creating thread
- Back t2=new Back();
- Back t3=new Back();
- t1.setDaemon(true);//now t1 isÂdaemon thread
- t1.start();//starting threads Â
- t2.start();
- t3.start();
- } }
- 10. A server should run for 10 secs and generate numbers continuously. The client connecting to it
- should read data and find out the sum of the data thus read. Write a Java program to implement this
- scenario.
- Sever
- import java.net.*;
- import java.io.*;
- class Rand extends Thread{
- DataOutputStream dout;
- public Rand(DataOutputStream d){
- dout=d;
- }
- public void run(){
- try{
- while(true){
- dout.writeInt((int)(Math.random()*100)+1);
- }
- }
- catch(IOException e)
- {}
- }
- }
- class ServerRandom{
- public static void main(String args[])throws Exception{
- ServerSocket ss=new ServerSocket(1111);
- Socket s=ss.accept();
- DataOutputStream dout=new DataOutputStream(s.getOutputStream());
- Rand r=new Rand(dout);
- r.start();
- Thread.sleep(3000);
- System.exit(0);
- }
- }
- client
- import java.net.*;
- import java.io.*;
- class ClientRandom{
- public static void main(String args[])
- {
- int sum=0;
- try{
- Socket s=new Socket("localhost",1111);
- System.out.println("bb");
- DataInputStream din=new DataInputStream(s.getInputStream());
- while(true){
- int a=din.readInt();
- System.out.println(a);
- sum=sum+a;
- }
- }
- catch(Exception e){
- System.out.println("sum="+sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment