k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

[Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

5 posters

Go down

Cool [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Sun Oct 07, 2012 9:54 pm

Đề:
Code:
Viết chương trình theo mô hình Client-Server sử dụng Socket ở chế độ có nối kết.
Trong đó:
o  Server sẽ nhận các yêu cầu là một chuỗi có khuôn dạng như sau:
        "OP  Operant1  Operant2\n"  (VD:    + 10  200)
Trong đó:
        - OP là một ký tự chỉ phép toán muốn thực hiện: '+' , '-' ,  '*' ,  '/'
        - Operant1, Operant2 là đối số của phép toán. 
        - Các thành phần trên cách nhau bởi 1 ký tự trắng ' '
        - Kết thúc yêu cầu bằng ký tự xuống dòng '\n'
Mỗi khi server nhận được một thông điệp, nó sẽ thực hiện phép toán:
      Operant1 OP Operant2  (chẳng hạn tính  100+200 ) để cho ra kết quả
(chẳng hạn số 300), sau đó đổi kết quá thành chuỗi và gởi về Client.
o  Client cho phép người dùng nhập  từ bàn phím các phép  toán muốn  tính  theo cách
thức thông thường  (VD: 100 + 200). 
Client tạo ra thông điệp yêu cầu theo đúng dạng do Server qui định (chuyển về dạng 
“+ 100 200”), gửi sang Server.
Client chờ nhận kết quả trả về và hiển thị ra màn hình. 

Lưu ý:


Cách chạy thì xem lại bài này [You must be registered and logged in to see this link.] , ở đây mình chỉ show code cho anh em thôi.

Chương trình Server:
Class luồng Server
Code:

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

public class ServerSum extends Thread {

   ServerSocket ss;
   int port=7;
   Scanner input;
   PrintWriter output;
   Socket s;
   String s1;
   String s2[];
   float kq;
   public ServerSum()
   {
      try {
         ss = new ServerSocket(this.port);
         output = new PrintWriter(System.out);
         output.println("Server được khởi động...!");
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public ServerSum(int p)
   {
      try {
         this.port = p;
         ss = new ServerSocket(this.port);
         output = new PrintWriter(System.out);
         output.println("Server được khởi động...!");
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public void Recived()
   {
      try {
         input = new Scanner(s.getInputStream());
         s1 = input.nextLine();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public void Procesing()
   {
      s2 = new String[100];
      s2 = s1.split(" ");
      switch(s2[0]){
      case "+":
            float a = Float.valueOf(s2[1]).floatValue();
            float b = Float.valueOf(s2[2]).floatValue();
            kq = a+b;
            break;
      case "-":
            float a1 = Float.valueOf(s2[1]).floatValue();
            float b1 = Float.valueOf(s2[2]).floatValue();
            kq = a1-b1;
            break;
      case "*":
            float a2 = Float.valueOf(s2[1]).floatValue();
            float b2 = Float.valueOf(s2[2]).floatValue();
            kq = a2*b2;
            break;
      case "/":
            float a3 = Float.valueOf(s2[1]).floatValue();
            float b3 = Float.valueOf(s2[2]).floatValue();
            kq = a3/b3;
            break;
      default:
            kq=0;
         break;
      }
   }
   public void Send()
   {
      try {
         output = new PrintWriter(s.getOutputStream());
         output.println(kq);
         output.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
      
   }
   public void run()
   {
      while(true)
      {
         try {
            s = ss.accept();
            Recived();
            Procesing();
            Send();
         } catch (IOException e) {
            e.printStackTrace();
         }
         
      }
   }

}


Hàm Main Server:

Code:

import java.io.PrintWriter;


public class MainServer {

   
   public static void main(String[] args) {
      ServerSum S = new ServerSum(8080);
      PrintWriter output = new PrintWriter(System.out);
      output.println("Server đã chạy và chờ kết nối từ Client...");
      S.start();

   }

}


Chương trình Client:
Class luồng Client :
Code:

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


public class ClientSum extends Thread {

 String address = "10.0.21.54";
 int port =7;
 boolean connect = false;
 Scanner input;
 PrintWriter output;
 Socket s;
 String s1;
 String s2[];
 String s3;
 float kq;
 public ClientSum()
 {
 }
 public ClientSum(String ad, int p)
 {
    this.address = ad;
    this.port =p;
    output = new PrintWriter(System.out);
    output.println("Client được khởi động...!");
 }
 public void connected()
 {

      try {
         s = new Socket(this.address, this.port);
         this.connect= true;
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

 }
 public void disconnect()
 {
    try {
      s.close();
      this.connect = false;
   } catch (IOException e) {
      e.printStackTrace();
   }
 }
 public void InputString()
 {
    input = new Scanner(System.in);
    output = new PrintWriter(System.out);
    output.print("Nhập vào biểu thức operant1 op operant2: ");
    output.flush();
    s1 = input.nextLine();
    s2 = new String[100];
    s2 = s1.split(" ");
    s3 = s2[1]+" "+s2[0]+" "+s2[2];
 }
 public void Send()
 {
    try {
      output = new PrintWriter(s.getOutputStream());
      output.println(s3);
      output.flush();
   } catch (IOException e) {
      e.printStackTrace();
   }
 }
 public void Recived()
 {
    try {
      input = new Scanner(s.getInputStream());
      kq = input.nextFloat();
   } catch (IOException e) {
      e.printStackTrace();
   }
   
 }
 public void Look()
 {
    output = new PrintWriter(System.out);
    output.println("Kết quả: "+kq);
    output.flush();
 }
}


Hàm main Client:
Code:

import java.io.PrintWriter;
import java.util.Scanner;


public class MainClient {

   public static void main(String[] args) {
      PrintWriter output = new PrintWriter(System.out);
      Scanner input = new Scanner(System.in);
      String address = "10.0.21.54";
      int port =7;
      output.println("Client run....!");
      output.flush();
      output.print("Nhập địa chỉ Server: ");
      output.flush();
      address = input.nextLine();
      output.print("Nhập cổng port: ");
      output.flush();
      port = input.nextInt();
      ClientSum C = new ClientSum(address, port);
      while(true){
         C.connected();
         if (C.connect) {
            C.InputString();
            C.Send();
            C.Recived();
            C.Look();
            C.disconnect();
         } else {
            output.print("Kết nối Server thất bại! ");
            output.flush();
         }
         }

   }

}

Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Tue Oct 30, 2012 6:38 pm

Chổ này là sao bác ơi??
Code:

output.print("Nhập vào biểu thức operant1 op operant2: ");
    output.flush();
    s1 = input.nextLine();
    s2 = new String[100];
    s2 = s1.split(" ");
    s3 = s2[1]+" "+s2[0]+" "+s2[2];
hai dòng này là sao bác??
s2 = s1.split(" ");
s3 = s2[1]+" "+s2[0]+" "+s2[2];


Em đú theo cuốn giáo trình nó dài gì nè
Code:

Scanner input = new Scanner(System.in);
         System.out.println("Nhap vao phep toan: ");
         String data = input.nextLine();
         
String chuoi1="",chuoi2="";
char pheptoan = 0;
for(int i=0;i<data.length();i++){
            if(data.charAt(i)=='+' || data.charAt(i)=='-' || data.charAt(i)=='*' || data.charAt(i)=='/'){
chuoi1 = data.substring(0,i); chuoi1.trim();
chuoi2 = data.substring(i+1); chuoi2.trim();
pheptoan = data.charAt(i);
break;
}
}
2 cái đều ko hỉu??
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Tue Oct 30, 2012 9:26 pm

Ok thiếm em sẽ giải thích đoạn này:
Code:

output.print("Nhập vào biểu thức operant1 op operant2: ");
    output.flush();
    s1 = input.nextLine();
    s2 = new String[100];
    s2 = s1.split(" ");
    s3 = s2[1]+" "+s2[0]+" "+s2[2];


output.print("Nhập vào biểu thức operant1 op operant2: ");
output.flush();
s1 = input.nextLine();

- Mình nhập vào 1 chuỗi bao gồm 2 số và phép toán có dạng: Operant1 OP Operant2
- Vd: đó là 4 + 5 (nhớ là có khoảng trắng)
- Khi đó chuỗi này sẽ lưu vào biến kiểu String đó là s1.
- OK Xong
s2 = new String[100];
s2 = s1.split(" ");
s3 = s2[1]+" "+s2[0]+" "+s2[2];

- Phần tử s2 là 1 mảng kiểu String
- s2 = new String[100]; khởi tạo s2 gồm 100 phần tử
- s2 = s1.split(" "); Tách chuỗi s1 (4 + 5) ra bởi khoảng trắng s1.split(" "), tức là nó gặp khoảng trắng nó bỏ, ta lấy ra được (4,+,5) rồi lần lượt lưu vào mảng s2 với s2[0]=4; s2[1]=+ và s2[2]=5;
- s3 = s2[1]+" "+s2[0]+" "+s2[2]; s3 là 1 biến kiểu chuỗi ghép các phần tử của mảng s2 lại thành 1 chuỗi kèm theo khoảng trắng, khi đó s3 là (+ 4 5)
- Nhiệm vụ của nguyên đoạn này là xử lý biểu thức trung tố (4 + 5) thành hậu tố (+ 4 5) theo đề yêu cầu rồi mới gởi cho Server để xử lý chuỗi trên
-OK?
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Tue Oct 30, 2012 9:30 pm

Lưu ý khi copy code về nhớ đổi tên class trùng với tên chương trình, nếu có thắc mắc về lỗi code thì xin cai TeamViewer 7 đọc pass và id em sẽ hỗ trợ từ xa, có thể hỏi bác Tài cũng được, em đã truyền nội công cho thiếm ấy
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by pklove1910 Sat Nov 03, 2012 1:52 pm

bị lỗi không hiểu ở dòng
s = ss.accept();
là sao vậy tâm. Và cái khai báo int port = 7 có thể giải thích rõ hơn được không
pklove1910
pklove1910

Posts : 207
Thanked : 3
Gia Nhập 11/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Sat Nov 03, 2012 4:14 pm

Bài của em viết theo cách cắt chuổi của giáo trình.
VD: nhập vào: 10+5 client sẻ xử lý lấy ra 10,5,+ sau đó gữi đến server yêu cầu có dạng sau: + 10 5
Server thực hiện rồi trả kết quả về client.
Code em viết không có chia ra các hàm receive, send, processing... như thầy mà gom vào hàm run hết luôn Surprised anh em góp ý.
1. Project lamtoan_server (gồm 2 package: server và program)
+ Package: server -> class: server_echo.java

Code:

package server;

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

public class server_echo extends Thread {
   Scanner read;
   PrintWriter write;
   Socket socket;
   
   public server_echo(){
      
   }

   public server_echo(Socket s){
      this.socket = s;
      try {
         read = new Scanner(s.getInputStream());
         write = new PrintWriter(s.getOutputStream());
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   
   public void run(){
      while(true){
         String data = read.nextLine(); // nhan tu client
         if(data.compareTo("@")==0){ // bang @ thoat vong lap
            break;
         }
         // xu ly yeu cau
         char pheptoan = data.charAt(0);
         int vitri = data.lastIndexOf(' ');// lay vi tri khoang trang dau tien
         String chuoi1 = data.substring(2,vitri); // cat chuoi tu vi tri thu 2 den khoang trang
         String chuoi2 = data.substring(vitri+1); // cat tu sau khoang trang toi het
         int x1 = Integer.valueOf(chuoi1).intValue();//ep kieu
         int x2 = Integer.valueOf(chuoi2).intValue();// ep kieu
         int kq=0;
         // bat dau tinh toan
         if(pheptoan == '/'){
            if(x2==0){
               write.println("Khong the thuc hien phep toan...");
               write.flush();
            } else {
               kq = (int) ((float)x1/x2);
               write.println(kq); //gui lai client
               write.flush();
            }
         } else {
            switch(pheptoan){
            case'+': kq = x1+x2; break;
            case'-': kq = x1-x2; break;
            case'*': kq = x1*x2; break;
            }
            write.println(kq); //gui lai client
            write.flush();
         }         
      } // end while
      try {
         this.socket.close();
         System.out.print("Ket thuc ket noi...");
      } catch (IOException e) {
         // TODO Auto-generated catch block
         System.out.print("Loi khi cham dut ket noi...");
      }
   }
   
}

+ Package: program -> class: chuong_trinh_server.java
Code:

package program;

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

import server.server_echo;

public class chuong_trinh_server {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      int port;
      System.out.print("Nhap vao cong lang nge: ");
      Scanner input = new Scanner(System.in);
      port = input.nextInt();
      try {
         ServerSocket ss = new ServerSocket(port);
         System.out.println("Server da khoi dong...");
         while(true){
            Socket s = ss.accept();
            System.out.println("Co 1 ket noi den: ip "+s.getInetAddress()+" port "+s.getPort());
            server_echo sv = new server_echo(s);
            sv.start();            
         }      
      } catch (IOException e) {
         // TODO Auto-generated catch block
         System.out.print("Khong the khoi dong duoc server ...");
      }
      
   }

}

2. Project lamtoan_client (2 package: client và program)
+ Package: client -> class: client_echo.java
Code:

package client;

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

public class client_echo {
   Scanner in;
   PrintWriter out;
   Socket socket;
   
   public client_echo(){
      
   }
   
   public client_echo(String ip, int port){
      try {
         this.socket = new Socket(ip,port);
         System.out.println("Ket noi server thanh cong..");
         this.in = new Scanner(this.socket.getInputStream());
         this.out = new PrintWriter(this.socket.getOutputStream());
         
      } catch (UnknownHostException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   
   public void run(){
      while(true){
         Scanner input = new Scanner(System.in);
         System.out.print("Nhap vao bieu thuc (dang operant1 op operant2): ");
         String data = input.nextLine();
         if(data.compareTo("@")==0){ // thoat  vong lap
            this.out.print(data); // gui @ qua de server no dong luon
            this.out.flush();
            break;
         }
         // xu ly chuoi truoc khi gui den server
         String chuoi1="",chuoi2="";
         char pheptoan = 0;
         for(int i=0;i<data.length();i++){
            if(data.charAt(i)=='+' || data.charAt(i)=='-' ||
                  data.charAt(i)=='*' || data.charAt(i)=='/'){
               chuoi1 = data.substring(0,i); chuoi1.trim();
               chuoi2 = data.substring(i+1); chuoi2.trim();
               pheptoan = data.charAt(i);
               break;
            }
         } // end xu ly chuoi
         String yeucau = pheptoan + " " + chuoi1 + " " + chuoi2; // gui yeucau den server
         
         this.out.println(yeucau);// gui du lieu di
         this.out.flush();
         
         String ketqua = this.in.nextLine(); // nhan du lieu ve
         System.out.println("Ket qua la: "+ketqua); // xuat ket qua ra
                  
      }
      try {
         this.socket.close();
         System.out.print("Bye bye baby...");
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

}

+ Package: program -> class: chuong_trinh_client.java
Code:

package program;

import java.util.Scanner;

import client.client_echo;

public class chuong_trinh_client {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Scanner input = new Scanner(System.in);
      System.out.print("Nhap ip may chu: ");
      String ip = input.nextLine();
      System.out.print("Nhap port may chu: ");
      int port = input.nextInt();
      client_echo cl = new client_echo(ip,port);
      cl.run();

   }

}


[img][You must be registered and logged in to see this link.][/img]
[img][You must be registered and logged in to see this link.][/img][img][You must be registered and logged in to see this link.][/img]

Anh em ai hoàn thành được bài này theo cách viết của thầy thì share nha...(ko share cut xxx ah)

P/s: Tin mới cập nhật hồi sáng thực hành: do nhóm 1 học ngu quá nên thầy quyết định dời ngày kiểm tra thực hành giữa kỳ vào sáng thứ 3 tuần sau sau nữa, còn sáng thứ 7 tuần sau ôn tập full lớp.
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by pklove1910 Sat Nov 03, 2012 10:32 pm

year như vậy là có thêm một tuần nữa để ôn! đúng là ngu thiệt Evil or Very Mad
pklove1910
pklove1910

Posts : 207
Thanked : 3
Gia Nhập 11/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by pklove1910 Sat Nov 03, 2012 10:35 pm

Cường cho hỏi mấy cái package dùng để làm j vậy?
pklove1910
pklove1910

Posts : 207
Thanked : 3
Gia Nhập 11/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Sat Nov 03, 2012 10:40 pm

pklove1910 đã viết:Cường cho hỏi mấy cái package dùng để làm j vậy?
Trong project tao ra mấy cái package (gói) để chia ra làm đó mà.
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by pklove1910 Sat Nov 03, 2012 10:51 pm

Trong class server_echo có đoạn code "char pheptoan = data.charAt(0)". báo lỗi là sao vậy Cường. ở trên đã khai báo
Code:
String data = read.nextLine()
pklove1910
pklove1910

Posts : 207
Thanked : 3
Gia Nhập 11/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Sun Nov 04, 2012 9:35 am

pklove1910 đã viết:Trong class server_echo có đoạn code "char pheptoan = data.charAt(0)". báo lỗi là sao vậy Cường. ở trên đã khai báo
Code:
String data = read.nextLine()

Em viết theo cuốn gt lổi em éo biết...
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by huynhtai Sun Nov 04, 2012 2:22 pm

Admin đã viết:Lưu ý khi copy code về nhớ đổi tên class trùng với tên chương trình, nếu có thắc mắc về lỗi code thì xin cai TeamViewer 7 đọc pass và id em sẽ hỗ trợ từ xa, có thể hỏi bác Tài cũng được, em đã truyền nội công cho thiếm ấy

Mấy bài này sao tớ biết chứ?? tongue
huynhtai
huynhtai

Posts : 90
Thanked : 3
Gia Nhập 22/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by huynhtai Sun Nov 04, 2012 2:27 pm

ủa nói z là ngày nào mới thi giửa kỳ z? thứ 3 tuần sau sau nửa là ngày 14/11 phải k? [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn  Operant1  OP  Operant2 1838043209
huynhtai
huynhtai

Posts : 90
Thanked : 3
Gia Nhập 22/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by NeverGiveUp Wed Nov 07, 2012 2:13 am

có phải như vậy không????????
[You must be registered and logged in to see this link.]
NeverGiveUp
NeverGiveUp

Posts : 83
Thanked : 3
Gia Nhập 12/09/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Wed Nov 07, 2012 7:12 am

Đỉnh quá đi thiếm [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn  Operant1  OP  Operant2 4223051845
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Wed Nov 07, 2012 7:18 am

pklove1910 đã viết:bị lỗi không hiểu ở dòng
s = ss.accept();
là sao vậy tâm. Và cái khai báo int port = 7 có thể giải thích rõ hơn được không

- Nếu bị lỗi dòng này bác xem coi có inport cái thư viện java.net vào chưa vì sử dụng Socket và SocketServer.
- Trường hợp mà mình không nhập vào cổng port, chương trình Client và Server mặc định lấy cộng 7 để trao đổi thông tin.
- OK Xong!
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Fri Nov 09, 2012 11:34 am

Em copy bài của Tâm về chạy.. chổ client em chưa khởi động server hoặc nhập port không đúng với port lắng nge của server là cái client nó lặp vô hạn câu: Kết nối Server thất bại! luôn monkey
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Admin Sat Nov 10, 2012 6:40 pm

uhm, dòng lặp while(true) nó vậy để tui rảnh điều chỉnh lại
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by shippou777 Tue Nov 13, 2012 5:55 pm

Đây là hàm processing của server bài + - * / của em.
Với yêu cầu gữi đi từ client vd là: + 3 4 server sẻ check các lổi: toán tử ko là + - * / , toán hạng ko là số, chia cho 0, yêu cầu gữi đi ko đúng mẫu.
A e góp ý:
Code:

public void processing(){//xu ly yeu cau
      try{
         String[] pheptoan = this.data.split(" ");
         try{
            float a = Float.parseFloat(pheptoan[1]);
            float b = Float.parseFloat(pheptoan[2]);
            String toantu = pheptoan[0];      
            if(toantu.compareTo("+")!=0 && toantu.compareTo("-")!=0 && toantu.compareTo("*")!=0
               && toantu.compareTo("/")!=0){
               this.ketqua =  "Toan tu khong ho tro...";
            }else{
               if(toantu.compareTo("+")==0) this.ketqua = ""+(a+b);
               if(toantu.compareTo("-")==0) this.ketqua = ""+(a-b);
               if(toantu.compareTo("*")==0) this.ketqua = ""+(a*b);
               if(toantu.compareTo("/")==0){
                  if(b!=0) this.ketqua = ""+(a/b);
                  if(b==0) this.ketqua = "Phep chia cho 0 khong thuc hien duoc...";
               }
            }
         }catch(NumberFormatException e){
            this.ketqua =  "Toan hang phai la so...";
         }
      }catch(Exception e){
         this.ketqua = "Yeu cau khong dung mau...";
      }
   }
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

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