GeeCON Prague 2014*
The moose likes Sockets and Internet Protocols and the fly likes Sending  Hello to Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending  Hello to Server" Watch "Sending  Hello to Server" New topic
Author

Sending Hello to Server

Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16



Facing many errors in Eclipse :

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Demo.main(Demo.java:9)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

That tells you that google.com is not listening for a TCP socket connection on port 43. Or possibly that there's no network path from your host to google.com, but that may give a different error message.

Any particular reason you assumed you'd be able to connect to google.com:43?
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
no particular reason, it was just hit & trial method to check such code.
well thanks for reply.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johhn Davis wrote:no particular reason, it was just hit & trial method to check such code.
well thanks for reply.


Then try port 80. Any site you can get to with your browser using http://some_site.com without specifying a port is listening on port 80. Of course, probably none of them are expecting to receive "Hello to server", so you'll get an error in response. If you want to initiate a real exchange, then try sending


inluding the newline (possibly two newlines--I forget the details), and read the response.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
I think it needs to be "GET / HTTP/1.0" for a proper HTTP request.


Ping & DNS - my free Android networking tools app
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Ulf Dittmer wrote:I think it needs to be "GET / HTTP/1.0" for a proper HTTP request.


Actually, two lines:
GET / HTTP/1.0

(second line blank with no spaces) will work
The protocol requires the second line.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pat Farrell wrote:
Ulf Dittmer wrote:I think it needs to be "GET / HTTP/1.0" for a proper HTTP request.


Actually, two lines:
GET / HTTP/1.0

(second line blank with no spaces) will work
The protocol requires the second line.


It must depend on how rigidly the server enforces the HTTP protocol, or what version it is. I often test connections by telneting to port 80 and just entering GET or GET / followed by one or two newlines.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Jeff Verdegan wrote:I often test connections by telneting to port 80 and just entering GET or GET / followed by one or two newlines.


GET /
followed by to CR is fine.

My point was that you always need the two CR, its not enough to have just the "GET" command
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
After editing

I am receiving different error :

Exception in thread "main" java.net.UnknownHostException: http://www.google.com
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Demo.main(Demo.java:9)


Jeff & Pat, where i am supposed to write GET / HTTP/1.0 ? When I put this at google url i got same error UnknownHostException .
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
The first parameter of the Socket constructor is a host name, not an URL - remove the "http://".

The" GET... " is the message you send instead of the one you're currently using.
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
Now i edited it as :


I didn't get any error . Ulf does it mean success ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Possibly. Try reading what gets sent as response and print it to the console to be sure.
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16
Try reading what gets sent as response and print it to the console to be sure.


I don't yet how to do this (may be i) . I assume it should be like , i am unicasting and it comes back (if so) , i store it in somewhere and watch the output at console.
But i did something. I switched off the Modem , run the code and Got the error.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johhn Davis wrote:Now i edited it as :


I didn't get any error . Ulf does it mean success ?


It means you're connected.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johhn Davis wrote:
Try reading what gets sent as response and print it to the console to be sure.


I don't yet how to do this (may be i) . I assume it should be like , i am unicasting and it comes back (if so) , i store it in somewhere and watch the output at console.


You get the Socket's OutputStream, write "GET /" and two newlines, get the InputStream, read as much of the response as you're interested in, and print it out to the screen as you read it. At least, that's what I would do if I was just playing around with sockets seeing if I understood how they work and wanted to test whether I could send and receive data. If there's some other more specific goal you have, then you may have to do something different to achieve it.


But i did something. I switched off the Modem , run the code and Got the error.


And this surprises you? Are you also surprised when you're unable to call anybody after switching off your phone, and unable to watch any shows after switching off your TV?
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16

I got that.

Jeff thanks for helping me here & in other threads too. Well i still couldn't get how to use that GET/ ,beside i give my best to play with sockets

Sharing a Joke :
" When Your life is in Darkness, pray GOD and ask him to free you from Darkness, Even after you pray, if You are still in Darkness please PAY ELECTRICITY BILL "
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johhn Davis wrote:
Jeff thanks for helping me here & in other threads too.


You're very welcome.

Well i still couldn't get how to use that GET/


Note that the GET / part a) has a space between "GET" and "/" and b) has nothing to do with sockets per se.

The GET / bit is part of the HTTP protocol, which is at a higher layer (Application layer) in the network stack than a simple TCP socket connection (Transport layer). Once you have established a socket connection to a server, how you interact with that server over that connection depends on what Application protocol(s) the server supports. In the examples here, I was assuming a web server supporting the HTTP protocol. When we send a GET command with the name of the resource to get ("/" in my example), the server responds with the contents of that resource. So we write "GET /" followed by two newlines, and then we read whatever the server sends back.

If the server we're connected to is supporting some other protocol rather that HTTP, there will be a different set of valid requests we can send, which will in turn generate a different set of responses.
Johhn Davis
Greenhorn

Joined: Sep 20, 2012
Posts: 16


No error that's good, & No response

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Three comments:
  • There should be a space between "/" and "HTTP" in the message.
  • Why are you wrapping a BufferedWriter inside of another BufferedWriter? That's one too many.
  • How can you say there's no response? There's no code to read a response.
  •  
    GeeCON Prague 2014
     
    subject: Sending Hello to Server