Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

lập trình UDP kiểm tra số chẵn lẻ

Discussion in 'Xây dựng ứng dụng server-client desktop' started by nhoxs2zin, 6/9/13.

  1. nhoxs2zin

    nhoxs2zin Member

    Ai kiểm tra dùm mình cái biến s1 bên udp_server..mình khai báo rồi mà nó vẫn báo lỗi

    PHP:
    package udp_client;

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

    public class 
    UDP_Client {

        public static 
    void main(String[] argsthrows IOException{
            
    DatagramSocket cl = new DatagramSocket();
            
    Scanner nhap = new Scanner(System.in);
            
    int x nhap.nextInt();
            
    byte buf[] = new byte[256];
            
    String s String.valueOf(x);
            
    buf s.getBytes();
            
    InetAddress addsv InetAddress.getByName("127.0.0.1");
            
    int portsv 2000;
            
    DatagramPacket p = new DatagramPacket(buf,buf.length,addsv,portsv);
            
    cl.send(p);
            
    byte buf2[]= new byte[256];
            
    DatagramPacket t = new DatagramPacket(buf2,buf2.length);
            
    cl.receive(t);
            
    String kq = new String(t.getData()).trim();
            
    System.out.print("Ket qua la " +kq);
            
    cl.close();
        }
    }
    PHP:
    package udp_server;

    import java.io.*;
    import java.net.*;

    public class 
    UDP_Server {

        public static 
    void main(String[] argsthrows IOException{
            
    DatagramSocket sv = new DatagramSocket(2000);
            
    byte buf[] = new byte[256];
            
    DatagramPacket q = new DatagramPacket(buf,buf.length);
            
    sv.receive(q);
            
    String kq = new String(q.getData()).trim();
            
    int m Integer.parseInt(kq);
            
    String s1;
            if(
    m%2==0)
            {
                
    s1 "So chan";
            }
            if(
    m%2==1)
            {
                
    s1 "So le";
            }
            
    byte buf2[] = new byte[256];
            
    buf2 s1.getBytes();
            
    InetAddress addcl q.getAddress();
            
    int portcl=q.getPort();
            
    DatagramPacket k = new DatagramPacket(buf2,buf2.length,addcl,portcl);
            
    sv.send(k);
            
    sv.close();
        }
    }
     
  2. Joe

    Joe Thành viên VIP

    =)) full of unnecessary stuff. Why not so ?
    PHP:
            int x nhap.nextInt();
            
    byte buf[] = (x+"").getBytes();
            
    InetAddress addsv InetAddress.getByName("localhost");
    and
    PHP:
            int m Integer.parseInt(kq);
            
    byte buf2[] = ((m%2==0)? "So chan":"So le").getBytes();
     
  3. nhoxs2zin

    nhoxs2zin Member

    Sao kì vậy anh :-/ thầy em dạy là phải khai báo mảng các byte
    PHP:
    byte buf[] = new byte[256];
    .UDP thì nó chỉ gửi theo gói dạng chuỗi nên em nhập số nguyên thì phải chuyển thành chuổi rồi đóng gói mới gửi đi được
    PHP:
    String s String.valueOf(x);
            
    buf s.getBytes();
            
    InetAddress addsv InetAddress.getByName("127.0.0.1");
    Còn code ở trên nó báo lỗi ở s1 mà anh Joe #:-s
     
  4. SITUVN

    SITUVN Well-Known Member

    Cho mình hỏi nó báo lỗi gì? :-/
     
  5. Joe

    Joe Thành viên VIP

    young man
    The beauty of Java is the neatness. Verbose with all kinds of intermediate variables is:
    1) unnecessary,
    2) confusing (see how you confused with your unnecessary s1)
    3) slowing down the whole system
    4) hogging all available resources
    5) starving other apps.
    If you use fuzzy logic to java your UDP you'd have to understand fuzzy logic, too.
    Fuzzy-Logic always assumes something and has 3 states: True, between and false. Your java with s1:
    String s1; <<<<< the between state---neither true (null) nor false (not null)
    if ((m%2)==0) s1 = .... <<< the true state....what happens if m%2 isn't 0 ?
    if ((m%2)==1) s1=... <<< unnecessary, true state....what happens if m%2 isn't 1 ?

    Why not so: String s1 = ((m%2) == 0)? value_0:value_1; ? No Fuzzy = No confusing....
    So, how does Javac knows that you run fuzzy-logic ? [-X
    probably your thầy is a Java-amateur....=))
     
    nhoxs2zin likes this.
  6. nhoxs2zin

    nhoxs2zin Member

    why :-/
    PHP:
    int x nhap.nextInt();
            
    byte buf[] = (x+"").getBytes();
    why not :((
    PHP:
    int x nhap.nextInt();
            
    byte buf[] = new byte[256];
            
    String s String.valueOf(x);
            
    buf s.getBytes();
    Because unnecessary ????????? @-)
     
  7. Joe

    Joe Thành viên VIP

    Young man,
    1) Why you reserve 256 bytes for a little string of several bytes ?
    2) Why you visit Hanoi via Mumbai-Beijing-Hongkong-Hanoi and not directly Saigon-Hanoi ? Are you swimming in Money or does your computer have some tera bytes (TB) ?
    3) where is the difference of the end product ?
    >>>>>>byte buf[] = (x+"").getBytes();<<<<<Saigon-Hanoi
    and
    >>>>>> byte buf[] = new byte[256]....= s.getBytes();<<<<<Saigon-....-Hanoi
    See the difference ?
     
    nhoxs2zin, dvt178 and JackV like this.
  8. nhoxs2zin

    nhoxs2zin Member

    Thanks anh Joe nha...mấy bữa nay lo học mấy môn khác nên cũng không online xem reply của anh :)
     
  9. Joe

    Joe Thành viên VIP

    Want a step higher upwards ? Then:
    PHP:
    int x nhap.nextInt();
    byte buf[] = { (byte) ((0xFF000000) >> 24), (byte)((0xFF0000) >> 16), (byte)((0xFF00) >> 8), (byte)(0xFF) };
    Advantage: only 4 bytes, best prformanceb-)
     
  10. nhoxs2zin

    nhoxs2zin Member

    được rồi anh ơi...cao quá chắc em chịu không nổi :D
     
  11. SITUVN

    SITUVN Well-Known Member

    Nếu thực sự muốn theo lập trình thì cũng đến lúc bạn chạm mặt nó thôi. ;)
     
    nhoxs2zin likes this.
  12. nhoxs2zin

    nhoxs2zin Member

    à anh Joe ơi... phần khai báo
    PHP:
    byte buf[] = new byte[256];
    không cần dùng bên Client,thế còn bên Server thì cần dùng đúng không anh?? vì Server cần phải khai báo
    PHP:
    byte buf[] = new byte[256];
    để nó nhận gói tin do Client gửi đến :-??
     
  13. Joe

    Joe Thành viên VIP

    Young man,
    There's NO requirement at all. Neither on the Client nor on the Server site. UDP stands User Datagram Protocol and it could also mean User "Defined Packet". Also, as a user, YOU must know how many bytes you "traffick" ;) around on the Internet (both Server/Client) without being caught by the Internet-Policeb-). Anyone who says of a fixed number (e.g. 256 bytes) is a vile liar :D Your server expects a number than couldn't be larger than 15 bytes (100.000.000.000.000). Agreed ? So you can code the server as following, too
    PHP:
    import java.io.*;
    import java.net.*;

    public class 
    UDP_Server {

        public static 
    void main(String[] argsthrows IOException{
            
    DatagramSocket sv = new DatagramSocket(2000);
            
    // we expect a number less than 15 digits....
            
    byte buf[] = new byte[15];
            
    DatagramPacket q = new DatagramPacket(buf,buf.length);
            
    sv.receive(q);
            
    String kq = new String(q.getData()).trim();
            
    int m Integer.parseInt(kq);
            
    byte buf2[] = ((m%2==0)? "So chan":"So le").getBytes();
            
    InetAddress addcl q.getAddress();
            
    int portcl=q.getPort();
            
    DatagramPacket k = new DatagramPacket(buf2,buf2.length,addcl,portcl);
            
    sv.send(k);
            
    sv.close();
        }
    }
    If the length exceeds 15 bytes, only the first 15 byte is taken, the rest will be discarded. Therefore it's called UDP;) And the Client:
    PHP:
    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class 
    UDP_Client {

        public static 
    void main(String[] argsthrows IOException{
            
    DatagramSocket cl = new DatagramSocket();
            
    Scanner nhap = new Scanner(System.in);
            
    System.out.print("Xin so: ");
            
    int x nhap.nextInt();
            
    // Ditect Hanoi-Saigon :o)
            
    byte buf[] = (x+"").getBytes();
            
    InetAddress addsv InetAddress.getByName("localhost");
            
    int portsv 2000;
            
    DatagramPacket p = new DatagramPacket(buf,buf.length,addsv,portsv);
            
    cl.send(p);
            
    byte buf2[]= new byte[256];
            
    DatagramPacket t = new DatagramPacket(buf2,buf2.length);
            
    cl.receive(t);
            
    String kq = new String(t.getData()).trim();
            
    System.out.print("Ket qua la " +kq);
            
    cl.close();
        }
    }
    By the way, you can find an UDPServer.java and UDPClient.java here:
    http://congdongjava.com/forum/threads/help-server-udp-trong-java.16608/#post-37284
    where I've set the UDPacket to 1KB (or 1024 bytes).
     

Chia sẻ trang này

Loading...