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 :)

Baba
  • 35
  • 4

3 Answers3

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

Kevin
  • 24,871
  • 19
  • 102
  • 158
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

Community
  • 1
  • 1
JohnKlehm
  • 2,368
  • 15
  • 9
  • Right he's talking about handing data between threads though? It's not clear what he's really trying to do. – JohnKlehm Jul 22 '11 at 01: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.
user207421
  • 305,947
  • 44
  • 307
  • 483