Guest User

Untitled

a guest
Jan 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. Det som jag vill koppla till
  2.  
  3.  
  4. // User calls this method to add text to the scroll pane.
  5.    // Special code has to be called to prevent threading problems in the Swing classes.
  6.    public void setText(String text)
  7.    {
  8.       EventQueue.invokeLater( new SetText(text) );
  9.    }
  10.  
  11.    private class SetText implements Runnable
  12.    {
  13.       private String text;
  14.  
  15.       public SetText(String text)
  16.       {
  17.          this.text = text;
  18.       }
  19.  
  20.       public void run()
  21.       {
  22.          textArea.append(text);
  23.       }
  24.  
  25.  
  26.  
  27. 77777777777777777777777777777777777777777777777777777777777777777777777777777777777777777
  28. Mitt krafs
  29.  
  30.  
  31.  
  32.  
  33. // This method is called from the GUI when the user has inputed some text
  34.     // and either pressed the enter key or pushed the send button.
  35.     public static void textEntered(String text){
  36.        
  37.         createSocket();
  38.         getStream();
  39.        
  40.         //Hur hämta från gui?
  41.         //setText();
  42.        
  43.         while(true){
  44.            
  45.        
  46.             //Skapar en bytebuffer som ska hålla meddelandet, som max får vara 1024 bytes långt
  47.             byte[] sendData = new byte[1024];
  48.        
  49.    
  50.             System.out.println("Skriv: " + text);
  51.            
  52.             try{
  53.                 text = inFromServer.readLine();
  54.             }
  55.             catch(IOException e){
  56.                 e.printStackTrace();
  57.                 System.out.println("there was a problem and the keyboard could not be read.");
  58.             }
  59.            
  60.             //Sparar undan meddelandet i den tomma buffern
  61.             sendData = text.getBytes();
  62.            
  63.             int packetSize = sendData.length;
  64.            
  65.             //Skapa ett datagrampacket med data, adress och portnummer
  66.             DatagramPacket sendPacket = new DatagramPacket(sendData, packetSize, adress, port);
  67.            
  68.             //Skickar paketet till servern
  69.             try {
  70.                 outToServer.writeBytes(sendPacket+ "\n" + "\r");
  71.             } catch (IOException e1) {
  72.                 // TODO Auto-generated catch block
  73.                 e1.printStackTrace();
  74.             }
  75.            
  76.             try{
  77.                 while(true){
  78.                    
  79.                     while((text = inFromUser.readLine()) != null){
  80.                         sentence = inFromUser.readLine(); //läser in texten från skrivbordet
  81.                         outToServer.writeBytes(sentence + "\n" + "\r"); //skickar texten till servern
  82.                         outToServer.flush();
  83.                         System.out.println("You said: " + text);
  84.                        
  85.                     }
  86.                 }
  87.                
  88.             }
  89.             catch(Exception e){
  90.                 e.printStackTrace();
  91.             }
  92.         }
  93.     }
Add Comment
Please, Sign In to add comment