User Tools

Site Tools


tanszek:oktatas: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
tanszek:oktatas:java_example_for_blocking_and_non-blocking_socket [2023/03/05 16:15] kneheztanszek: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("localhost");
 +
 + byte[] sendData = new byte[1024];
 + byte[] receiveData = new byte[1024];
 +
 + String sentence = inFromUser.readLine();
 + sendData = sentence.getBytes();
 +
 + DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 8080);
 + clientSocket.send(sendPacket);
 +
 + DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
 + clientSocket.receive(receivePacket);
 + String modifiedSentence = new String(receivePacket.getData());
 +
 + System.out.println("converted:" + modifiedSentence);
 + clientSocket.close();
 + }
 + }
 +</code>
 +
 +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, bytesReceived.length);
 + // here we are waiting for the packets
 + serverSocket.receive(receivePacket);
 +
 + String textMessage = new String(receivePacket.getData());
 +
 + System.out.println("I got: " + textMessage);
 +
 + InetAddress IPAddress = receivePacket.getAddress();
 + int port = receivePacket.getPort();
 +
 + String upperCaseText = textMessage.toUpperCase();
 + bytesSent = upperCaseText.getBytes();
 +
 + // send back
 + DatagramPacket sendPacket = new DatagramPacket(bytesSent, bytesSent.length, IPAddress, port);
 + serverSocket.send(sendPacket);
 + serverSocket.close();
 +
 + }
 +  }
 +</code>
  
tanszek/oktatas/java_example_for_blocking_and_non-blocking_socket.1678032928.txt.gz · Last modified: 2023/03/05 16:15 by knehez