Professional Documents
Culture Documents
On
On
On
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();
/**
*
* @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);
chuoikt = chuoikytu.getBytes();
kt = kytu.getBytes();
chuỗi in hoa
server
package clientserver;
import java.io.*;
import java.net.*;
import org.omg.CORBA.Request;
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);
}
}
}