On

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

Đề: Nhập vào chuỗi ký tự và ký tự kt, thay thế những ký tự kt có trong chuỗi

ký tự thành "@" và đếm xem bao nhiêu ký tự đã được thay thế, viết TCP Song song
và UDP:
TCP Song song:
==========================================================================
Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
*
* @author vnnam
*/
public class TCPClient {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost",7);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
Scanner sc = new Scanner(System.in);
String chuoikt;
String kt;
String proto;
byte [] b = new byte[60];
byte [] c = new byte[60];
while (true) {
System.out.println("Nhap vao chuoi ky tu: ");
chuoikt = sc.nextLine();
System.out.println("Nhap vao ky tu kt: ");
kt = sc.nextLine();
proto = chuoikt + "@" + kt;
//System.out.println("Protocol: "+ proto);
b = proto.getBytes();
if(chuoikt.equals("")==true) break;
if(kt.equals("")==true) break;
os.write(b);

is.read(c);
String kq = new String(c);
System.out.println("ket qua la:" + kq);
}
s.close();

} catch (IOException e) {
}
}
}
==========================================================================
Server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
/**
*
* @author vnnam
*/
public class TCPServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(7);
try {
Socket s = ss.accept();
Requestprocessing rp = new Requestprocessing(s);
rp.start();
} catch (IOException e) {
}
} catch (IOException e) {
}
}
}
class Requestprocessing extends Thread {
private Socket s;
public Requestprocessing(Socket s1){
s = s1;
}
public void run(){
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
byte [] b = new byte[60];
byte [] chuoi = new byte[60];
byte [] kytu = new byte[60];
byte [] c = new byte[60];
int ch = 0;
while (true) {
int dem = 0;
ch = is.read(b);
String dulieu = new String(b);
String[] tach = dulieu.split("@");
String chuoikt = tach[0];
String kt = tach[1];

chuoi = chuoikt.getBytes();
kytu = kt.getBytes();

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


if(chuoi[i]==kytu[0]) dem++;
}
String soluong = String.valueOf(dem);
String kq = chuoikt.replace(kt.charAt(0), '@');
String kqcuoi = kq + "_"+ soluong;
c = kqcuoi.getBytes();
os.write(c);
System.out.println("Ket qua: "+ kq);
//c = dulieu.getBytes();
//os.write(c);
}
} catch (IOException e) {
}
}
}
==========================================================================
UDP
Client:
[Tin nhắn đã thu hồi]
[Tin nhắn đã thu hồi]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
*
* @author vnnam
*/
public class UDPClient {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket();
InetAddress ipadd = InetAddress.getByName("localhost");
Scanner sc = new Scanner(System.in);
byte[] b = new byte[60000];
byte[] c = new byte[60000];
int ch=0;
String chuoikt;
String kt;
while (true) {
System.out.println("Nhap vao chuoi ky tu: ");
chuoikt = sc.nextLine();
System.out.println("Nhap vao ky tu kt: ");
kt = sc.nextLine();
String proto = chuoikt + "@" + kt;

b = proto.getBytes();
DatagramPacket dataSend = new DatagramPacket(b, b.length, ipadd,
9);
ds.send(dataSend);

DatagramPacket dataReceive = new DatagramPacket(c, c.length);


ds.receive(dataReceive);
String kq = new String(dataReceive.getData(), 0,
dataReceive.getLength());
System.out.println("Ke qua: "+kq);
}
} catch (IOException e) {
}
}
}
==========================================================================
Server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
/**
*
* @author vnnam
*/
public class UDPServer {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(9);
byte[] b = new byte[60000];
byte[] c = new byte[60000];
String chuoikytu,kytu;
byte[] chuoikt = new byte[60000];
byte[] kt = new byte[60000];
while (true) {
int dem=0;
DatagramPacket dataReceive = new DatagramPacket(b, b.length);
ds.receive(dataReceive);
String str = new String(dataReceive.getData(), 0,
dataReceive.getLength());

String[] tach = str.split("@");


chuoikytu = tach[0];
kytu = tach[1];

chuoikt = chuoikytu.getBytes();
kt = kytu.getBytes();

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


if(chuoikt[i]==kt[0]) dem++;
}

String kq = chuoikytu.replace(kytu.charAt(0), '@');


c = kq.getBytes();
DatagramPacket dataSend = new DatagramPacket(c, c.length,
dataReceive.getAddress(), dataReceive.getPort());
ds.send(dataSend);
}
} catch (IOException e) {
}
}
}

chuỗi in hoa
server

package clientserver;

import java.io.*;
import java.net.*;
import org.omg.CORBA.Request;

public class ServerBaiI {


public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(7);
while (true) {
try {
Socket s = ss.accept();
RequestProcessing rp = new RequestProcessing(s);
rp.start();
} catch (IOException e) {
System.out.print("socket bị lỗi: "+e );
}
}
} catch (IOException e) {
System.out.println("Tạo Socket bị lỗi: "+ e);
}
}
}
class RequestProcessing extends Thread {
private Socket s;
public RequestProcessing(Socket s1){
s = s1;
}
public void run() {
try {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
int ch = 0;
String str = "";
byte[] b = new byte[60];
byte[] c = new byte[60];
while (true) {
ch = is.read(b);
if (ch == -1) break;
str = new String(b);
String kq = str.toUpperCase();
c = kq.getBytes();
os.write(c);
}
s.close();
} catch (IOException e) {
System.out.println("Ket noi loi: "+ e);
}
}
}

client
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open thxe template in the editor.
*/
package clientserver;

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
*
* @author SINHVIEN
*/
public class ClientBaiI {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",7);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
Scanner sc = new Scanner(System.in);
String kq="";
byte[] c = new byte[60];
int ch1 = 0;
try {
while (true) {
System.out.println("Nhập vào chuổi ký tự: ");
String ch = sc.nextLine();
System.out.println("Đây là biến ch vừa nhập: "+ch);
if (ch =="no") break;
byte[] b = new byte[60];
b = ch.getBytes();
System.out.println("Đây là byte b: "+b);
os.write(b);
is.read(c);
kq = new String(c);
System.out.println("Chuổi vừa nhập là: "+kq);
}
s.close();
} catch (IOException e) {
}
} catch (IOException e) {
System.out.println("Tạo Socket bị lỗi: "+e);
}
}
}

You might also like