SHARE
TWEET

Untitled

a guest Oct 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MAIN.JAVA
  2.  
  3. package org.suai;
  4.  
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws Exception{
  11.         DatagramSocket clientSocket = new DatagramSocket(9876);
  12.         InetAddress IPAddress = InetAddress.getByName("localhost");
  13.         Thread ww = new Thread(new WorkerWriter(clientSocket, IPAddress, 9877));
  14.         Thread wr = new Thread(new WorkerReader(clientSocket));
  15.         ww.start();
  16.         wr.start();
  17.     }
  18. }
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. WORKERREADER.JAVA
  31.  
  32. package org.suai;
  33.  
  34. import java.net.DatagramPacket;
  35. import java.net.DatagramSocket;
  36.  
  37. public class WorkerReader implements Runnable{
  38.  
  39.     private DatagramSocket serverSocket;
  40.     private byte[] receiveData = new byte[1024];
  41.  
  42.     public WorkerReader(DatagramSocket ss){
  43.         serverSocket = ss;
  44.     }
  45.  
  46.     public void run(){
  47.         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  48.         int i = 0;
  49.         while (true) {
  50.             try {
  51.                 serverSocket.receive(receivePacket);
  52.                 String sentence = new String(receivePacket.getData());
  53.                 System.out.println(sentence);
  54.                 while (receiveData[i] != 0)
  55.                     receiveData[i++] = 0;
  56.                 i = 0;
  57.             }
  58.             catch (Exception e){
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. WORKERWRITER.JAVA
  76.  
  77. package org.suai;
  78.  
  79. import java.io.BufferedReader;
  80. import java.io.InputStreamReader;
  81. import java.net.DatagramPacket;
  82. import java.net.DatagramSocket;
  83. import java.net.InetAddress;
  84.  
  85. import static java.lang.System.exit;
  86.  
  87. public class WorkerWriter implements Runnable {
  88.  
  89.     private BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  90.     private DatagramSocket serverSocket;
  91.     private InetAddress IPAddress;
  92.     private int port;
  93.     private String str;
  94.     private String name = "Client";
  95.     private String quit = "@quit";
  96.     private byte[] sendData = new byte[1024];
  97.  
  98.     public WorkerWriter(DatagramSocket ss, InetAddress ia, int p){
  99.         serverSocket = ss;
  100.         IPAddress = ia;
  101.         port = p;
  102.     }
  103.  
  104.     public void run(){
  105.         while (true) {
  106.             try {
  107.                 str = inFromUser.readLine();
  108.                 if (str.equals(quit)){
  109.                     exit(0);
  110.                 }
  111.                 if (str.startsWith("@name")){
  112.                     name = str.substring(6);
  113.                     continue;
  114.                 }
  115.                 str = name + ": " + str;
  116.                 sendData = str.getBytes();
  117.                 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,
  118.                         port);
  119.                 serverSocket.send(sendPacket);
  120.             } catch (Exception e) {
  121.                 e.printStackTrace();
  122.             }
  123.         }
  124.     }
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top