Jump to content

Java server application


Perakp

Recommended Posts

Hello,

 

I've written a simple chat application with Java, following easily found tutorials from the internet. I've created a multithreaded serverside to handle multiple connections and a simple but neat chat window.

First I tested the application using getLocalHost() method:

int port=2004;
InetAddress address=InetAddress.getLocalHost();
           requestSocket = new Socket(address, port);

The chat server worked nicely even with multiple chat windows open, each message sent was received by the others etc.

Next, I got the thing working using my internal Ip address, or the ip address given by my router.

 

Now my problem is, how can I get the thing working from any computer, how do I actually connect two computers through these sockets? So far I've only connected the server to the client on the same computer, but that's of no use unless you enjoy chatting with yourself.

 

Here's what I've tried and looked into so far:

- router settings. I've told the router to forward outside traffic to my chosen port (2004). The router settings ask for the name of the application. Do I need to put something there or is it just to look nice? What does the router care what application uses the incoming data?

- firewall settings. Now the funny thing is, I'm doing this on a Mac, and there is no way to configure the firewall to open certain ports, it "automagically" opens ports for certain trusted applications (Eclipse included) Turning the firewall off didn't change anything.

- Does it matter that my internet is provided by my university? Is it possible that they have something in place that makes this impossible?

 

 

Here are the related parts of my code:

Server side:

/**
  * Wait for connection attempts and hand out the sockets to threads that manage the connection.
  */
   public void run()
   {
       try{
       serverSocket = new ServerSocket(2004, 10);
       while(running){
           System.out.println("Waiting for connection");
           Socket clientSocket=serverSocket.accept();
           ClientServiceThread [bleep]hread = new ClientServiceThread(clientSocket, id++, this);
           clients[id]=[bleep]hread;
           [bleep]hread.start();
       }
       }catch(Exception e){
           //
       }  

   }

Client side:

/**
    * Connects to the server
    */
   public void connect(){
       try{
           //1. get the socket
           int port=2004
          InetAddress address=InetAddress.getByName(myExternalIpAddress);
           requestSocket = new Socket(address, port);
           //2. get streams
           out = new ObjectOutputStream(requestSocket.getOutputStream());
           out.flush();
           in = new ObjectInputStream(requestSocket.getInputStream());
   }
       catch(Exception e){
          System.out.println("Connection failed");
       }
       }

 

 

If you are experience with this kind of thing, please share your knowledge! =)

Link to comment
Share on other sites

The name should just be cosmetic. It's the port number that matters.

 

Not very familiar with Macs. Can you add your app as a trusted one?

 

It's possible. Unlikely however.

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

You could try to telnet to your server from your client (open command prompt, type telnet "IP HERE" "PORT HERE" and see if you get a response. If you server is correctly listening on the port, you should get a response.

 

I'm not an expert on telnet though so that's really just a guess.

 

You could also just try a ping and tracert to the server IP to see if any problems arise there.

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

got it working, woot

What did you do/what was wrong? It's useful if anyone stumbles across this thread in the future to have some kind of solution posted :P

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

While doing port forwarding there were two fields, "port from" and "port to". I thought they meant a range of ports to be redirected, for example ports from 2000 to 3000. But no, it meant directing incoming traffic from port x to another port y, in my case I put them both to 2004, and that was it.

 

Now that I got this simple server/client connection working, I can move onto more interesting things, namely multiplayer games. (other than hotseat =D) Chat client still has some minor problems, for example connections aren't handled properly when user closes the window without disconnecting first, and support for multiple channels could be fun too. Not that I'd be into creating a whole new irc client, but stuff like that could make for a good programming exercise.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.