Solved

simple server / client help pls

Posted on 2004-08-15
6
184 Views
Last Modified: 2010-03-31
Hi. i am making a program but im having trouble using server.accept() since it stops the thread that i am using. is there a way to keep running the thread while it waits for a connection so that my program can connect to itself.
thankx
0
Comment
Question by:teamasia
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11804727
Yes, you should use a separate thread to do that in, or use non-blocking IO
0
 
LVL 92

Expert Comment

by:objects
ID: 11806121
post your server code
(why do u need your server to connect to itself)
0
 
LVL 5

Accepted Solution

by:
vilia earned 50 total points
ID: 11806400
In the example provided below (common solution),  server waits for a client
   Socket client = ss.accept();
As a client makes connection, the new thread is created (serveClientConnection) and started. The server waits for the second client, and so on.

serveClientConnection is responsible for server-client communication.

The thread with
   Socket client = ss.accept();
should do nothing but wait for a new client connection and spawn a new thread.


private static final int SERVER_PORT = 1234;
public static void main(String[] argv) {
      try {
            ServerSocket ss = new ServerSocket(SERVER_PORT);
            while (true) {  // server loop
                  // wait for a client
                  Socket client = ss.accept();
                  // start server- client communication in
                  // the new thread
                  Thread serveClientConnection = new Thread() {
                        public void run() {
                              // communicate with client
                        }
                  };
                  serveClientConnection.start();
            }
      } catch (Exception e) {
            e.getStackTrace();
      }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11807356
>> server.accept() since it stops the thread that i am using

That's what its supposed to do. It waits for clients to connect.

>> is there a way to keep running the thread while it waits for a connection

What *other* processing do you do in that thread, that you want it to keep running?

>> so that my program can connect to itself

Program connect to itself ;-) ??
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11899800
teamasia,

Can you elaborate a little more on what your question was and how that comment helped? I still don't seem to understand:

>> my program can connect to itself

- and how the accepted answer does that ;-) ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Saving Tweets with emojis using Twitter4J in a Mysql table 2 73
Java JRE greater than 1.6 5 50
ForLoop Example 3 40
reverse digits of a number using for loop 5 36
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now