Tải bản đầy đủ - 0 (trang)
//dos.write(10);// ma 10 : ky tu dau dong`

//dos.write(10);// ma 10 : ky tu dau dong`

Tải bản đầy đủ - 0trang

for (int i = 0; i < arr.length; i++)

{

if(Integer.parseInt(arr[i]) == x)

{

result= String.valueOf(i);

return result;

}

}

return "Ko tim thay so can tim";

}

public static void main(String arg[]){

try {

Socket client= new Socket("127.0.0.1",5000);

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

DataOutputStream dos= new DataOutputStream(client.getOutputStream());

BufferedReader brIn= new BufferedReader(new InputStreamReader(client.getInputStream()));

System.out.println("Nhap chuoi can gui ");

String st = br.readLine();

dos.writeBytes(st);

dos.write(13);

dos.write(10);

System.out.println("nhap so can tim :");

int b = br.read();

dos.write(b);

dos.flush();// xoa bo nho dem tren Socket

//System.out.println("Du lieu da nhan:");

//st = brIn.readLine();

//System.out.println(st);

int i= brIn.read();

System.out.println("Vi tri cua so do la "+ b);

brIn.close();

dos.close();

client.close();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Bai4: XÓA SỐ HẠNG TRONG MẢNG

Server.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server

{

18



static String ViTri(String st)

{

String kq = " ";

String[] arr = st.split(",");

int i=0;

while(i
{

if(Integer.parseInt(arr[i]) %2 != 0)

{

kq=kq+arr[i]+",";

i++;

}

else

i++;

}

return kq;

}

public static void main(String[] args)

{

try

{

ServerSocket server = new ServerSocket(1234);

System.out.println("Server: Da khoi dong may chu\n");

Socket soc= server.accept();

System.out.println("May chu da duoc ket noi");

BufferedReader

br=new

InputStreamReader(soc.getInputStream()));

PrintWriter pw=new PrintWriter(soc.getOutputStream(), true );

String st=br.readLine();

String kq;

kq=ViTri(st);

pw.write(kq);

pw.write("\n");

pw.flush();

pw.close();

soc.close();

server.close();

}

catch(IOException e)

{

System.err.println("Khong lam viec");

System.exit(1);

}

}



BufferedReader(new



}

Client.java

import java.io.IOException;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Scanner;

public class Client

{

public static void main(String[] args)

19



{

try

{

Socket inComing=new Socket("localhost",1234);

System.out.println("Client: da ket noi may chu");

PrintWriter out=new PrintWriter(inComing.getOutputStream(), true );

Scanner in= new Scanner(inComing.getInputStream());

Scanner sc=new Scanner(System.in);

String st="";

System.out.println("Nhap chuoi: ");

st=sc.nextLine();

out.write(st);

out.write("\n");

out.flush();

String kq=in.nextLine();

System.out.println("Cac so sau khi xoa so chan la: "+ kq);

out.close();

in.close();

inComing.close();

}

catch(UnknownHostException e)

{

System.err.println("Khong tim thay may chu");

System.exit(1);

}

catch(IOException e)

{

System.err.println("Khong the ket noi");

System.exit(1);

}

}

}

Bai5: CHỮ CÁI ĐẦU THÀNH CHỮ HOA

server.java

import java.io.*;

import java.net.*;

import java.util.*;

public class server {

public static void main(String []args) throws IOException

{

//B1 : Ket Noi

ServerSocket server = new ServerSocket(8080);

Socket client = server.accept(); System.out.println("ket noi thanh cong");

//B2 : Khai bao doi tuong

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

//B3 : Nhan Chuoi Tu Client

String st = br.readLine();

//B4 : Xu Ly

String kq = ChuHoaDau(st);

//B5 : Tra ket qua ve Client

//pw.write(String.valueOf(kq));

20



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 ChuHoaDau(String st)

{

if (st.charAt(0)!=' ')

{

st = st.substring(0,1).toUpperCase().concat(st.substring(1));

}

for (int i=0;i
if (st.charAt(i)==' ')

{

int vt = i+1;

String st1 = st.substring(0,vt);

String st2 = st.substring(vt,vt+1).toUpperCase();

String st3 = st.substring(vt+1);

st = st1.concat(st2.concat(st3));

}

return st;

}

}

client.java

import java.io.*;

import java.net.*;

public class client {

public static void main(String []args) throws Exception

{

//B1 : ket noi

Socket server = new Socket("127.0.0.1",8080);

System.out.println("ket Noi thanh cong");

//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 : Nhap Chuoi

String st;

System.out.print("Nhap Chuoi : ");

st = in.readLine();

//B4 : Gui len Server

pw.write(st);

pw.write("\n");

pw.flush();

//B5 : Nhap Ket qua tra ve

21



//int kq = Integer.parseInt(br.readLine());

String kq = br.readLine();

//B6 : In ket qua ra man hinh

System.out.println("Chuyen Chu Cai Dau Thanh Chu Hoa : " + kq);

//B7 : ket Thuc

pw.close();

br.close();

server.close();

System.out.println("Dong Ket Noi");

}

}

Bai6: ĐẾM TẦN SỐ KÝ TỰ

server.java

import java.io.*;

import java.net.*;

import java.util.*;

public class server {

public static void main(String []args) throws IOException

{

//B1 : Ket Noi

ServerSocket server = new ServerSocket(8080);

Socket client = server.accept(); System.out.println("ket noi thanh cong");

//B2 : Khai bao doi tuong

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

//B3 : Nhan Chuoi Tu Client

String st = br.readLine();

//B4 : Xu Ly

int kq = DemKT(st);

//B5 : Tra ket qua ve Client pw.write(String.valueOf(kq));

//pw.write(DemSoLanXuatHienKyTu(st));

pw.write("\n");

pw.flush();

//B6 : Ket Thuc

pw.close();

br.close();

server.close();

client.close();

System.out.println("Dong Ket Noi");

}

public static int DemKT(String st)

{

int dem = 0;

char kt='A'; //Ky Tu Muon Dem

for (int i=0;i
if (kt==st.charAt(i))

dem++;

return dem;

}

}

client.java

22



import java.io.*;

import java.net.*;

public class client {

public static void main(String []args) throws Exception

{

//B1 : ket noi

Socket server = new Socket("127.0.0.1",8080);

System.out.println("ket Noi thanh cong");

//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 : Nhap Chuoi

String st;

System.out.print("Nhap Chuoi : ");

st = in.readLine();

//B4 : Gui len Server

pw.write(st);

pw.write("\n");

pw.flush();

//B5 : Nhap Ket qua tra ve

int kq = Integer.parseInt(br.readLine());

//String kq = br.readLine();

//B6 : In ket qua ra man hinh

System.out.println("So Lan Xuat Hien Cua 1 Ky Tu Trong Chuoi : " +kq);

}

}

CẮT KÝ TỰ TRỐNG MỘT CHUỖI

SERVER:

import java.io.*;

import java.net.*;

public class server {

public static void main(String [] args) throws IOException

{

//B1 : ket noi

ServerSocket server = new ServerSocket(12);

Socket client = server.accept();

//B2 : khai bao doi tuong

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

//B3 : Nhan Chuoi Tu Client

String st = br.readLine();

//B4 : Xu Ly

String kq = CutSpace(st);

//B5 : Tra ket qua ve Client

pw.write(kq);

pw.write("\n");

pw.flush();

//B6 : Ket Thuc

pw.close();

br.close();

23



client.close();

server.close();

}

public static String CutSpace(String st)

{

String str = "";

st = st.trim();

st = st.replace(" "," ");

String arr[] = st.split(" ");

for(int i=0;i
{

arr[i] = arr[i].substring(0,1).toUpperCase().concat(arr[i].substring(1));

str += arr[i];

}

return str;

}

}

CLIENT:

import java.io.*;

import java.net.*;

public class client{

public static void main(String [] args) throws IOException

{

//B1 : Ket Noi

Socket server = new Socket("127.0.0.1",12);

//B2 : Khai bao

BufferedReader in = new BufferedReader(new

InputStreamReader(System.in));

BufferedReader br = new BufferedReader(new

InputStreamReader(server.getInputStream()));

PrintWriter pw = new PrintWriter(server.getOutputStream());

//B3 : Nhap Chuoi Tu Ban Phim

String st;

System.out.println("Nhap Chuoi Tu Ban Phim : ");

st = in.readLine();

//B4 : Gui len Server

pw.write(st);

pw.write("\n");

pw.flush();

//B5 : Nhap ket qua tra ve tu Server

st = br.readLine();

//B6 : In ket qua ra man hinh

System.out.println("Chuoi Sau Khi Cat La: " + st);

//B7 : Ket Thuc

pw.close();

br.close();

server.close();

}

}

24



BÀI TOÁN THAY THẾ KÝ TỰ

SERVER:

import java.io.*;

import java.net.*;

import java.util.*;

public class server {

public static void main(String []args) throws IOException

{

//B1 : Ket Noi

ServerSocket server = new ServerSocket(80); Socket client = server.accept();

//B2 : Khai bao doi tuong

BufferedReader br = new BufferedReader(new

InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

//B3 : Nhan Chuoi Tu Client

String st = br.readLine();

//B4 : Xu Ly

String kq = thaythe(st);

//B5 : Tra ket qua ve Client

pw.write(kq);

pw.write("\n");

pw.flush();

//B6 : Ket Thuc pw.close();

br.close();

server.close();

client.close();

}

public static String thaythe(String st)

{

String str="";

str = st.replaceAll("a","A");

return str;

}}

CLIENT:

import java.io.*;

25



import java.net.*;

public class client {

public static void main(String []args) throws Exception

{

//B1 : ket noi

Socket server = new Socket("127.0.0.1",80);

//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 : Nhap Chuoi

String st;

System.out.print("Nhap Chuoi : ");

st = in.readLine();

//B4 : Gui len Server

pw.write(st);

pw.write("\n");

pw.flush();

//B5 : Nhap Ket qua tra ve

st = br.readLine();

//B6 : In ket qua ra man hinh

System.out.println("Chuoi Sau Khi Thay The La: " + st);

//B7 : ket Thuc

pw.close();

br.close(); server.close();

}

}

BÀI TOÁN ĐĂNG NHẬP 3 LẦN BẰNG TCP

CLIENT:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

public class client {

public static void main(String[] args) throws Exception

26



{

Socket client = new Socket("127.0.0.1", 13);

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

int s=0;

do

{

System.out.print("Nhap user: \n");

String fibo = in.readLine();

pw.write(fibo);

pw.write("\n");

pw.flush();

System.out.print("Nhap pass: \n");

String YeuCau = in.readLine();

pw.write(YeuCau);

pw.write("\n");

pw.flush();

String kq = br.readLine();

System.out.println("Ket qua nhan tu server: " + kq);

if(kq.equals("true"))

{

s=3;

String kiemtra = "Ban da dang nhap thanh cong";

System.out.println("Ket qua nhan tu server: " + kiemtra);

}

else

{

s++;

System.out.println("Ban da nhap sai:"+s+" lan");

//String kiemtra = HienChuoi(kq);

String kiemtra = "Vui long nhap lai user - pass";

System.out.println("Ket qua nhan tu server: " + kiemtra);

}

}while(s<3);

pw.close();

br.close();

client.close();

27



}

}

SERVER:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class server {

public static void main(String[] args) throws Exception

{

System.out.print("Da ket noi may chu \n");

ServerSocket server = new ServerSocket(13);

System.out.println("Cho ket noi tu Client");

Socket client = server.accept();

BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintWriter pw = new PrintWriter(client.getOutputStream());

String TK = "admin";

String MK = "admin";

int s=0;

do

{

String st = br.readLine();

System.out.println("Gia tri user nhan ve tu client: "+st);

//String user = String.format("%d", st);

String yc = br.readLine();

System.out.println("Gia tri pass nhan ve tu client: "+yc);

//String pass = String.format("%d", yc);

if(st.equals(TK) && yc.equals(MK))

{

String data = "true";

System.out.println("KQ: "+data);

pw.write(data);

pw.print("\n");

pw.flush();//Xoa du lieu

s=3;

28



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

//dos.write(10);// ma 10 : ky tu dau dong`

Tải bản đầy đủ ngay(0 tr)

×