Created
May 21, 2016 09:05
-
-
Save aserrallerios/71a40d395b4c0a1ed5c6f04a642198b2 to your computer and use it in GitHub Desktop.
UDP Server
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.*; | |
class UDPServer | |
{ | |
public static void main(String args[]) throws Exception | |
{ | |
DatagramSocket serverSocket = new DatagramSocket(9876); | |
byte[] receiveData = new byte[1024]; | |
byte[] sendData = new byte[1024]; | |
while(true) | |
{ | |
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); | |
serverSocket.receive(receivePacket); | |
String sentence = new String( receivePacket.getData()); | |
System.out.println("RECEIVED: " + sentence); | |
InetAddress IPAddress = receivePacket.getAddress(); | |
int port = receivePacket.getPort(); | |
String capitalizedSentence = sentence.toUpperCase(); | |
sendData = capitalizedSentence.getBytes(); | |
DatagramPacket sendPacket = | |
new DatagramPacket(sendData, sendData.length, IPAddress, port); | |
serverSocket.send(sendPacket); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment