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

contraidep81

New Member
18/8/10
12
0
1
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);
    }
    }
    
    
}
 

nguyenan

New Member
10/12/11
2
0
0
Ðề: Đổ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[] args) throws  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
    		st= br.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[] args) throws 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 st= br.readLine();
    		// b4 xu ly
    		String kq= chuhoa(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