Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. package userApplication;
  2. import java.net.*;
  3. import java.io.*;
  4. public class userApplication {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.     userApplication app = new userApplication();
  8.     //κωδικοι ιθακης
  9.     String echo = "E1904";
  10.     int serverPort = 38009;
  11.     int clientPort = 48009;
  12.    
  13.     app.echo(echo,serverPort,clientPort);
  14.    
  15.         }
  16.  
  17.  
  18.  
  19. public void requestCode(String cd, int serverPort) throws IOException { //κανει ενα request στην ιθακη για ενα πακετο που θα καθοριστει απο τον κωδικο που θα της βαλω σαν ορισμα
  20.        
  21.     byte[] code = "cd".getBytes();//bytes του κωδικου της ιθακης
  22.        
  23.     byte[] hostIP = { (byte)155,(byte)207,(byte)18,(byte)208 };//Ip ιθακης
  24.     InetAddress hostAddress = InetAddress.getByAddress(hostIP);
  25.    
  26.     DatagramSocket s = new DatagramSocket(); //δημιουργει socket send για να στειλω στον σερβερ τον κωδικο του αιτηματος
  27.    
  28.     DatagramPacket p = new DatagramPacket(code,code.length,hostAddress,serverPort);
  29.    
  30.     s.send(p);
  31.    
  32.     s.close();
  33.    
  34.    
  35.    
  36. }  
  37.    
  38. public void echo(String echoCode,int serverPort, int clientPort) throws IOException {
  39.    
  40.        
  41.     byte[] rxbuffer = new byte[32]; //πινακας που θα δεχτει το response της ιθακης σε Bytes
  42.            
  43.     DatagramSocket r = new DatagramSocket(clientPort); //πυλη για να ερθει το response της ιθακης
  44.     DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length); //εδω θα ερθει το response της ιθακης
  45.        
  46.     r.setSoTimeout(1200);
  47.    
  48.     for (int i = 0 ; i <20 ; i++) {
  49.          try {
  50.              requestCode(echoCode,serverPort);
  51.              r.receive(q);
  52.              String message = new String(rxbuffer,0,q.getLength());
  53.              System.out.println(message);
  54.              
  55.          } catch (Exception x) {
  56.              System.out.println(x);
  57.          }
  58.         }
  59.    
  60. r.close(); 
  61. }
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement