letscheat1234

NETWORK

Mar 1st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 KB | None | 0 0
  1. ques 1 : Server's date and time at client side
  2.  
  3. client:
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.*;
  7. class DateClient
  8. {
  9. public static void main(String[] arga) throws Exception
  10. {
  11. Socket cs = new Socket("localhost",9999);
  12. DataInputStream in = new DataInputStream(cs.getInputStream());
  13. System.out.println(in.readLine());
  14. in.close();
  15. }
  16. }
  17.  
  18. Server:
  19.  
  20. import java.io.*;
  21. import java.net.*;
  22. import java.util.*;
  23. class DateServer
  24. {
  25. public static void main(String[] arga) throws Exception
  26. {
  27. ServerSocket ss = new ServerSocket(9999);
  28. String line;
  29. Socket cs = ss.accept();
  30. DataInputStream in = new DataInputStream(cs.getInputStream());
  31. PrintStream os = new PrintStream(cs.getOutputStream());
  32. os.println("Server Date" + (new Date()).toString() + "\n");
  33. in.close();
  34. os.close();
  35. }
  36. }
  37.  
  38. Ques 2: Client address at server
  39. Client::
  40. import java.io.*;
  41. import java.net.*;
  42. import java.util.*;
  43. class ClientSecond
  44. {
  45. public static void main(String[] arga) throws Exception
  46. {
  47. Socket cs = new Socket("localhost",9999);
  48. DataInputStream in = new DataInputStream(cs.getInputStream());
  49. System.out.println(in.readLine());
  50. in.close();
  51. }
  52. }
  53. Server::
  54. import java.io.*;
  55. import java.net.*;
  56. import java.util.*;
  57. class ServerSecond
  58. {
  59. public static void main(String[] arga) throws Exception
  60. {
  61. ServerSocket ss = new ServerSocket(9999);
  62. String line;
  63. Socket cs = ss.accept();
  64. DataInputStream in = new DataInputStream(cs.getInputStream());
  65. PrintStream os = new PrintStream(cs.getOutputStream());
  66. os.println("Server Date" + (new Date()).toString() + "\n");
  67. InetAddress addr = ss.getInetAddress();
  68.  
  69. System.out.println(addr);
  70.  
  71.  
  72. in.close();
  73. os.close();
  74. }
  75. }
  76.  
  77. Ques 3:Echo UDP Server
  78. Cient::
  79. import java.io.*;
  80. import java.net.*;
  81.  
  82. public class ClientThree
  83. {
  84. public static void main(String args[])
  85. {
  86. DatagramSocket sock = null;
  87. int port = 7777;
  88. String s;
  89.  
  90. BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
  91.  
  92. try
  93. {
  94. sock = new DatagramSocket();
  95.  
  96. InetAddress host = InetAddress.getByName("localhost");
  97.  
  98. while(true)
  99. {
  100. //take input and send the packet
  101. echo("Enter message to send : ");
  102. s = (String)cin.readLine();
  103. byte[] b = s.getBytes();
  104.  
  105. DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
  106. sock.send(dp);
  107.  
  108. //now receive reply
  109. //buffer to receive incoming data
  110. byte[] buffer = new byte[65536];
  111. DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
  112. sock.receive(reply);
  113.  
  114. byte[] data = reply.getData();
  115. s = new String(data, 0, reply.getLength());
  116.  
  117. //echo the details of incoming data - client ip : client port - client message
  118. echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
  119. }
  120. }
  121.  
  122. catch(IOException e)
  123. {
  124. System.err.println("IOException " + e);
  125. }
  126. }
  127.  
  128. //simple function to echo data to terminal
  129. public static void echo(String msg)
  130. {
  131. System.out.println(msg);
  132. }
  133. }
  134.  
  135. Server::
  136.  
  137. import java.io.*;
  138. import java.net.*;
  139. import java.util.*;
  140. class ServerThree
  141. {
  142. public static void main(String[] args) throws Exception
  143. {
  144. DatagramSocket ds = new DatagramSocket(7777);
  145. byte[] buffer = new byte[65536];
  146. DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
  147. while(true)
  148. {
  149. ds.receive(packet);
  150. byte[] data = packet.getData();
  151. String s = new String(data, 0, packet.getLength());
  152. echo(packet.getAddress().getHostAddress() + " : " + packet.getPort() + " - " + s);
  153.  
  154. s = "OK : " + s;
  155. DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , packet.getAddress() , packet.getPort());
  156. ds.send(dp);
  157. }
  158. }
  159. public static void echo(String msg)
  160. {
  161. System.out.println(msg);
  162. }
  163. }
  164.  
  165. ques4:: simple chat app
  166.  
  167. client ::
  168. import java.io.*;
  169. import java.net.*;
  170. import java.util.*;
  171. class ClientFour
  172. {
  173. public static void main(String[] arga) throws Exception
  174. {
  175. Socket cs = new Socket("localhost",8888);
  176. DataInputStream in = new DataInputStream(cs.getInputStream());
  177. PrintStream out = new PrintStream(cs.getOutputStream());
  178. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  179. String s;
  180. while ( true )
  181. {
  182. System.out.print("Client : ");
  183. s=stdin.readLine();
  184. out.println(s);
  185. s=in.readLine();
  186. System.out.print("Server : "+s+"\n");
  187. if ( s.equalsIgnoreCase("BYE") )
  188. break;
  189. }
  190. out.close();
  191. in.close();
  192. cs.close();
  193. stdin.close();
  194. }
  195. }
  196. Server::
  197. import java.io.*;
  198. import java.net.*;
  199. import java.util.*;
  200. class ServerFour
  201. {
  202. public static void main(String[] arga) throws Exception
  203. {
  204. ServerSocket ss = new ServerSocket(8888);
  205. Socket cs = ss.accept();
  206. DataInputStream in = new DataInputStream(cs.getInputStream());
  207. PrintStream out = new PrintStream(cs.getOutputStream());
  208. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  209. String s;
  210. while ( true )
  211. {
  212. s=in.readLine();
  213. if (s.equalsIgnoreCase("END"))
  214. {
  215. out.println("BYE");
  216. break;
  217. }
  218. System. out.print("Client : "+s+"\n");
  219. System.out.print("Server : ");
  220. s=stdin.readLine();
  221. out.println(s);
  222. }
  223.  
  224.  
  225.  
  226. cs.close();
  227. ss.close();
  228. in.close();
  229. out.close();
  230. }
  231. }
  232.  
  233.  
  234.  
  235. 4. Write a Java program to develop a simple Chat application.
  236.  
  237. Server
  238.  
  239. import java.io.*;
  240. import java.io.BufferedReader;
  241. import java.io.InputStream;
  242. import java.io.InputStreamReader;
  243. import java.io.OutputStream;
  244. import java.io.PrintWriter;
  245. import java.net.*;
  246. public class GossipServer
  247. {
  248. public static void main(String[] args) throws Exception
  249. {
  250. ServerSocket sersock = new ServerSocket(3000);
  251. Socket sock = sersock.accept( );
  252. // reading from keyboard (keyRead object)
  253. BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  254. // sending to client (pwrite object)
  255. OutputStream ostream = sock.getOutputStream();
  256. PrintWriter pwrite = new PrintWriter(ostream, true);
  257.  
  258. // receiving from server ( receiveRead object)
  259. InputStream istream = sock.getInputStream();
  260. BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  261.  
  262. String receiveMessage, sendMessage;
  263. while(true)
  264. {
  265. if((receiveMessage = receiveRead.readLine()) != null)
  266. {
  267. System.out.println(receiveMessage);
  268. }
  269. sendMessage = keyRead.readLine();
  270. pwrite.println(sendMessage);
  271. pwrite.flush();
  272. }
  273. }
  274. }
  275.  
  276. Client
  277. import java.io.*;
  278. import java.net.*;
  279. public class GossipClient
  280. {
  281. public static void main(String[] args) throws Exception
  282. {
  283. Socket sock = new Socket("127.0.0.1", 3000);
  284. // reading from keyboard (keyRead object)
  285. BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
  286. // sending to client (pwrite object)
  287. OutputStream ostream = sock.getOutputStream();
  288. PrintWriter pwrite = new PrintWriter(ostream, true);
  289.  
  290. // receiving from server ( receiveRead object)
  291. InputStream istream = sock.getInputStream();
  292. BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
  293.  
  294. System.out.println("Start the chitchat, type and press Enter key");
  295.  
  296. String receiveMessage, sendMessage;
  297. while(true)
  298. {
  299. sendMessage = keyRead.readLine(); // keyboard reading
  300. pwrite.println(sendMessage); // sending to server
  301. pwrite.flush(); // flush the data
  302. if((receiveMessage = receiveRead.readLine()) != null) //receive from server
  303. {
  304. System.out.println(receiveMessage); // displaying at DOS prompt
  305. }
  306. }
  307. }
  308. }
  309.  
  310.  
  311.  
  312. 5. The message entered in the client is sent to the server and the server encodes the message and
  313. returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
  314. b as c …z as a. This process is done using the TCP/IP protocol. Write a Java program for the above
  315.  
  316.  
  317. client
  318. import java.io.*;
  319. import java.net.*;
  320. import java.util.*;
  321. class ClientFive
  322. {
  323. public static void main(String[] arga) throws Exception
  324. {
  325. Socket cs = new Socket("localhost",8888);
  326. DataInputStream in = new DataInputStream(cs.getInputStream());
  327. PrintStream out = new PrintStream(cs.getOutputStream());
  328. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  329. String s;
  330. while ( true )
  331. {
  332. System.out.print("Client : ");
  333. s=stdin.readLine();
  334. out.println(s);
  335. s=in.readLine();
  336. System.out.print("Server : "+s+"\n");
  337. if ( s.equalsIgnoreCase("BYE") )
  338. break;
  339. }
  340. out.close();
  341. in.close();
  342. cs.close();
  343. stdin.close();
  344. }
  345. }
  346.  
  347. Server
  348.  
  349.  
  350.  
  351. import java.io.*;
  352. import java.net.*;
  353. import java.util.*;
  354. class ServerFive
  355. {
  356. public static void main(String[] arga) throws Exception
  357. {
  358. ServerSocket ss = new ServerSocket(8888);
  359. Socket cs = ss.accept();
  360. DataInputStream in = new DataInputStream(cs.getInputStream());
  361. PrintStream out = new PrintStream(cs.getOutputStream());
  362. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  363. String s;
  364. while ( true )
  365. {
  366. s=in.readLine();
  367. if (s.equalsIgnoreCase("END"))
  368. {
  369. out.println("BYE");
  370. break;
  371. }
  372.  
  373. String s1="";
  374. for(char c : s.toCharArray())
  375. {
  376. int x = (int)c;
  377.  
  378. if(x>=65 && x<=90)
  379. {
  380. x=(((x+1)-65)%26)+65;
  381. s1 += Character.toString((char)x);
  382. }
  383. else if(x>=97 && x<=122)
  384. {
  385. x=(((x+1)-97)%26)+97;
  386. s1 += Character.toString((char)x);
  387. }
  388. else
  389. {
  390. s1 += c;
  391. }
  392.  
  393. }
  394.  
  395. System. out.print("Client : "+s+"\n");
  396.  
  397.  
  398. out.println(s1);
  399. }
  400.  
  401.  
  402.  
  403. cs.close();
  404. ss.close();
  405. in.close();
  406. out.close();
  407. }
  408. }
  409. 6. The message entered in the client is sent to the server and the server encodes the message and
  410. returns it to the client. Encoding is done by replacing a character by the character next to it i.e. a as b,
  411. b as c …z as a. This process is done using UDP. Write a Java program for the above
  412.  
  413. Client
  414. import java.io.*;
  415. import java.net.*;
  416.  
  417. public class ClientSix
  418. {
  419. public static void main(String args[])
  420. {
  421. DatagramSocket sock = null;
  422. int port = 7777;
  423. String s;
  424.  
  425. BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
  426.  
  427. try
  428. {
  429. sock = new DatagramSocket();
  430.  
  431. InetAddress host = InetAddress.getByName("localhost");
  432.  
  433. while(true)
  434. {
  435. //take input and send the packet
  436. echo("Enter message to send : ");
  437. s = (String)cin.readLine();
  438. byte[] b = s.getBytes();
  439.  
  440. DatagramPacket dp = new DatagramPacket(b , b.length , host , port);
  441. sock.send(dp);
  442.  
  443. //now receive reply
  444. //buffer to receive incoming data
  445. byte[] buffer = new byte[65536];
  446. DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
  447. sock.receive(reply);
  448.  
  449. byte[] data = reply.getData();
  450. s = new String(data, 0, reply.getLength());
  451.  
  452. //echo the details of incoming data - client ip : client port - client message
  453. echo(reply.getAddress().getHostAddress() + " : " + reply.getPort() + " - " + s);
  454. }
  455. }
  456.  
  457. catch(IOException e)
  458. {
  459. System.err.println("IOException " + e);
  460. }
  461. }
  462.  
  463. //simple function to echo data to terminal
  464. public static void echo(String msg)
  465. {
  466. System.out.println(msg);
  467. }
  468. }
  469.  
  470. Server
  471.  
  472. import java.io.*;
  473. import java.net.*;
  474. import java.util.*;
  475. class ServerSix
  476. {
  477. public static void main(String[] args) throws Exception
  478. {
  479. DatagramSocket ds = new DatagramSocket(7777);
  480. byte[] buffer = new byte[65536];
  481. DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
  482. while(true)
  483. {
  484. ds.receive(packet);
  485. byte[] data = packet.getData();
  486. String s = new String(data, 0, packet.getLength());
  487. String s1="";
  488. for(char c : s.toCharArray())
  489. {
  490. int x = (int)c;
  491.  
  492. if(x>=65 && x<=90)
  493. {
  494. x=(((x+1)-65)%26)+65;
  495. s1 += Character.toString((char)x);
  496. }
  497. else if(x>=97 && x<=122)
  498. {
  499. x=(((x+1)-97)%26)+97;
  500. s1 += Character.toString((char)x);
  501. }
  502. else
  503. {
  504. s1 += c;
  505. }
  506.  
  507. }
  508. echo(packet.getAddress().getHostAddress() + " : " + packet.getPort() + " - " + s1);
  509.  
  510. s1 = "OK : " + s1;
  511. DatagramPacket dp = new DatagramPacket(s1.getBytes() , s1.getBytes().length , packet.getAddress() , packet.getPort());
  512. ds.send(dp);
  513. }
  514. }
  515. public static void echo(String msg)
  516. {
  517. System.out.println(msg);
  518. }
  519. }
  520.  
  521. 7. Write a Java program to display the name and address of the computer that we are currently
  522. working on.
  523.  
  524. import java.net.*;
  525. import java.io.*;
  526. public class cm
  527. {
  528. public static void main(String args[])
  529. {
  530. try
  531. {
  532. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  533. System.out.println("Enter the url which you want to find ip address and host name");
  534. String str=br.readLine();
  535. InetAddress add=InetAddress.getByName(str);
  536. System.out.println("Local Host Information");
  537. System.out.println("Host Name:"+add.getHostName());
  538. System.out.println("IP address:"+add.getHostAddress());
  539. }
  540. catch(Exception e)
  541. {
  542. System.out.println(e);
  543. }
  544. }
  545.  
  546. 9. Using threading concepts, write a Java program to create a daemon process.
  547.  
  548. import java.util.*;
  549. import java.io.*;
  550. import java.net.*;
  551. public class Back extends Thread
  552. public void run()
  553. if(Thread.currentThread().isDaemon())
  554. {//checking for daemon thread  
  555.  System.out.println("daemon thread work"); 
  556.  } else{ 
  557.  System.out.println("user thread work");
  558.  } 
  559.  } 
  560. class Daemon{
  561.  
  562.  public static void main(String[] args){ 
  563. Back t1=new Back();//creating thread 
  564.  Back t2=new Back(); 
  565. Back t3=new Back(); 
  566. t1.setDaemon(true);//now t1 isÂdaemon thread 
  567.  t1.start();//starting threads  
  568.  t2.start(); 
  569.  t3.start(); 
  570. } } 
  571.  
  572. 10. A server should run for 10 secs and generate numbers continuously. The client connecting to it
  573. should read data and find out the sum of the data thus read. Write a Java program to implement this
  574. scenario.
  575.  
  576. Sever
  577. import java.net.*;
  578. import java.io.*;
  579. class Rand extends Thread{
  580. DataOutputStream dout;
  581. public Rand(DataOutputStream d){
  582. dout=d;
  583. }
  584. public void run(){
  585. try{
  586. while(true){
  587. dout.writeInt((int)(Math.random()*100)+1);
  588. }
  589. }
  590. catch(IOException e)
  591. {}
  592. }
  593. }
  594. class ServerRandom{
  595. public static void main(String args[])throws Exception{
  596. ServerSocket ss=new ServerSocket(1111);
  597. Socket s=ss.accept();
  598. DataOutputStream dout=new DataOutputStream(s.getOutputStream());
  599. Rand r=new Rand(dout);
  600. r.start();
  601. Thread.sleep(3000);
  602. System.exit(0);
  603. }
  604. }
  605.  
  606. client
  607.  
  608. import java.net.*;
  609. import java.io.*;
  610. class ClientRandom{
  611. public static void main(String args[])
  612. {
  613. int sum=0;
  614. try{
  615. Socket s=new Socket("localhost",1111);
  616. System.out.println("bb");
  617. DataInputStream din=new DataInputStream(s.getInputStream());
  618.  
  619. while(true){
  620. int a=din.readInt();
  621. System.out.println(a);
  622. sum=sum+a;
  623. }
  624. }
  625. catch(Exception e){
  626. System.out.println("sum="+sum);
  627. }
  628. }
  629. }
Advertisement
Add Comment
Please, Sign In to add comment