Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. public int receivemessage(Socket client) {
  2.     int amountBytes = 0;
  3.    
  4.     // This try catch is to trick the socket into being non-blocking when checking for data
  5.     try {
  6.         InputStream stream = client.getInputStream();
  7.         amountBytes = stream.available();
  8.         this.in = new DataInputStream(stream);
  9.     } catch (Exception localException) {
  10.         //
  11.     }
  12.    
  13.     // I there's incoming data, then read it
  14.     int msg = 0;
  15.     if (amountBytes > 0) {
  16.         try {
  17.             msg = this.in.read();
  18.             this.buffer.in.clear();
  19.             this.buffer.inpointer = 0;
  20.             for (int i = 0; i < msg + 1; i++) {
  21.                 int read = this.in.read();
  22.                 if (i > 0)
  23.                     this.buffer.append(read);
  24.             }
  25.         }
  26.         catch (Exception localException1) {
  27.         }
  28.     }
  29.     this.in = null;
  30.     return msg;
  31. }
  32.  
  33. public void sendmessage(Socket socket) {
  34.     try {
  35.         if (socket != null) {
  36.             OutputStream outst = socket.getOutputStream();
  37.  
  38.             BufferedOutputStream bos = new BufferedOutputStream(outst);
  39.             if (bos != null) {
  40.                 bos.write(this.buffer.out.size());
  41.                 bos.write(0);
  42.                 for (int i = 0; i < this.buffer.out.size(); i++) {
  43.                     int bytesend = ((Integer)this.buffer.out.get(i)).intValue();
  44.                     bos.write(bytesend);
  45.                 }
  46.  
  47.                 this.buffer.in.clear();
  48.                 bos.flush();
  49.             }
  50.         } else {
  51.             this.buffer.in.clear();
  52.         }
  53.     } catch (Exception e) {
  54.         //
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement