Client.java 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package clientest;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. public class Client {
  8. public Client(){
  9. try {
  10. Socket client = new Socket("localhost", 5499);
  11. Thread test = new Thread(new Runnable() {
  12. @Override
  13. public void run() {
  14. DataInputStream input;
  15. try {
  16. input = new DataInputStream(client.getInputStream());
  17. while(true){
  18. try {
  19. System.out.println(input.readUTF());
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. });
  29. test.start();
  30. while(true){
  31. DataOutputStream output = new DataOutputStream(client.getOutputStream());
  32. output.writeUTF("test123");
  33. try {
  34. Thread.sleep(1000);
  35. } catch (InterruptedException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. } catch (UnknownHostException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }