Connecting to a socket and reading from it

T

Thomas OZENNE

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();
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top