Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket problem  RSS feed

 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am testing out a sample client Socket that displays time and it works fine :

but I am trying to write a class that separates each function
1) Obtain a socket
2) Obtain a connection to host,port (Socket sock, String address,int portNo)
3) Read from server (Socket sock)
4) Write to server (Socket sock)
5) Close connections (Socket sock)
6) Close sockets (Socket sock)

but when I break the above code into these functions and call them from main it does't work anymore.
Any suggestions?
Thanks
-Deb
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I meant close the streams not connections ( Line 5).
Thanks,
-Deb
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debbie,
I'm assuming that your are still trying to connect to Uncle Sam's time server in Boulder with your new program. If that is the case, it's never going to work. Why? Because it uses an established protocol. To use that service you have to accept the protocol, namely connect, open up an input stream and read what it sends and be content with the fact that it immediately closes the connection after transmitting the time data.
Now if you want to establish your own protocol there is nothing to stop you. For the server, you create a ServerSocket on whatever port you want your service to run on. For the client you do just like you did in the above code only to the port and host where your fledgling service is running. You will basically setup an infinite loop (while(true)) on the server and wait for client connections:

Once the connection is established, you start the communication between client and server. Who starts the communication? That's up to you, this is your protocol. The server can send a banner like: "Greetings from Debbie's Server", or you can use a more paranoid approach and insist that the client start talking first with a prescribed request. Either way, for client and server, you open the Sockets's InputStream for listening and the OutputStream for talking.
Hope this help,
Michael Morris
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Michael,
Thanks for the explanation or I might have been trying to do that forever!
My problem with a homegrown server is that it keeps refusing connections or has them reset or has someother I/O problem. Hence the Uncle Sam server.
I know I haven't described the problem accurately but please let me know if you know why I get these problems.
Thanks,
-Deb
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debbie,
Lots of things can go wrong with client to server connections, especially in a world full of firewalls and security paranoya.
To find out what's going on, you are generally better off getting the server running first. Assuming that the server is serving up plain text, you can use telnet to connect to it once it is running and test it. Let's say you have your server running on port 1953 and when a client connects, it sends a banner and then waits for a client response. Then the telnet session would go something like this:

telnet localhost 1953
Connection To localhost...
"Greetings from Debbie's server"
...
further communication between client and server
...
Connection lost

Note that you may not be able to see what you type in some implementations of telnet (Microsoft's NT/2000 version in particular) but you should be able to get most of the bugs out of your server like this.
Next you move on to the client. Try connecting from the localhost first. So in our above example you would create a Socket thus:

Once you get that going, then try connecting from another machine on the same LAN. Finally, you try to connect thru the internet.
Lots of things can happen to prevent your client from connecting to the server, especially from the internet. If your server is behind a firewall, you have to be sure that your service port is passed thru to the local IP where your service is running. If the server is behind a NAT server then you can just about forget ever connecting to it from outside.
Also be sure you use a port above 1024 and make sure that the port you choose does not have some other service already running on it.
Hope his helps,
Michael Morris
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Michael,
Thanks for the pointers. I will try exactly that.
-Deb
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!