Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn dùng phương thức split() y. [IMG]images/smilies/daydreaming.gif[/IMG]

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Xin giúp về các phương thức xử lý chuỗi trong java.

    Ví dụ như:
    String str = hello diendan congdongcviet

    Xử lý theo kiểu:
    + Cắt chuỗi từ bên trái "hello", bên phải "congdongcviet", ở giữa "diendan";
    + Đảo, ghép chuỗi;
    + Xác định vị trí của kí tự trong chuỗi;
    + Đổi số thành chuỗi và ngược lại.

    Có một đoạn code thuộc dạng bài tập tạo Client - Server bằng Socket, xử lý tuần tự ở chế độ có nối kết. Công việc là xử lý chuổi từ Client gửi lên Server, trả về chuỗi đã được xử lý. Đoạn này chỉ cắt được bên trái, giữa và bên phải thì em không làm được. Mong các anh gợi ý giúp. Em dùng JGreator Pro để dịch chương trình.

    Server
    Mã:
    import java.net.*;
    import java.io.*;
    public class STCPEchoServer_Chuoi {
    	public final static int defaultPort =9999;
    	public static void main (String[] args) {
    		try{
    			ServerSocket ss = new ServerSocket(defaultPort);
    			while(true){
    				try{
    					Socket s = ss.accept();
    					OutputStream os = s.getOutputStream();
    					InputStream is = s.getInputStream();
    					
    					InputStreamReader isr = new InputStreamReader(is);
    					BufferedReader br = new BufferedReader(isr);
    					
    					PrintWriter pw = new PrintWriter(os);
    					while(true){
    						String line = br.readLine();
    						if(line==null)break;
    						int i = line.indexOf(" ");
    						String str = line.substring(0,i);
    						System.out.println(line);
    						pw.println(str);
    						pw.flush();
    					}
    				}catch(IOException e){
    					System.out.print("Connection Error:"+e);
    				}
    			}
    		}catch(IOException e){
    			System.out.print("Server Creation Error:"+e);
    		}
    	}
    }
    Client
    Mã:
    import java.io.*;
    import java.net.Socket;
    public class TCPEchoClient_Chuoi {
    	public static void main (String[] args) {
    		try{
    			Socket s = new Socket("127.0.0.1",9999);
    			
    			InputStream is = s.getInputStream();
    						
    			InputStreamReader isk = new InputStreamReader(System.in);
    			BufferedReader brk = new BufferedReader(isk);
    			
    			InputStreamReader isr = new InputStreamReader(is);
    			BufferedReader br = new BufferedReader(isr);
    			
    			OutputStream os = s.getOutputStream();
    			PrintWriter pw = new PrintWriter(os);
    			
    			while(true){
    				try{
    					String line = brk.readLine();
    					boolean k = line.equals("thoat");
    					if(k==true)break;
    					pw.println(line);
    					pw.flush();
    					String line1 = br.readLine();
    					System.out.println(line1);						
    				}catch(IOException ie){
    					System.out.print("Loi: Khong tao duoc socket");
    				}
    			}
    			s.close();
    		}catch(IOException ie){
    			System.out.print("Loi: Khong tao duoc socket1");
    	}
    }
    }

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •