Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i found this example http://www.ase.md/~aursu/ClientServerThreads.html .. I'm using the chat example. And i tested it on pc. It actualy works, but now i want to atach it for android... I dont know how to change the codes because it i'm new on android development. This is my client:

        import java.io.*;
import java.net.*;

public class UDPClient implements Runnable{

    // Declaration section
    // clientClient: the client socket
    // os: the output stream
    // is: the input stream

    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;

    public static void main(String[] args) {

    // The default port 

    int port_number=2222;
        String host="192.168.43.200";

    if (args.length < 2)
        {
        System.out.println("Usage: java MultiThreadChatClient  \n"+
                   "Now using host="+host+", port_number="+port_number);
        } else {
        host=args[0];
        port_number=Integer.valueOf(args[1]).intValue();
        }
    // Initialization section:
    // Try to open a socket on a given host and port
    // Try to open input and output streams
    try {
            clientSocket = new Socket(host, port_number);
            inputLine = new BufferedReader(new InputStreamReader(System.in));
            os = new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host "+host);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+host);
            System.err.println(e);
        }

    // If everything has been initialized then we want to write some data
    // to the socket we have opened a connection to on port port_number 

        if (clientSocket != null && os != null && is != null) {
            try {

        // Create a thread to read from the server

                new Thread(new UDPClient()).start();

        while (!closed) {
                    os.println(inputLine.readLine()); 
                }

        // Clean up:
        // close the output stream
        // close the input stream
        // close the socket

        os.close();
        is.close();
        clientSocket.close();   
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           

    public void run() {     
    String responseLine;

    // Keep on reading from the socket till we receive the "Bye" from the server,
    // once we received that then we want to break.
    try{ 
        while ((responseLine = is.readLine()) != null) {
        System.out.println(responseLine);
        if (responseLine.indexOf("*** Bye") != -1) break;
        }
            closed=true;
    } catch (IOException e) {
        System.err.println("IOException:  " + e);
    }
    }
}
share|improve this question
This is off topic for gamedev. You'd get a better answer on stack overflow. I'd migrate it, but it seems you're suspended over there. – Tetrad Feb 11 '12 at 17:32

closed as off topic by Tetrad Feb 11 '12 at 17:32

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.