User Tools

Site Tools


tanszek:oktatas:iss_t:java_example_for_blocking_and_non-blocking_socket

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:iss_t:java_example_for_blocking_and_non-blocking_socket [2023/03/06 07:06] – [Non-blocking TCP sockets in Java] kneheztanszek:oktatas:iss_t:java_example_for_blocking_and_non-blocking_socket [2024/03/04 08:00] (current) – [Blocking UDP sockets in Java] knehez
Line 1: Line 1:
 ==== Blocking TCP sockets in Java ==== ==== Blocking TCP sockets in Java ====
  
-=== Socket server source code === +=== Blocking Socket server source code === 
-<code java>+<sxh java> 
 +    package org.ait;
     import java.io.IOException;     import java.io.IOException;
     import java.io.ObjectInputStream;     import java.io.ObjectInputStream;
Line 71: Line 72:
     }     }
     }     }
-</code+</sxh
-=== Socket client source === +=== Blocking Socket client source === 
-<code java>+<sxh java> 
 +    package org.ait;
     import java.io.IOException;     import java.io.IOException;
     import java.io.ObjectInputStream;     import java.io.ObjectInputStream;
Line 137: Line 139:
     }     }
     }     }
-</code>+</sxh>
  
 ==== Blocking UDP sockets in Java ==== ==== Blocking UDP sockets in Java ====
Line 143: Line 145:
 The following Agent sends a message and waits for a response on port 8080, also with UDP. Older versions of the Eclipse IDE, the text you type on the console can be sent by pressing ctrl+z The following Agent sends a message and waits for a response on port 8080, also with UDP. Older versions of the Eclipse IDE, the text you type on the console can be sent by pressing ctrl+z
  
-<code java> +<sxh java> 
-  package org.ait; +package org.ait; 
-   + 
-  import java.io.BufferedReader; +import java.io.BufferedReader; 
-  import java.io.InputStreamReader; +import java.io.InputStreamReader; 
-  import java.net.DatagramPacket; +import java.net.DatagramPacket; 
-  import java.net.DatagramSocket; +import java.net.DatagramSocket; 
-  import java.net.InetAddress; +import java.net.InetAddress; 
-   + 
-  public class UDPClient { +public class UDPClient { 
- public static void main(String args[]) throws Exception { +    public static void main(String args[]) throws Exception { 
- BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); +        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
- DatagramSocket clientSocket = new DatagramSocket(); +        DatagramSocket clientSocket = new DatagramSocket(); 
- InetAddress IPAddress = InetAddress.getByName("localhost"); +        InetAddress IPAddress = InetAddress.getByName("localhost"); 
-  + 
- byte[] sendData = new byte[1024]; +        byte[] sendData = new byte[1024]; 
- byte[] receiveData = new byte[1024]; +        byte[] receiveData = new byte[1024]; 
-  + 
- String sentence = inFromUser.readLine(); +        String sentence = inFromUser.readLine(); 
- sendData = sentence.getBytes(); +        sendData = sentence.getBytes(); 
-  + 
- DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 8080); +        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 8080); 
- clientSocket.send(sendPacket); +        clientSocket.send(sendPacket); 
-  + 
- DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); +        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
- clientSocket.receive(receivePacket); +        clientSocket.receive(receivePacket); 
- String modifiedSentence = new String(receivePacket.getData()); +        String modifiedSentence = new String(receivePacket.getData()); 
-  + 
- System.out.println("converted:" + modifiedSentence); +        System.out.println("converted:" + modifiedSentence); 
- clientSocket.close(); +        clientSocket.close(); 
-+    
-+
-</code>+</sxh>
  
 The UDP server waits for the agents messages on port 8080 and converts them to uppercase letters and sends them back to the client UDP socket. The UDP server waits for the agents messages on port 8080 and converts them to uppercase letters and sends them back to the client UDP socket.
  
-<code java> +<sxh java> 
-  package org.ait; +package org.ait; 
-   + 
-  import java.net.DatagramPacket; +import java.net.DatagramPacket; 
-  import java.net.DatagramSocket; +import java.net.DatagramSocket; 
-  import java.net.InetAddress; +import java.net.InetAddress; 
-   + 
-  public class UDPServer { +public class UDPServer { 
- public static void main(String args[]) throws Exception { +    public static void main(String args[]) throws Exception { 
-  + 
- DatagramSocket serverSocket = new DatagramSocket(8080); +        DatagramSocket serverSocket = new DatagramSocket(8080); 
-  + 
- byte[] bytesReceived = new byte[1024]; +        byte[] bytesReceived = new byte[1024]; 
- byte[] bytesSent = new byte[1024]; +        byte[] bytesSent = new byte[1024]; 
-  + 
-  +        DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length); 
- DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length); +        // here we are waiting for the packets 
- // here we are waiting for the packets +        serverSocket.receive(receivePacket); 
- serverSocket.receive(receivePacket); + 
-  +        String textMessage = new String(receivePacket.getData()); 
- String textMessage = new String(receivePacket.getData()); + 
-  +        System.out.println("I got: " + textMessage); 
- System.out.println("I got: " + textMessage); + 
-  +        InetAddress IPAddress = receivePacket.getAddress(); 
- InetAddress IPAddress = receivePacket.getAddress(); +        int port = receivePacket.getPort(); 
- int port = receivePacket.getPort(); + 
-  +        String upperCaseText = textMessage.toUpperCase(); 
- String upperCaseText = textMessage.toUpperCase(); +        bytesSent = upperCaseText.getBytes(); 
- bytesSent = upperCaseText.getBytes(); + 
-  +        // send back 
- // send back +        DatagramPacket sendPacket = new DatagramPacket(bytesSent, bytesSent.length, IPAddress, port); 
- DatagramPacket sendPacket = new DatagramPacket(bytesSent, bytesSent.length, IPAddress, port); +        serverSocket.send(sendPacket); 
- serverSocket.send(sendPacket); +        serverSocket.close(); 
- serverSocket.close(); + 
-  +    
-+
-  +</sxh>
-</code>+
  
 ==== Non-blocking TCP sockets in Java ==== ==== Non-blocking TCP sockets in Java ====
-{{:tanszek:oktatas:iss_t:sockets-blocking-nonblocking.png?400|}}+{{:tanszek:oktatas:iss_t:sockets-blocking-nonblocking.png?800|}}
  
 Reading:  Reading: 
Line 231: Line 232:
  
 ===== Non-blocking loop ===== ===== Non-blocking loop =====
-<code java>+<sxh java>
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        
Line 241: Line 242:
        
     if(socketChannel != null){     if(socketChannel != null){
-        // az összeköttetés létrejött+        // the connection is accepted
     }     }
    }    }
-</code>+</sxh>
  
  
 ===== Non-blocking Java client example ===== ===== Non-blocking Java client example =====
  
-<code java>+<sxh java>
   import java.io.IOException;   import java.io.IOException;
   import java.io.InputStream;   import java.io.InputStream;
Line 316: Line 317:
   }   }
  
-</code>+</sxh>
  
  
 ===== Non-blocking Java server example ===== ===== Non-blocking Java server example =====
-<code java>+<sxh java>
   public class Server implements Runnable {   public class Server implements Runnable {
  // The port we will listen on  // The port we will listen on
Line 450: Line 451:
  }  }
   }   }
-</code>+</sxh>
  
tanszek/oktatas/iss_t/java_example_for_blocking_and_non-blocking_socket.1678086375.txt.gz · Last modified: 2023/03/06 07:06 by knehez