?
Solved

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

Posted on 2005-03-30
9
Medium Priority
?
203 Views
Last Modified: 2010-03-31
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
Comment
Question by:Idarhillgaar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 13663276
Yes it is possible to have server and client in the same process, but each handled by different thread.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13663504
But why would you want to do this ;-) ?
0
 

Author Comment

by:Idarhillgaar
ID: 13663776
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 30

Expert Comment

by:Mayank S
ID: 13663831
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
 
LVL 15

Expert Comment

by:aozarov
ID: 13663961
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
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 13663990
If its in the same application then its better to have client and server running in same process but different threads...
0
 

Author Comment

by:Idarhillgaar
ID: 13664041
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
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 750 total points
ID: 13664458
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
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 750 total points
ID: 13664927
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question