Is it possible to have a client & server working under a main, but seperate threads

I have  a standard Client and server that I want to get working under the same process, problably using threads.
-Is this possible or is it redundant?
--
To reformulate my question: I need to start both client & server to await files and send files(not yet implemented) to/from another peer client/Server.


Code Server:
      public void Server() throws IOException
      {
            ServerSocket ss = new ServerSocket(5556);
            Socket s;
            System.out.println(ss);
            try
            {            
                  if((s=ss.accept())!=null)
                  {
                        System.out.println("Connection is ok");
                        s.close();
                  }
                  ss.close();      
            }

Coder Client:
public void Client(String url) throws IOException
      {
            Socket s = new Socket(url,5556);
            System.out.println(url);
            try
            {
                  System.out.println("Socket = " + s);
                  s.close();
            }
                                catch ..... etc

--
Many thanks for any help.

Idar
















IdarhillgaarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThummalaRaghuveerCommented:
Yes it is possible to have server and client in the same process, but each handled by different thread.
0
Mayank SAssociate Director - Product EngineeringCommented:
But why would you want to do this ;-) ?
0
IdarhillgaarAuthor Commented:
Well I am makin  a system that takes in clients (file & string) transfers and can send at the same time, so I need one application that can do this more or less simultanously.

So I need a client to send files, a server to receive files from other that local client on each peer - I would need to initiate client/server from one application.

Was this clear? If any1 have a better design don't be shy to let me know.

many thanks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Mayank SAssociate Director - Product EngineeringCommented:
Let me get this a little more clearly. The clients only send file-names or do they send entire files? And once the server has received them, what does it do with them? Also where does the client pick up them from? And many such clients keep sending files simultaneously?
0
aozarovCommented:
When a server gets the request from the client -> serverSocket.accept(); you get a Socket which you can then further talk with that client.
if you can then pass that socket to a seperate Thread that will handle this client (receive more data/ send response).
A simplistic approach for the server would be to to pass such Socket to a new Thread that will handle this request (using that way you can handle many requests) but this will not scale to many clients.
Another approach is to pass that Socket to a ThreadPool and handle the request.
If you have java 1.5/5.0 then you can use asynchonous IO which will help you to scale much better.
see -> http://www.javaalmanac.com/egs/java.nio/NbServerSocket.html (for the server)
and http://www.javaalmanac.com/egs/java.nio/NbClientSocket.html (for the client)
0
ThummalaRaghuveerCommented:
If its in the same application then its better to have client and server running in same process but different threads...
0
IdarhillgaarAuthor Commented:
1: Remote client tells locale server that it wants a file, the local server tells locale client to send the file to remote server.
2: Locale Client sends files, the file name and status(string)
3: remote Server stores the files according to preference.

<remote>                           <local>
Client-------------------------->Server
        (request server for file)   |
                                            |
                                            |                                          
                                            | (Give file name and address to upload to an IP addr.)
                                            v
 Server<---------------------- client
          (Upload file x to server)

Hope this clears things up, if this is a bad way of doing it, let me know.


--
Idar
0
aozarovCommented:
Such interaction between the server and the client can be done (in the server side) using the Socket returned after establishing the connection (serverSocket.accept()).
I don't see the need to to have 2 sockets per client on the server.
Look here for more info/example : http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
0
ThummalaRaghuveerCommented:
As said by aozarov there is  no necisity to have multiple sockets.

Here U are changing the terminology of server. Here I guess the locale server is nothing but a thread which is listening to incoming stream from socket and locale client is nothing but a thread handling the outgoing stream from the socket. Both the threads use the same socket.

If this system all together is called a client. then U need a client program that connects to a remote server and then extracts the output stream and input stream and gives it to 2 threads one handling the role of locale client and the other handling role of locale server.  Then figure out a logic according to which these two thread cordiante.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.