import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SimpleClientActivity extends Activity { private Socket client; private PrintWriter printwriter; private EditText textField; private Button button; private String messsage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textField = (EditText) findViewById(R.id.editText1); //reference to the text field button = (Button) findViewById(R.id.button1); //reference to the send button //Button press event listener button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { messsage = textField.getText().toString(); //get the text message on the text field textField.setText(""); //Reset the text field to blank try { client = new Socket("192.168.43.112", 4444); //connect to server printwriter = new PrintWriter(client.getOutputStream(),true); printwriter.write(messsage); //write the message to output stream printwriter.flush(); printwriter.close(); client.close(); //closing the connection } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SimpleClientActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="Send" /> </RelativeLayout>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Main { private static ServerSocket serverSocket; private static Socket clientSocket; private static InputStreamReader inputStreamReader; private static BufferedReader bufferedReader; private static String message; public static void main(String[] args) { try { serverSocket = new ServerSocket(4444); //Server socket } catch (IOException e) { System.out.println("Could not listen on port: 4444"); } System.out.println("Server started. Listening to the port 4444"); while (true) { try { clientSocket = serverSocket.accept(); //accept the client connection inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); bufferedReader = new BufferedReader(inputStreamReader); //get the client message message = bufferedReader.readLine(); System.out.println(message); inputStreamReader.close(); clientSocket.close(); } catch (IOException ex) { System.out.println("Problem in message reading"); } } } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง