Socket question

G

GaryM

When creating a socket with new Socket(host, port) the connection is
made as part of the object creation process. If the remote host sends
an initial login text, where does this go? I cannot seem to retrieve
this text, as I create BufferedReader from the InputStream later?
 
S

Shripathi Kamath

GaryM said:
When creating a socket with new Socket(host, port) the connection is
made as part of the object creation process. If the remote host sends
an initial login text, where does this go? I cannot seem to retrieve
this text, as I create BufferedReader from the InputStream later?

Please post snippets of your code. It is unclear as to what you are doing.

Taking stab in the dark, once connected, the data from the remote host
arrive over the socket.
 
G

Gordon Beaton

When creating a socket with new Socket(host, port) the connection is
made as part of the object creation process. If the remote host sends
an initial login text, where does this go? I cannot seem to retrieve
this text, as I create BufferedReader from the InputStream later?

Anything the remote sends will be available on the inputStream
associated with the Socket you created.

This login text you are referring to - apparently you have a
particular protocol in mind that you neglected to share with us. Maybe
it requires you to send some kind of handshake before it sends the
login text. Try sending a newline.

/gordon
 
S

Steve Horsley

When creating a socket with new Socket(host, port) the connection is
made as part of the object creation process. If the remote host sends
an initial login text, where does this go? I cannot seem to retrieve
this text, as I create BufferedReader from the InputStream later?

It will be waiting in the buffers for you to retrieve it.

Are you using BufferedReader.readLine()? If so, remember that readline
will not return a line until it sees an end-of-line sequence. So if a host
says: (see the cursor after the username prompt)

User authentication:
Username: _

then BufferedReader will return the "User authentication:" line, but will
not give you the "Username: " prompt because it hasn't seen a line ending.

Steve.
 
G

GaryM

Anything the remote sends will be available on the inputStream
associated with the Socket you created.

This login text you are referring to - apparently you have a
particular protocol in mind that you neglected to share with us.
Maybe it requires you to send some kind of handshake before it
sends the login text. Try sending a newline.

No particular protocol, but here is my code where I connection to an
SMTP Server. The server always proves 220 Welcome etc... message as
soon as you connect. This code blocks on the readline(). Obviously I
am not understanding something. Nothing new there :) ...

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

public class RemoteSimpleTester {

public static void main(String[] args) {

try {
Socket socket = new Socket("smtp.mail.yahoo.com",
25);

BufferedReader rd =
new BufferedReader(
new InputStreamReader
(socket.getInputStream()));

String line;
while ((line = rd.readLine()) != null) {
System.out.println(rd.readLine());
}

rd.close();
socket.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}
 
G

Gordon Beaton

No particular protocol, but here is my code where I connection to an
SMTP Server. The server always proves 220 Welcome etc... message as
soon as you connect.

That isn't "no particular protocol". If you are talking to an smtp
server you could mention that when you ask for help.

As you've already discovered (and for the benefit of others), you're
incorrectly invoking readLine() twice in the loop, so only every
second line will be displayed.

/gordon
 
G

GaryM

Gordon Beaton said:
That isn't "no particular protocol". If you are talking to an smtp
server you could mention that when you ask for help.

Excuse me? How does it differ from any other common protocol in the
context of the original queston? It doesn't. So I left it generic
because I am working with lots of protocols.
 
G

Gordon Beaton

Excuse me? How does it differ from any other common protocol in the
context of the original queston? It doesn't. So I left it generic
because I am working with lots of protocols.

It was impossible to tell from your initial post whether it mattered
or not. Since I'm not a mindreader I asked, thinking it might help if
you provided more information. On Usenet and elsewhere, there is a
correlation between the information you provide and the help you get.

I only reacted to your contradiction: "no particular protocol"
followed by "smtp".

/gordon
 
G

GaryM

Gordon Beaton said:
It was impossible to tell from your initial post whether it mattered
or not. Since I'm not a mindreader I asked, thinking it might help if
you provided more information. On Usenet and elsewhere, there is a
correlation between the information you provide and the help you get.

I only reacted to your contradiction: "no particular protocol"
followed by "smtp".

I got the answer without providing additional the info. Why? Becuase
the question covered the issue without providing. You should stop
looking for ways to talk down to people. You'll get further. And you
know what they say about sarcasm.

Gary
 
S

Sudsy

GaryM said:
I got the answer without providing additional the info. Why? Becuase
the question covered the issue without providing. You should stop
looking for ways to talk down to people. You'll get further. And you
know what they say about sarcasm.

Gary

Actually, the protocol question was important and I'll tell you
why: not all of them are line-oriented. And even some of them
which are don't necessarily give you a prompt.
Additional information would have been helpful in this case
(and I'm not talking down to you).
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top