This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Seems like I can't read from a scoket? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Seems like I can Watch "Seems like I can New topic
Author

Seems like I can't read from a scoket?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi

I'm currently trying to write a chat program, and a server program acting manager for the chat clients.

Evrything works just fine until I reach the chat part, the client can connect to the server, but it seems that the server never gets the message.

here is the code:

WARNING: At first i tryed to keep the coding in nice order and do evrything by the book, but as I've tryed to fix it MANY times, it ended up looking sort of like a mess. I've also done quite some things against the "rules" such as doing no recovery code to an exception.



and the server:


I hope it was not too messy.

Anyway, does anyone know what is wrong here?

Thanks in advance!
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149


the main problem is in your setupServer() method.

You create a ServerSocket and start listening on it.

Then, when a client comes in, the server accepts the client,
writes a message and sets the client socket (sock) to null.

You already have a ClientHandler but you are not using it.

Your server should run in loop after setting up the listen
socket (ServerSocket).
For each incoming client it should then create a new
ClientHandler and pass the client socket (Socket)
to that client handler.

Then it will work.

Another problem is:
your server and client are Java programs running in
a JVM each. When closing the window, the JVM does not
get terminated (System.exit()).
You can fix that by adding

to your code.

Hope that helps
Matt
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thanks you very much!

It helped quite a lot

And thanks for the thing about "killing" the JVM, before I had to manualy shut it down each time I tested it .
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Okay so I re-wrote a lot of the code and it ended up like this:





(The ip adress is replaced by my ip adress, which i found by googeling "my ipadress" and a coulpe of homepages told me the same adress, so I just took that)

It works fine when i try to connect from my laptop, or with local adress on my own machine, but a sent the client to two friends, but none of them could connect.

Is there something special to know when people are connecting from another network? (thier firewall was turned off during the try)
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

let's deal with the code first

Nice job Hans, you got it working!

  • your setupServer() method really should only set up the server


  • you could move the threadList, handlerList and serverSocket instantiation to another method and rename it (e.g. serverLoop())


  • try to separate the GUI (Swing) part from everything else, makes it easier when it comes to dealing with telAll() etc


  • And:
    what if a thread looses connection? will it be properly terminated and removed from the list?

    You could introduce a thread group and a vulture thread for that group that goes for all
    the cadavers you leave behind

    I find David Flanagan's example quite useful.



    The Connection Thread object is the equivalent to your ClientHandler.

    Matt
    Matt Cartwright
    Ranch Hand

    Joined: Aug 25, 2008
    Posts: 149

    and now about the network problem

    let's assume you are connected to the Net via DSL.
    Event hough this connection is durable your provider
    probably issues you a new IP address every now and then.

    Unless you are using a static IP address from your ISP or
    dynamic DNS the IP address your friends use to connect
    will change.

    So this is how we can get to your front door.

    Then there is the firewall.

    You say it works from your laptop. So I assume you are connecting
    from your laptop to a 192.168.x.x or 10.x.x.x address.

    These are private addresses and cannot be routed to the Net.

    You will have to enable port forwarding on the firewall for port 5323 and
    have it pointing to the IP address you can connect from your laptop.
    This should be the same address used by your server ("myIpAddress").

    Hope that helps
    Matt






    Hans vogn
    Ranch Hand

    Joined: Feb 04, 2009
    Posts: 46
    So if I want people from the outside world to connect to my server, I have to mess with port forwarding?
    Matt Cartwright
    Ranch Hand

    Joined: Aug 25, 2008
    Posts: 149

    that is one of the many options

    let's assume you have a switch / hub with a built-in DSL modem
  • port forwarding is the recommended way
  • you also could configure the chat machine to be in the Demilitarized Zone (DMZ)


  • In the latter case you have to make sure your firewall is tight.

    I have two SHH port-forwarders configured and one for HTTP.
    Every night I have an average of 1,800 break in attempts
    between them.

    Maybe you should implement a bit of protocol on your chat server
    to detect valid connect requests.

    Password authentication would be a plus before you allow any
    client to send and receive chat messages.

    Hope that helps
    Matt





    Hans vogn
    Ranch Hand

    Joined: Feb 04, 2009
    Posts: 46
    I think I have just to try make the server better, and acept that I can't connent with people from the outside world. I don't really know much about internet related stuff like DMZ, and beside from that, I can't even connect to my router as I don't have the password for it.

    But I'll try to improve the server.

    By the way, i find this forum really helpfull and friendly, thansk for the nice responses
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: Seems like I can't read from a scoket?
     
    Similar Threads
    Chapter 15 SimpleChatClient
    How do you formally disconect a client from a server?
    Best way for login authentication
    While loop problem
    I need help improving this chat server and chat client!