Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Đổi chữ thường sang HOA bằng giao thức TCP

Discussion in 'Xây dựng ứng dụng server-client desktop' started by contraidep81, 13/11/11.

  1. contraidep81

    contraidep81 New Member

    Em có bài này bị lỗi chổ nào hok bít nửa các anh có thể xem giúp giùm em không?

    FIle Client


    Code:
    /**
     * @(#)CLIENT_TCP.java
     *
     *
     * @author 
     * @version 1.00 2011/11/13
     */
    import java.io.*;
    import java.net.*;
    
    public class CLIENT_TCP {
    
        public CLIENT_TCP() {
        }
        public static void main (String[] args) {
    	    try{
    	    	//Nhap dia chi server
    		System.out.print("Nhap dia chi server: ");
    		BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    		String dc=br.readLine();
    		//ket noi socket
    		Socket s= new Socket(dc,7070);
    		InputStream is=s.getInputStream();
    		OutputStream os=s.getOutputStream();
    		BufferedReader kb=new BufferedReader(new InputStreamReader(is));
    		PrintWriter pw=new PrintWriter(os);
    		System.out.print("Nhap chuoi ");
    		String ch1=kb.readLine();
    		//String ch1="sap den ngay thi roi";
    		pw.write(ch1);
    		pw.write("\n");
    		pw.flush();
    		String ch2=kb.readLine();
    		System.out.println(ch2);
    		s.close();
    	    }
    	    catch(IOException e){
    	    	System.out.println("Loi "+e);
    	    }
        }
        
        
    }
    File Server

    Code:
    /**
     * @(#)SERVER_TCP.java
     *
     *
     * @author 
     * @version 1.00 2011/11/13
     */
    import java.io.*;
    import java.net.*;
    
    public class SERVER_TCP {
    
        public SERVER_TCP() {
        }
        public static void main (String[] args) {
        try{
        	//tao socket
        	int port=7070;
        	ServerSocket ss=new ServerSocket(7070);
        	System.out.println("Da tao xong server Socket");
        	Socket s = ss.accept();
        	System.out.println("Co mot client ket noi ");
        	//Tao luong nhan
        	InputStream is = s.getInputStream();
        	OutputStream os = s.getOutputStream();
        	BufferedReader br= new BufferedReader(new InputStreamReader(is));
        	PrintWriter pw=new PrintWriter(os);
        	//nhan chuoi tu client
        	String nhan;
        	nhan=br.readLine();
        	//xu ly
        	String kq=nhan.toUpperCase();
        	//gui ket qua
        	pw.write(kq);pw.flush();
        	s.close();
        	
        }
        catch(IOException e){
        	System.out.println("Loi "+e);
        }
        }
        
        
    }
     
  2. nguyenan

    nguyenan New Member

    Ðề: Đổi chữ thường sang HOA bằng giao thức TCP

    PHP:
    /**
     * @(#)Client.java
     *
     * Client application
     *
     * @author 
     * @version 1.00 2012/1/15
     */
     
     
    import java.io.*;
     
    import java.net.*;

     
    public class 
    Client {
        
        public static 
    void main(String[] argsthrows  Exception
            
    {
                
    //b1: ket  noi den server
                
    Socket server=new Socket("127.0.0.1",8080);
                
    //b2 khai bao doi tuong
                
    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
                
    BufferedReader br=new BufferedReader(new InputStreamReader(server.getInputStream()));
                
    PrintWriter pw= new PrintWriter(server.getOutputStream());
                
    //b3 nhapchuoi
                
    String st;
                
    System.out.println ("nhap chuoi:");
                
    st=in.readLine();
                
    //b4 gui len server
                
    pw.write(st);
                
    pw.write("\n");
                
    pw.flush();
                
    // b 5 nahn ketqua trqa ve
                
    stbr.readLine();
                
    //b6  inra manhinh
                
    System.out.println ("chuyen chuoi thanh chu thuong:" +st);
                
    //b7 ket thuc
                
                
    pw.close();
                
                
    br.close();
                
    server.close();
            }
        }

    //
    /**
     * @(#)Server.java
     *
     * Server application
     *
     * @author 
     * @version 1.00 2012/1/15
     */
    import java.io.*;
    import java.net.*;
     
    public class 
    Server {
        
        public static 
    void main(String[] argsthrows IOException
            
    {
                
                
    // buoc1 ket noi
                
    ServerSocket server= new ServerSocket(8080);
                
    Socket client=server.accept();
                
    System.out.println ("ket  noi thanh cong:");
                
    // b2 khai bao  cac doi tuong
                
    BufferedReader br= new BufferedReader(new InputStreamReader(client.getInputStream()));
                
    PrintWriter pw= new PrintWriter(client.getOutputStream());
                
    // nhan chuoi tu client
                
    String stbr.readLine();
                
    // b4 xu ly
                
    String kqchuhoa(st);
                
    // tra ket ve cho client
            //    pw.write(String.valueOf(kq));
                
    pw.write(kq);
                
    pw.write("\n");
                
    pw.flush();
                
    //b6 ket thuc
                
    pw.close();
                
    br.close();
                
    server.close();
                
    client.close();
                
    System.out.println ("dong ket noi:");
                }
            public static 
    String  chuhoa(String st)
            {
                
    String str="";
                
    str=st.toUpperCase();
                return 
    str;
                
                
            }    
            }
    một số bài tập bạn tham khảo thêm mình củng mới tìm hiểu chúc bạn thành công
     

Chia sẻ trang này

Loading...