Jump to content

Connecting two apps, and listening to them

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
I want to create two applications, one server-side, one client-side, which will be connected to each other, and listen to each other, sending objects to the other side, when requested.

My questions are, how do I connect to the other application, if I have an IP? I've been reading a bit about Sockets, which seems to be the way to do it, but I'm not sure. And how would I listen for some data sent from the other side, or even listen for specific data?

I want it to start out by the ClientApp connecting to the ServerApp, and then sending its IP address to the ServerApp so the ServerApp knows where to send stuff to. But I'm thinking that that might not be necessary if the ClientApp makes the connection.

In case I need to send the IP, I want the ServerApp to listen for that IP address coming in through the connection, and then send an object to the ClientApp.

So I also need the ClientApp to listen for that object coming in. The ClientApp will then receive the object, and perform a method on it that I have defined. When it is done with this method, the object should contain some data, so therefore I now want to send that object back to the ServerApp.

Therefore I want the ServerApp to also listen for that object coming in. And when that is done, I want the ServerApp to just run and wait until it gets another request from the ClientApp.

Also, I want the ServerApp to be able to send out objects to several ClientApps, therefore I'm programming the ServerApp using Threads.

Listening to the apps for specific data is my biggest problem right now, and the only thing I really don't know how to do.

Edit:
Also, how do I check whether or not a thread is still running?

Hmm, noone who knows how to make a connection between two Java applications? :(

Edited by WingedPanther, 02 January 2009 - 03:27 PM.
Double post


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What you are talking about is socket programming. While I'm not familiar with how it works in Java, I would definitely research it. You are talking about fairly basic things, here.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Java Networking
O'reilly Java Networking

:)

The first one is a tutorial by Sun, and the second one is a really good book about network programming in Java. :)

#4
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Thanks to the both of you. :) The Java tutorial is really helpful, it never occured to me that there'd be a networking tutorial.

I have but one question. The Java tutorial mentions using threads with sockets if you want the server to accept multiple clients. But how would you do that exactly? As far as I know, Socket is not Runnable.

Edit:
Actually, I missed that that part of the tutorial has two Java files, one of which is a KKMultiServerThread.

However, suppose I want to send an Object from my Server to my Client, and then want to run a method on that Object from my client. How do I change the code in the tutorial? As it is now, it reads and writes line by line to the Server and Client.

#5
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Well the example is using InputStreamReaders and OutputStreamWriters. To send your object you serialize it and send it over a standard OutputStream.

Alternatively you can use RMI (Remote Method Invocation) but seeing as your only starting out I'd advise to start with sockets.

HTH

#6
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Actually, I found out that there is an ObjectOutputStream and ObjectInputStream in Java, which I'm now making use of, and which have solved most of my problems.

A few, however, persist to cause me a headache. If my Client requests an Object from my Server by using readObject, the Server obviously needs to write the Object back using writeObject. But how can I check from my Server that the Client has invoked readObject?

Also, I'm using the MultiServer example to try and make my Server capable of dealing multiple Clients. I do this by having a Connection class (the KKMultiServerThread from the example) for each client that starts a connection, and adding the Connection to an ArrayList.

However, I need to be able to respond to any Connection (Thread) ending. How can I do that? The only way I can think of, would be to run through the entire ArrayList and check for each Connection if it has ended, but that's not plausible. I need some kind of trigger in my Server, that triggers whenever any Connection terminates, and identifies that Connection as well, so I can remove it from my ArrayList.

Also, there is a flush method for an ObjectOutputStream, where it says that it forces the stream to write any bytes to be written. Is this necessary to use to ensure that my Object has actually been sent to the other side of the connection?

#7
Stu_328

Stu_328

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Why dont you have a request object that holds all the types of requests that a client can invoke? Make the client throw that to the server. When the server picks it up it'll know what to send back.

For handling connections:

1)
Don't have a connection for each client, have a connection for each transaction. ie every time a server completes a transaction close the connection. The client then starts a new connection for a new request.

2)
Alternatively, when the client closes the connection a socket exception will be thrown. When you catch it, close that connnection.

-------------

You should always flush a stream after sending something down it. Like going to the loo.

HTH

#8
ThemePark

ThemePark

    Programmer

  • Members
  • PipPipPipPip
  • 124 posts
Actually, I noticed that the Java tutorial example uses states, so I think I will go the same way.

Having a connection for each transaction isn't an option, since I will be having a lot of clients and therefore a lot of transactions. But I think I'm slowly figuring a solution out. If not, I'll post here again.

Right now I need a test server, so therefore I'm attempting to turn the Java tutorial example into using ObjectOutputStream and ObjectInputStream instead, which should work since Strings are Objects. However, it doesn't. Somewhere along the line it just hangs, presumably at the part where it creates the streams. I can't see why though.

Edit:
Actually, I tried adding an out.flush to my server as well, and now it works, even when I later remove that line. But that leads me to a question. I know that the output stream should be flushed whenever you write something to it, but should it also be flushed as soon as it's created?

Edited by ThemePark, 08 January 2009 - 03:45 AM.