Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Seems like I can't read from a scoket?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 152
    VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 152
    VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic