Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Rocket Java

This document contains code for a server and client program that communicate over a socket connection. The server opens a socket on port 9999 and creates a new thread for each client connection to reverse strings received from the client. The client connects to the server, takes user input, sends it to the server, receives the reversed string back and prints it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Rocket Java

This document contains code for a server and client program that communicate over a socket connection. The server opens a socket on port 9999 and creates a new thread for each client connection to reverse strings received from the client. The client connects to the server, takes user input, sends it to the server, receives the reversed string back and prints it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ package socketdaochuoi; import java.io.*; import java.net.*; /** * * @author Admin */ public class Server { public static void main(String args[]) { try { ServerSocket ss = new ServerSocket(9999);// Tao cong 9999 de serv er lang nghe while (true)// Cho client ket noi { // Su dung multithread // Khi co 1 client gui yeu cau toi thi se tao ra 1 thread phu c vu client do new ThreadSocket(ss.accept()).start(); } } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); } //catch (InterruptedException ie) { } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package socketdaochuoi; import java.net.Socket; import java.io.*; /** * * @author Admin */ public class ThreadSocket extends Thread{ Socket socket= null; public ThreadSocket(Socket socket) {

this.socket=socket; } public void run() { try { DataOutputStream sendToClient= new DataOutputStream(socket.getOut putStream());// Tao output stream BufferedReader fromClient= new BufferedReader(new InputStreamRead er(socket.getInputStream()));//Tao input stream while (true) { String sentence=fromClient.readLine();// Chui nhn c t C lient System.out.println("FROM CLIENT: " + sentence); if (sentence.equalsIgnoreCase("quit")) break; String reverseSentence= reverse(sentence); //Thread.sleep(10000); // Gi s khi x l n mt khong 5s sendToClient.writeBytes(reverseSentence+'\n'); } } catch (Exception e) { e.printStackTrace(); } } public String reverse(String input) throws InterruptedException { String output=""; StringBuilder strBuilder= new StringBuilder(input); output=strBuilder.reverse().toString(); return output; } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package socketdaochuoi; import java.io.*; import java.net.Socket; /** * * @author Admin */ public class Client { public static void main(String args[]) { try {

String cau1;// Cau duoc gui toi server String ketQua;//Cau duoc server xu ly va tra lai la in hoa BufferedReader inFormUser= new BufferedReader(new InputStreamRead er(System.in));// Tao input stream Socket clientSocket= new Socket("127.0.0.1",9999);// Tao clinent socket de ket noi toi server DataOutputStream sendToServer= new DataOutputStream(clientSocket. getOutputStream());// Tao output stream ket noi toi socket BufferedReader inFromServer = new BufferedReader(new InputStreamR eader(clientSocket.getInputStream())); while(true) { cau1 = inFormUser.readLine();// Nhap vao 1 cau sendToServer.writeBytes(cau1+'\n');// gui toi server if (cau1.equalsIgnoreCase("quit"))// Gap chuoi quit break; ketQua = inFromServer.readLine();// Nhan lai tu server System.out.println("FROM SERVER: "+ketQua); } clientSocket.close();//Dong ket noi } catch (IOException e) { System.out.println("Exception Client: "+e.getMessage()); } } }

You might also like