Bài 29: xây dựng chương trình chat server làm việc ở chế độ song song

Màu nền
Font chữ
Font size
Chiều cao dòng

Server :

import java.net.*;

import java.io.*;

public class Songsong {

public static String nhap() throws IOException{

    String s;

    DataInputStream st=new DataInputStream(System.in);

    s=st.readLine();

    return s;

    }

public final static int defaultPort = 1234; // Cổng mặc định

public static void main(String[] args) {

try {

ServerSocket ss = new ServerSocket(defaultPort); //Tạo socket cho server

while (true) {

try { Socket s = ss.accept(); // Lắng nghe các yêu cầu nối kết

RequestProcessing rp = new RequestProcessing(s); // Tạo phần xử lý

rp.start(); // Khởi động phần xử lý cho Client hiện tại

} catch (IOException e) {

System.out.println("Connection Error: "+e);

} } } catch (IOException e) {

System.err.println("Create Socket Error: "+e); }

} }

class RequestProcessing extends Thread {

public static String nhap() throws IOException{

    String s;

    DataInputStream st=new DataInputStream(System.in);

    s=st.readLine();

    return s;

    }

Socket channel; //Socket của kênh ảo nối với Client hiện tại

public RequestProcessing(Socket s){

channel = s;// Nhận socket của kênh ảo nối với Client

}

public void run() {

        String ch;

try {

DataOutputStream os = new DataOutputStream(channel.getOutputStream());

DataInputStream is = new DataInputStream(channel.getInputStream());

while (true) {    

String n = is.readUTF(); // Nhận ký tự từ Client

System.out.println(n);

if (n == null) break; // Thoát nếu kênh ảo bị xóa

System.out.print("server: ");

ch=nhap();

os.writeUTF(ch); // Gởi ký tự nhận được về Client

} } catch (IOException e) {

System.err.println("Request Processing Error: "+e);  }

 } }

Client:

import java.net.*;

import java.io.*;

public class Songsong {

public static String nhap() throws IOException{

    String s;

    DataInputStream st=new DataInputStream(System.in);

    s=st.readLine();

    return s;

    }

public final static int defaultPort = 1234; // Cổng mặc định

public static void main(String[] args) {

try {

ServerSocket ss = new ServerSocket(defaultPort); //Tạo socket cho server

while (true) {

try { Socket s = ss.accept(); // Lắng nghe các yêu cầu nối kết

RequestProcessing rp = new RequestProcessing(s); // Tạo phần xử lý

rp.start(); // Khởi động phần xử lý cho Client hiện tại

} catch (IOException e) {

System.out.println("Connection Error: "+e);

} } } catch (IOException e) {

System.err.println("Create Socket Error: "+e); }

} }

class RequestProcessing extends Thread {

public static String nhap() throws IOException{

    String s;

    DataInputStream st=new DataInputStream(System.in);

    s=st.readLine();

    return s;

    }

Socket channel; //Socket của kênh ảo nối với Client hiện tại

public RequestProcessing(Socket s){

channel = s;// Nhận socket của kênh ảo nối với Client

}

public void run() {

        String ch;

try {

DataOutputStream os = new DataOutputStream(channel.getOutputStream());

DataInputStream is = new DataInputStream(channel.getInputStream());

while (true) {    

String n = is.readUTF(); // Nhận ký tự từ Client

System.out.println(n);

if (n == null) break; // Thoát nếu kênh ảo bị xóa

System.out.print("server: ");

ch=nhap();

os.writeUTF(ch); // Gởi ký tự nhận được về Client

} } catch (IOException e) {

System.err.println("Request Processing Error: "+e);  }

 } }

Bạn đang đọc truyện trên: Truyen2U.Pro