Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class EchoThread extends Thread {
  9.     protected Socket socket;
  10.  
  11.     public EchoThread(Socket clientSocket) {
  12.         this.socket = clientSocket;
  13.     }
  14.  
  15.     public void run() {
  16.         InputStream inp = null;
  17.         BufferedReader brinp = null;
  18.         DataOutputStream out = null;
  19.         try {
  20.             inp = socket.getInputStream();
  21.             brinp = new BufferedReader(new InputStreamReader(inp));
  22.             out = new DataOutputStream(socket.getOutputStream());
  23.         } catch (IOException e) {
  24.             return;
  25.         }
  26.         String line;
  27.         while (true) {
  28.             try {
  29.                 String playerName = "";
  30.  
  31.                 out.writeBytes("WERBIMSTDU\n\r");
  32.                 out.flush();
  33.  
  34.                 line = brinp.readLine();
  35.                 Pattern ibimsPattern = Pattern.compile("^IBIMS#.+");
  36.                 Matcher ibimsMatcher = ibimsPattern.matcher(line);
  37.  
  38.                 if(ibimsMatcher.matches()) {
  39.                     playerName = line.substring(6);
  40.                     out.writeBytes("HALLO#" + playerName + "\n\r");
  41.                     out.flush();
  42.                 } else {
  43.                     out.writeBytes("Du bimst 1 Lauch.\n\r");
  44.                 }
  45.  
  46.                 //out.writeBytes("Server sagt: " + line + "\n\r");
  47.                 //out.flush();
  48.             } catch (IOException e) {
  49.                 e.printStackTrace();
  50.                 return;
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement