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 08:10] – [Blocking TCP 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 2: | Line 2: | ||
=== Blocking 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> |
=== Blocking 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(" | + | 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 sockets in Java ==== | ==== Non-blocking TCP sockets in Java ==== | ||
Line 231: | Line 232: | ||
===== Non-blocking loop ===== | ===== Non-blocking loop ===== | ||
- | <code java> | + | <sxh java> |
| | ||
Line 244: | Line 245: | ||
} | } | ||
} | } | ||
- | </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.1678090215.txt.gz · Last modified: 2023/03/06 08:10 by knehez