Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Connecting to a socket and reading from it

Reply
Thread Tools

Connecting to a socket and reading from it

 
 
Thomas OZENNE
Guest
Posts: n/a
 
      10-26-2005
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();
}
}


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-26-2005

On Wed, 26 Oct 2005 14:56:04 +0200, "Thomas OZENNE" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments