Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Connecting to a socket and reading from it

Thread view: 
Thomas OZENNE - 26 Oct 2005 13:56 GMT
Hello,

I'm trying to connect to a socket and read from it just the code loop on
read() can someone help? what can I do ?

The code :
import java.io.*;
import java.net.*;

public class NewsTest {
   public static void main(String[] args) throws IOException {

       Socket echoSocket = null;
       PrintWriter out = null;
       InputStreamReader in = null;

       try {
           echoSocket = new Socket("smtp.free.fr", 25);
           out = new PrintWriter(echoSocket.getOutputStream(), true);
           in = new InputStreamReader(echoSocket.getInputStream());
       } catch (UnknownHostException e) {
           System.err.println("Don't know about host: taranis.");
           System.exit(1);
       } catch (IOException e) {
           System.err.println("Couldn't get I/O for "
                              + "the connection to: taranis.");
           System.exit(1);
       }

BufferedReader stdIn = new BufferedReader(
                                  new InputStreamReader(System.in));
String userInput= new String();
String output = new String();
int c =0;

while (userInput!="end") {
  while((c = in.read())>= 0)
 {
   System.out.print((char)c);
  }
  userInput = stdIn.readLine();
  out.println(userInput);
  System.out.println(userInput);
}

 System.out.println("end");
out.close();
in.close();
stdIn.close();
echoSocket.close();
   }
}
Roedy Green - 26 Oct 2005 15:32 GMT
>I'm trying to connect to a socket and read from it just the code loop on
>read() can someone help? what can I do ?

see http://mindprod.com/jgloss/readblocking.html
http://mindprod.com/applets/fileio.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.