Connecting to a socket and reading from it

Discussion in 'Java' started by Thomas OZENNE, Oct 26, 2005.

  1. 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();
    }
    }
     
    Thomas OZENNE, Oct 26, 2005
    #1
    1. Advertising

  2. Thomas OZENNE

    Roedy Green Guest

    Roedy Green, Oct 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    5,117
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    1,033
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    601
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    716
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    760
Loading...

Share This Page