I am trying to learn java. I want to know whats the best way of doing it?
For example I got the code for setting up a TCP server in java which sends the data given in string. Now I want it to send the data dynamically to the client as the user types it.eg a sentence.How to do it? Here are the client and server codes.

Server.java

import java.lang.*;
import java.io.*;
import java.net.*;

class Server {
   public static void main(String args[]) {
      String data = "Toobie ornaught toobie";
      try {
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.print("Server has connected!\n");
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: '" + data + "'\n");
         out.print(data);
         out.close();
         skt.close();
         srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

Client.java

import java.lang.*;
import java.io.*;
import java.net.*;

class Client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));
         System.out.print("Received string: '");

         while (!in.ready()) {}
         System.out.println(in.readLine()); // Read one line and output it

         System.out.print("'\n");
         in.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

Please help me learn java..
thank you.

Edited 5 Years Ago by peter_budo: Keep It Clean - Do wrap your programming code blocks within [code] ... [/code] tags

Do you think it helps by posting this question in the C# forum?

Sorry guys. Mistake..

Thread closed. If you want to follow discussion you can do so here

@ajayb in the future instead of double posting hit "Flag Bad Post" button and ask for post to be move to correct section

This article has been dead for over six months. Start a new discussion instead.