Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java

Diễn đàn hỏi đáp Java, hướng dẫn bài tập Java, Giúp đỡ về Java


You are not connected. Please login or register

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

1Buôi 2 - Bài 2 Empty Buôi 2 - Bài 2 Tue Mar 12, 2013 2:48 pm

Admin


Admin
Admin
Server:
Code:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nguyenict
 */
public class Server2_2 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9999);
        System.out.println("Server is started!");
        while (true){
            Socket s = ss.accept();
            Client c = new Client(s);
            c.start();
        }
    }
}
class Client extends  Thread{
    Socket s;
    Client(Socket s){
        this.s = s;
    }
    public void run(){
        try{
            Scanner in = new Scanner(s.getInputStream());
            PrintWriter out = new PrintWriter(s.getOutputStream());
            while (true){
                String[] recive = in.nextLine().split(" ");
                float ans=0;
                if (recive[0].toLowerCase().equals("EXIT"))
                    break;
                switch(recive[0]){
                    case "+":
                        ans = Integer.parseInt(recive[1])+Integer.parseInt(recive[2]);
                        out.println(ans+"");
                        out.flush();
                        break;
                    case "-":
                        ans = Integer.parseInt(recive[1])-Integer.parseInt(recive[2]);
                        out.println(ans+"");
                        out.flush();
                        break;
                    case "*":
                        ans = Integer.parseInt(recive[1])*Integer.parseInt(recive[2]);
                        out.println(ans+"");
                        out.flush();
                        break;
                    case "/":
                        ans = (float)Integer.parseInt(recive[1])/(float)Integer.parseInt(recive[2]);
                        out.println(ans+"");
                        out.flush();
                        break;
                }
            }
            s.close();
        }catch(IOException e){
           
        }
    }
}

Client:
Code:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nguyenict
 */
public class Client2_2 {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s = new Socket("127.0.0.1",9999);
        Scanner in = new  Scanner(s.getInputStream());
        PrintWriter out = new PrintWriter(s.getOutputStream());
        Scanner keyboard = new Scanner(System.in);
        while (true){
            System.out.print("Nhap phep toan: ");
            String str = keyboard.nextLine();
            String[] recive = str.split(" ");
            if (recive[0].toUpperCase().equals("EXIT"))
                break;
            out.println(recive[1]+" "+recive[0]+" "+recive[2]);
            out.flush();
            String ans = in.nextLine();
            System.out.println("Ket qua: "+ans);
        }
        s.close();
    }
}

https://javasupport.forumvi.com

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết