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.

Recommended Answers

All 4 Replies

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

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

I do, since java is a poor man's C#

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.