반응형
[android-beginners] Sockets with Android.
Android_n00b
Fri, 02 Oct 2009 08:59:43 -0700
Hi I'm implementing a program which uses sockets to communicate between the client and server. I am getting this to work fine with just a message. However, I want to have an EditText field in my application, which when I hit a 'Send' button, sends the text from the field to my server and logs it. I am trying to implement this, but my program always crashes. Any help would be appreciated. Here is my code: Android Client: public class SocketTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Thread sThread = new Thread(new TCPServer()); Thread cThread = new Thread(new TCPClient()); sThread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } cThread.start(); } } TCP Server: public class TCPServer implements Runnable{ public static final String SERVERIP = "127.0.0.1"; public static final int SERVERPORT = 8888; public void run() { try { Log.d("TCP", "S: Connecting..."); ServerSocket serverSocket = new ServerSocket (SERVERPORT); while (true) { Socket client = serverSocket.accept(); Log.d("TCP", "S: Receiving..."); try { BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = in.readLine(); Log.d("TCP", "S: Received: '" + str + "'"); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { client.close(); Log.d("TCP", "S: Done."); } } } catch (Exception e) { Log.e("TCP", "S: Error", e); } } } TCP Client: public class TCPClient extends Activity implements Runnable { final EditText msg = (EditText) findViewById(R.id.message); final Button sendButton = (Button) findViewById(R.id.sendserver); public void run() { sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { InetAddress serverAddr = InetAddress.getLocalHost(); Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, TCPServer.SERVERPORT); String message = msg.toString(); try { Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream ())),true); out.println(message); Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done."); } catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { socket.close(); } } catch (Exception e) { Log.e("TCP", "C: Error", e); } } }) ; } } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---