Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Scanner;
- public class Server {
- public static void main(String[] args) {
- try {
- DatagramSocket datagramSocket = new DatagramSocket(1234);
- byte[] recieved_message = new byte[6000];
- DatagramPacket datagramPacket = null;
- while(true) {
- datagramPacket = new DatagramPacket(recieved_message, recieved_message.length);
- datagramSocket.receive(datagramPacket);
- String s = cast(recieved_message).toString();
- System.out.println(s);
- if(s.charAt(0) == 'E' && s.charAt(1) == 'N' && s.charAt(2) == 'D') {
- return;
- }
- recieved_message = new byte[6000];
- }
- }
- catch (IOException i) {
- i.printStackTrace();
- }
- }
- private static StringBuilder cast(byte[] b) {
- StringBuilder s = new StringBuilder();
- for(int i = 0; i < b.length; i++) {
- s.append((char) b[i]);
- }
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement