Created
August 10, 2014 01:52
-
-
Save pori/ddb8c490d46d206cfc45 to your computer and use it in GitHub Desktop.
A simple UDP packet sending command line application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.Scanner; | |
class UDPServer | |
{ | |
public static void main(String args[]) throws Exception | |
{ | |
Scanner in = new Scanner(System.in); | |
System.out.println("Enter UDP client IP address:"); | |
String ip = in.nextLine(); | |
InetAddress ipAddress = InetAddress.getByName(ip); // Attempt connection to UDP client | |
boolean running = false; | |
if (ipAddress.isReachable(3000)) { | |
System.out.println("Connected: " + ipAddress.toString()); | |
running = true; | |
} else { | |
System.out.println("Error establishing connection."); | |
} | |
DatagramSocket clientSocket = new DatagramSocket(); // Socket for sending data | |
clientSocket.connect(ipAddress, 2390); // IP address and port | |
while (running) { | |
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Enter data:"); | |
String data = inFromUser.readLine(); | |
if (data == "exit") { | |
System.out.println("Goodbye"); | |
running = false; | |
} | |
else { | |
System.out.println("Sending data..."); | |
byte[] sendData = new byte[255]; | |
sendData = data.getBytes(); | |
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length); | |
clientSocket.send(sendPacket); | |
System.out.println("Data sent."); | |
} | |
} | |
clientSocket.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment