Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int receivemessage(Socket client) {
- int amountBytes = 0;
- // This try catch is to trick the socket into being non-blocking when checking for data
- try {
- InputStream stream = client.getInputStream();
- amountBytes = stream.available();
- this.in = new DataInputStream(stream);
- } catch (Exception localException) {
- //
- }
- // I there's incoming data, then read it
- int msg = 0;
- if (amountBytes > 0) {
- try {
- msg = this.in.read();
- this.buffer.in.clear();
- this.buffer.inpointer = 0;
- for (int i = 0; i < msg + 1; i++) {
- int read = this.in.read();
- if (i > 0)
- this.buffer.append(read);
- }
- }
- catch (Exception localException1) {
- }
- }
- this.in = null;
- return msg;
- }
- public void sendmessage(Socket socket) {
- try {
- if (socket != null) {
- OutputStream outst = socket.getOutputStream();
- BufferedOutputStream bos = new BufferedOutputStream(outst);
- if (bos != null) {
- bos.write(this.buffer.out.size());
- bos.write(0);
- for (int i = 0; i < this.buffer.out.size(); i++) {
- int bytesend = ((Integer)this.buffer.out.get(i)).intValue();
- bos.write(bytesend);
- }
- this.buffer.in.clear();
- bos.flush();
- }
- } else {
- this.buffer.in.clear();
- }
- } catch (Exception e) {
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement