Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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
















0
Idarhillgaar
Asked:
Idarhillgaar
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now