tanszek:oktatas:iss_t:java_example_for_blocking_and_non-blocking_socket
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tanszek:oktatas:iss_t:java_example_for_blocking_and_non-blocking_socket [2023/03/06 06:56] – [Blocking UDP based sockets in Java] knehez | tanszek: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 based sockets in Java ==== | + | ==== Blocking TCP sockets in Java ==== |
| - | === Socket server source code === | + | === Blocking |
| - | <code java> | + | <sxh java> |
| + | package org.ait; | ||
| import java.io.IOException; | import java.io.IOException; | ||
| import java.io.ObjectInputStream; | import java.io.ObjectInputStream; | ||
| Line 22: | Line 23: | ||
| try { | try { | ||
| // 1. create a socket server listening to port 8080 | // 1. create a socket server listening to port 8080 | ||
| - | providerSocket = new ServerSocket(8080, 10); | + | providerSocket = new ServerSocket(8080); |
| // 2. waiting for the connection (here we are waiting until next connection) | // 2. waiting for the connection (here we are waiting until next connection) | ||
| connection = providerSocket.accept(); | connection = providerSocket.accept(); | ||
| Line 71: | Line 72: | ||
| } | } | ||
| } | } | ||
| - | </code> | + | </sxh> |
| - | === Socket client source === | + | === Blocking |
| - | <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 based sockets in Java ==== | + | ==== Blocking UDP sockets in Java ==== |
| 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(" | + | InetAddress IPAddress = InetAddress.getByName(" |
| - | + | ||
| - | 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, | + | DatagramPacket sendPacket = new DatagramPacket(sendData, |
| - | clientSocket.send(sendPacket); | + | clientSocket.send(sendPacket); |
| - | + | ||
| - | DatagramPacket receivePacket = new DatagramPacket(receiveData, | + | DatagramPacket receivePacket = new DatagramPacket(receiveData, |
| - | clientSocket.receive(receivePacket); | + | clientSocket.receive(receivePacket); |
| - | String modifiedSentence = new String(receivePacket.getData()); | + | String modifiedSentence = new String(receivePacket.getData()); |
| - | + | ||
| - | System.out.println(" | + | System.out.println(" |
| - | 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, | |
| - | DatagramPacket receivePacket = new DatagramPacket(bytesReceived, | + | // 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(" | |
| - | System.out.println(" | + | |
| - | + | 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, |
| - | DatagramPacket sendPacket = new DatagramPacket(bytesSent, | + | serverSocket.send(sendPacket); |
| - | serverSocket.send(sendPacket); | + | serverSocket.close(); |
| - | serverSocket.close(); | + | |
| - | + | } | |
| - | } | + | } |
| - | } | + | </sxh> |
| - | </code> | + | |
| - | ==== Non-blocking TCP based sockets in Java ==== | + | ==== Non-blocking TCP sockets in Java ==== |
| - | {{tanszek: | + | {{:tanszek: |
| Reading: | Reading: | ||
| Line 230: | Line 231: | ||
| - | ===== Elvi váz ===== | + | ===== Non-blocking loop ===== |
| - | <code java> | + | <sxh java> |
| | | ||
| Line 241: | Line 242: | ||
| if(socketChannel != null){ | if(socketChannel != null){ | ||
| - | // az összeköttetés létrejött | + | // the connection is accepted |
| } | } | ||
| } | } | ||
| - | </code> | + | </sxh> |
| - | ===== Nem blokkolt kliens példa | + | ===== 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> |
| - | ===== Nem blokkolt szerver példa | + | ===== 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.1678085760.txt.gz · Last modified: 2023/03/06 06:56 by knehez
