1. Bernd30bln, 19.07.2010 #1
    Bernd30bln

    Bernd30bln Threadstarter Neuer Benutzer

    Hallo,

    Kennt jemand ein einfaches Chat Tutorial.

    Ich will einfach nur vom Handy eine message an meinen PC schicken über WLan.

    Gruß Bernd
     
  2. Operator, 19.07.2010 #2
    Operator

    Operator Android-Lexikon

    Hi,

    bitte in Zukunft nur ein Fragezeichen verwende, danke!
     
  3. K2DaC, 19.07.2010 #3
    K2DaC

    K2DaC Junior Mitglied

    muss das ganze MultiUser fähig sein ?
    und soll das Handy oder der PC als Server dienen ?
    Reicht die nur quellcode ?
    Fragen über Fragen =)

    Um mich vom Lernen abzulenken, hier mal ein 20min entwurf einer Kommunikatin zwischen Handy und JavaServer.
    Dirty copy und paste ausm internet + eigene Modifikation des Clients für android

    Zuerst der Server, in Java. Läuft in Endlosschleife, bis ".bye" als Befehl bzw als Nachricht gesendet wird :

    Code:
    import java.net.*;
    import java.io.*;
    
    public class ChatServer implements Runnable
    {  private Socket       socket = null;
       private ServerSocket server = null;
       private Thread       thread = null;
       private DataInputStream  streamIn  =  null;
    
       public ChatServer()
       {  
          int port = 1337; 
          try
          {  System.out.println("Binding to port " + port + ", please wait  ...");
             server = new ServerSocket(port);  
             System.out.println("Server started: " + server);
             start();
          }
          catch(IOException ioe)
          {  
              System.out.println(ioe); 
          }
       }
       public void run()
       {  while (thread != null)
          {   try
             {  System.out.println("Waiting for a client ..."); 
                socket = server.accept();
                System.out.println("Client accepted: " + socket);
                open();
                boolean done = false;
                while (!done)
                {  try
                   {  String line = streamIn.readUTF();
                      System.out.println(line);
                      done = line.equals(".bye");
                   }
                   catch(IOException ioe)
                   {  done = true;  }
                }
                close();
             }
             catch(IOException ie)
             {  System.out.println("Acceptance Error: " + ie);  }
          }
       }
       public void start()
       {  if (thread == null)
          {  thread = new Thread(this); 
             thread.start();
          }
       }
       @SuppressWarnings("deprecation")
       public void stop()
       {  if (thread != null)
          {  thread.stop(); 
             thread = null;
          }
       }
       public void open() throws IOException
       {  streamIn = new DataInputStream(new 
                            BufferedInputStream(socket.getInputStream()));
       }
       public void close() throws IOException
       {  if (socket != null)    socket.close();
          if (streamIn != null)  streamIn.close();
       }
       public static void main(String args[])
       {  
           ChatServer server =  new ChatServer();
       }
    }
    
    
    
    und hier der Client

    Code:
    package de.MyChatCientApp;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    
    import java.net.*;
    import java.io.*;
    
    
    public class MyChatCientApp extends Activity {
        /** Called when the activity is first created. */
        String ip = "0";
        ChatClient client = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
             
             
             
        }
        
        public void onSendClick(View v)
        {
            EditText nachricht = (EditText) this.findViewById(R.id.EditText01);
            String msg = nachricht.getText().toString();
            client.send(msg);
            
        }
        
        public void onConnectClick(View v)
        {
            EditText AdressEditText = (EditText) this.findViewById(R.id.EditText02);
            ip = AdressEditText.getText().toString();
            int port = 1337;
            client =  new ChatClient(ip, port);
        }
    }
    
    
    
    
    class ChatClient
    {  
       private Socket socket              = null;
       private DataInputStream  console   = null;
       private DataOutputStream streamOut = null;
    
       public ChatClient(String serverName, int serverPort)
       {  System.out.println("Establishing connection. Please wait ...");
          try
          {  socket = new Socket(serverName, serverPort);
             Log.d("DEBUG","Connected: " + socket);
             start();
          }
          catch(UnknownHostException uhe)
          {  Log.d("DEBUG","Host unknown: " + uhe.getMessage());
          }
          catch(IOException ioe)
          {  Log.d("DEBUG","Unexpected exception: " + ioe.getMessage());
          }
          
         
       }
       
       public void start() throws IOException
       {  
          console   = new DataInputStream(System.in);
          streamOut = new DataOutputStream(socket.getOutputStream());
       }
       
       public void stop()
       {  try
          {  if (console   != null)  console.close();
             if (streamOut != null)  streamOut.close();
             if (socket    != null)  socket.close();
          }
          catch(IOException ioe)
          {  System.out.println("Error closing ...");
          }
       }
       
       public void send(String msg)
       {
           try
           {  
              streamOut.writeUTF(msg);
              streamOut.flush();
           }
           catch(IOException ioe)
           {  
               Log.d("DEBUG","Sending error: " + ioe.getMessage());
           }
       }
      
    }
    
    Die main.XML

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="300px"></EditText>
    <TextView android:layout_height="wrap_content" android:id="@+id/TextView01" android:layout_toRightOf="@+id/EditText01" android:layout_width="wrap_content" android:text="Nachricht"></TextView><Button android:layout_height="wrap_content" android:id="@+id/Button01" android:layout_below="@+id/EditText01" android:layout_width="wrap_content" android:text="Send" android:onClick="onSendClick"></Button>
    <EditText android:layout_height="wrap_content" android:id="@+id/EditText02" android:layout_below="@+id/Button01" android:layout_width="wrap_content" android:text="127.0.0.1" android:width="300px"></EditText>
    
    <Button android:layout_height="wrap_content" android:id="@+id/Button02" android:layout_below="@+id/EditText02" android:layout_width="wrap_content" android:text="connect" android:onClick="onConnectClick"></Button><TextView android:layout_height="wrap_content" android:id="@+id/TextView02" android:layout_below="@+id/Button01" android:layout_toRightOf="@+id/EditText02" android:layout_width="wrap_content" android:text="Server IP"></TextView>
    
    </RelativeLayout>
    
    
    und im Manifest
    <uses-permission android:name="android.permission.INTERNET" />
    nicht vergessen !

    Anbei noch ein BeweisScreen :)
     

    Anhänge:

  4. Bernd30bln, 22.07.2010 #4
    Bernd30bln

    Bernd30bln Threadstarter Neuer Benutzer

    Hallo K2DaC,

    Danke für die Hilfe stellung.

    Gruß Bernd
     

Besucher kamen mit folgenden Begriffen auf unsere Seite:

  1. new operator in android