DataOutputStream: writeUTF(String str) : DataOutputStream « java.io « Java by API






DataOutputStream: writeUTF(String str)

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MainClass extends Thread {
  public MainClass() throws IOException {
  }

  public void run() {
    try {
      Socket socket = new Socket("127.0.0.1", 2000);

      DataInputStream in = new DataInputStream(socket.getInputStream());
      BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
      DataOutputStream out = new DataOutputStream(socket.getOutputStream());

      while (true) {
        System.out.print("Enter response: ");
        String response = console.readLine();
        out.writeUTF(response);

        String message = in.readUTF();
        System.out.println(message);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    try {
      Thread t = new MainClass();
      t.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
           
       








Related examples in the same category

1.new DataOutputStream(FileOutputStream fileOutputStream)
2.DataOutputStream: writeBoolean(boolean b)
3.DataOutputStream: writeByte(byte b)
4.DataOutputStream: writeChar(char ch)
5.DataOutputStream: writeDouble(double d)
6.DataOutputStream: writeFloat(float f)
7.DataOutputStream: writeInt(int i)
8.DataOutputStream: writeLong(long l)
9.DataOutputStream: writeShort(int i)