2

It seems that when I use the accept method from the Socket class the whole program freezes up until data comes through. I've passed the socket to a thread and it doesn't freeze but I need the thread to return data back which I don't think it can do.

Code For getting useername

public boolean checkUsername() {
    NetworkIO n = new NetworkIO();
    // Grabs username from edittext field
    username = usernameEditText.getText().toString();
    System.out.println(usernameEditText.getText().toString());
    // queries databse for username
    try {
        resultFromServer = n.query("username",
                "select username FROM user_info  WHERE MATCH (username)  AGAINST ('"
                        + username + "' IN NATURAL LANGUAGE MODE);");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Code for NetworkIO class

public class NetworkIO extends Activity {
    Socket networkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;


public String query(String request, String fieldRequested)
        throws IOException {

    // Tries to get connection
    try {

        networkSocket = new Socket("192.168.1.8", 9009);

        out = new PrintWriter(networkSocket.getOutputStream(), true);

        in = new BufferedReader(new InputStreamReader(
                networkSocket.getInputStream()));
        System.out.println("Hi from in made");
    } catch (UnknownHostException e) {
        System.err.println("Unknown host");
        System.exit(-1);

    } catch (IOException e) {
        System.err.println("IOException");
        System.exit(-1);
    }


    //Sends Request
    out.println(request);
    out.println(fieldRequested);
    String resultFromServer = "";

    //Waits for response and if its null exit
    while ((in.readLine()) != null) {
        resultFromServer += in.readLine();

    }
    System.out.println(resultFromServer);

    //Close the connections
    out.close();
    in.close();
    networkSocket.close();


    return resultFromServer;
}
}

Any criticism of my code will be much appreciated :)

4
  • 2
    probably helpful: stackoverflow.com/questions/1215418/…
    – Thilo
    Commented Jul 22, 2011 at 1:10
  • 3
    I've got answer before with no code but thanks for the tip. I'll add some right now.
    – Baba
    Commented Jul 22, 2011 at 1:11
  • Consider using non-blocking I/O (start with Socket.getChannel() and go from there). Commented Jul 22, 2011 at 1:11
  • somehow this remember me spring breaks... "show your code! show your code" Commented Jul 22, 2011 at 1:11

3 Answers 3

3

The usual pattern is to run the accept loop inside a thread, and have a separate thread pool of handlers that you dispatch requests to.

When you get a new socket from accept, hand the socket over to a worker thread to process. That way you don't ever need to pass anything back to your main thread.

Example is straight out of the ExecutorService javadocs: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

1
  • @Baba: see my revised answer.
    – Kevin
    Commented Jul 22, 2011 at 2:09
1

It's interesting that youre using sockets and threads. Is there a reason that you aren't using pipes instead? Performance of sockets vs pipes: Pipes have much better performance when you are just using threads.

Now if you really need sockets here is some example code that might help illustrate the general idea of the whole client server socket organization:

http://zerioh.tripod.com/ressources/sockets.html

1
  • Right he's talking about handing data between threads though? It's not clear what he's really trying to do.
    – JohnKlehm
    Commented Jul 22, 2011 at 1:16
0

It seems that when I use the accept method from the Socket class the whole program freezes up until data comes through.

  1. There is no accept() method in the Socket class.
  2. There is one in the ServerSocket class.
  3. It blocks until a connection comes through.
  4. It returns a Socket.
  5. Reading on the Socket blocks until data comes through.

Not the answer you're looking for? Browse other questions tagged or ask your own question.