Advertisement
donut188

UDPClient

Dec 8th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class UDPClient
  5. {
  6.     private GUIForClient gfc;
  7.    
  8.     public UDPClient(GUIForClient gfc)
  9.     {
  10.         this.gfc = gfc;
  11.     }
  12.  
  13.     public static void main(String[] args) throws Exception
  14.     {
  15.         int cport = 29841;
  16.         String host = "localhost";
  17.  
  18.         if (args.length < 1)
  19.         {
  20.             System.out.println("You are now connected to the host: " + host + " On port number: " + cport);            
  21.         }
  22.         else{
  23.             cport = Integer.valueOf(args[0]).intValue();
  24.             System.out.println("You are now connected to the host: " + host + " On port number: " + cport);
  25.         }
  26.  
  27.         InetAddress ia = InetAddress.getByName(host);
  28.         SendThread send = new SendThread(ia, cport);
  29.         send.start();
  30.         ReceiveThread receive = new ReceiveThread(send.getSocket());
  31.         receive.start();
  32.     }
  33.  
  34. }
  35.  
  36. class SendThread extends Thread
  37. {
  38.     private InetAddress serverIP;
  39.     private DatagramSocket ds;
  40.     private boolean stopThread = false;
  41.     private int sport;
  42.  
  43.     public SendThread(InetAddress addr, int sport) throws SocketException
  44.     {
  45.         this.serverIP = addr;
  46.         this.sport = sport;
  47.         this.ds = new DatagramSocket();
  48.         this.ds.connect(serverIP, sport);
  49.     }
  50.  
  51.     public void halt()
  52.     {
  53.         this.stopThread = true;
  54.     }
  55.  
  56.     public DatagramSocket getSocket()
  57.     {
  58.         return this.ds;
  59.     }
  60.  
  61.     public void run()
  62.     {
  63.         try
  64.         {
  65.             byte[] b1 = new byte[1024];
  66.             b1 = "".getBytes();
  67.             DatagramPacket dp1 = new DatagramPacket(b1, b1.length, serverIP, sport);
  68.             ds.send(dp1);
  69.  
  70.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  71.  
  72.             while (true)
  73.             {
  74.                 if (stopThread)
  75.                 {
  76.                     return;
  77.                 }
  78.  
  79.                 String messageFromClient = br.readLine();
  80.  
  81.                 if (messageFromClient.equals("."))
  82.                 {
  83.                     break;
  84.                 }
  85.  
  86.                 byte[] sentData = new byte[1024];
  87.  
  88.                 sentData = messageFromClient.getBytes();
  89.  
  90.                 DatagramPacket dp2 = new DatagramPacket(sentData, sentData.length, serverIP, sport);
  91.  
  92.                 System.out.println("Please type your next message.");
  93.  
  94.                 ds.send(dp2);
  95.  
  96.                 Thread.yield();
  97.             }
  98.         }
  99.         catch(IOException ioe)
  100.         {
  101.             System.err.println(ioe);
  102.         }
  103.     }        
  104. }
  105.  
  106. class ReceiveThread extends Thread
  107. {
  108.     private DatagramSocket ds;
  109.     private boolean stopThread = false;
  110.  
  111.     public ReceiveThread(DatagramSocket ds) throws SocketException
  112.     {
  113.         this.ds = ds;
  114.     }
  115.  
  116.     private void halt()        
  117.     {
  118.         this.stopThread = true;
  119.     }
  120.  
  121.     public void run()        
  122.     {
  123.         byte[] receivedData = new byte[1024];
  124.  
  125.         while (true)
  126.         {
  127.             if(stopThread)
  128.             {
  129.                 return;
  130.             }
  131.  
  132.             DatagramPacket dp3 = new DatagramPacket(receivedData, receivedData.length);
  133.             System.out.println("You're in the client!");
  134.             try
  135.             {
  136.                 ds.receive(dp3);
  137.                 System.out.println("I will keep receiving packets...");
  138.  
  139.                 String replyFromServer = new String(dp3.getData(), 0, dp3.getLength());
  140.  
  141.                 System.out.println("UDPClient: Response from the server: \"" + replyFromServer + "\"\n");
  142.  
  143.                 Thread.yield();
  144.             }
  145.             catch(IOException ioe)
  146.             {
  147.                 System.err.println(ioe);
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement