tanszek:oktatas:java_example_for_blocking_and_non-blocking_socket
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
tanszek:oktatas:java_example_for_blocking_and_non-blocking_socket [2023/03/05 16:15] – knehez | tanszek:oktatas:java_example_for_blocking_and_non-blocking_socket [2023/03/05 16:18] (current) – knehez | ||
---|---|---|---|
Line 140: | Line 140: | ||
==== Non-blocking TCP based sockets in Java ==== | ==== Non-blocking TCP based 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 | ||
+ | |||
+ | <code java> | ||
+ | package org.ait; | ||
+ | | ||
+ | import java.io.BufferedReader; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.net.DatagramPacket; | ||
+ | import java.net.DatagramSocket; | ||
+ | import java.net.InetAddress; | ||
+ | | ||
+ | public class UDPClient { | ||
+ | public static void main(String args[]) throws Exception { | ||
+ | BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); | ||
+ | DatagramSocket clientSocket = new DatagramSocket(); | ||
+ | InetAddress IPAddress = InetAddress.getByName(" | ||
+ | |||
+ | byte[] sendData = new byte[1024]; | ||
+ | byte[] receiveData = new byte[1024]; | ||
+ | |||
+ | String sentence = inFromUser.readLine(); | ||
+ | sendData = sentence.getBytes(); | ||
+ | |||
+ | DatagramPacket sendPacket = new DatagramPacket(sendData, | ||
+ | clientSocket.send(sendPacket); | ||
+ | |||
+ | DatagramPacket receivePacket = new DatagramPacket(receiveData, | ||
+ | clientSocket.receive(receivePacket); | ||
+ | String modifiedSentence = new String(receivePacket.getData()); | ||
+ | |||
+ | System.out.println(" | ||
+ | clientSocket.close(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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> | ||
+ | package org.ait; | ||
+ | | ||
+ | import java.net.DatagramPacket; | ||
+ | import java.net.DatagramSocket; | ||
+ | import java.net.InetAddress; | ||
+ | | ||
+ | public class UDPServer { | ||
+ | public static void main(String args[]) throws Exception { | ||
+ | |||
+ | DatagramSocket serverSocket = new DatagramSocket(8080); | ||
+ | |||
+ | byte[] bytesReceived = new byte[1024]; | ||
+ | byte[] bytesSent = new byte[1024]; | ||
+ | |||
+ | |||
+ | DatagramPacket receivePacket = new DatagramPacket(bytesReceived, | ||
+ | // here we are waiting for the packets | ||
+ | serverSocket.receive(receivePacket); | ||
+ | |||
+ | String textMessage = new String(receivePacket.getData()); | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | InetAddress IPAddress = receivePacket.getAddress(); | ||
+ | int port = receivePacket.getPort(); | ||
+ | |||
+ | String upperCaseText = textMessage.toUpperCase(); | ||
+ | bytesSent = upperCaseText.getBytes(); | ||
+ | |||
+ | // send back | ||
+ | DatagramPacket sendPacket = new DatagramPacket(bytesSent, | ||
+ | serverSocket.send(sendPacket); | ||
+ | serverSocket.close(); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
tanszek/oktatas/java_example_for_blocking_and_non-blocking_socket.1678032928.txt.gz · Last modified: 2023/03/05 16:15 by knehez