Java Socket send text to server
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Main { public static void main(String argv[]) throws Exception { String fact;/*from w w w.j av a 2s . c o m*/ // create a socket to the server Socket clientEnd = new Socket("localhost", 6789); System.out.println("Connected to localhost at port 6789"); // get streams PrintWriter toServer = new PrintWriter(clientEnd.getOutputStream(), true); BufferedReader fromServer = new BufferedReader(new InputStreamReader(clientEnd.getInputStream())); BufferedReader fromUser = new BufferedReader(new InputStreamReader(System.in)); // get an integer from user System.out.print("Enter an integer: "); String n = fromUser.readLine(); // send it to server toServer.println(n); System.out.println("Sent to server: " + n); // retrieve result fact = fromServer.readLine(); System.out.println("Received from server: " + fact); // close the socket clientEnd.close(); } }