(1)BYTE TO STRING
(2)STRING TO BYTE
(3)THREAD
(4)HANDLE
- packagecom.FJICC.lzm;
- importjava.util.ArrayList;
- importjava.util.Enumeration;
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importjava.io.OutputStream;
- importjava.net.InetAddress;
- importjava.net.NetworkInterface;
- importjava.net.Socket;
- importjava.net.ServerSocket;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.app.Dialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- import android.os.Handler;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- publicclasstcp_serverextendsActivity{
- private ButtonserverStart_btn;
- private ButtonserverStop_btn;
- private TextViewreceivedata_tv;
- private Buttonsetport_btn;
- private EditTextsenddata_et;
- private Buttonsend_btn;
- public int PORT = 8080 ;
- publicHandlermHandler;
- protectedstaticfinalintGUINOTIFIER=0x1234;
- @Override
- publicvoidonBackPressed(){
- //TODOAuto-generatedmethodstub
- super.onBackPressed();
- Intents = new Intent ();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity (i);
- finish();
- }
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super .onCreate (savedInstanceState);
- setContentView(R.layout.tcpserver_main);
- serverStart_btn=(Button)findViewById(R.id.btnStart);
- serverStop_btn=(Button)findViewById(R.id.btnStop);
- setport_btn=(Button)findViewById(R.id.btnSet);
- send_btn=(Button)findViewById(R.id.btnSend);
- senddata_et=(EditText)findViewById(R.id.et_send);
- receivedata_tv=(TextView)findViewById(R.id.tv_receive);
- serverStart_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- public void onClick(Viewv){
- //TODOAuto-generatedmethodstub
- serverStart_btn.setEnabled(false);
- setport_btn.setEnabled(false);
- serverStop_btn.setEnabled(true);
- newThread()
- {
- @Override
- public void run () {
- //TODOAuto-generatedmethodstub
- super.run();
- ServerSocketserverSocket=null;
- try{
- //Create a ServerSocket object to listen to the PORT port
- serverSocket=newServerSocket(PORT);
- //Receive tcp connection and return socket object
- Socketsocket=serverSocket.accept();
- //get the input stream
- InputStreaminputStream=socket.getInputStream();
- ///////////////////////////////////////////////////////////////////////////////////////
- //get the output stream
- OutputStreamoutputStream=socket.getOutputStream();
- byte[]byteBuffer=newbyte[1024];
- int temp = 0 ;
- Strings;
- //read received data
- while((temp=inputStream.read(byteBuffer))!=-1)
- {
- outputStream.write(byteBuffer,0,temp);
- // Convert byte to string
- //String(byte[],int,int) uses the platform's default character encoding to convert the specified byte subarray to generate a new String
- s=newString(byteBuffer,0,temp);
- // Convert string to byte
- //byte[]bs=str.getBytes();
- //Define a message variable m
- Messagem= new Message();
- //The tag GUINOTIFIER of the message is defined earlier
- m.what=tcp_server.GUINOTIFIER;
- //Pass the data to be transferred to m.obj
- m.obj=s;
- // send message
- tcp_server.this.mHandler.sendMessage(m);
- }
- //System.out.println(newString(byteBuffer,0,temp));
- outputStream.flush();
- socket.close();
- serverSocket.close();
- }catch(IOExceptione){
- e.printStackTrace ();
- }
- }
- }.start();
- }
- });
- //create handler
- mHandler=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- switch (msg.what){ //You can do the corresponding operation when you get the notification from Handle
- case tcp_server.GUINOTIFIER: //tcp_server is the class name of Activity
- //Clear textView
- receivedata_tv.setText("");
- //Set the textView to display the content
- receivedata_tv.setText(msg.obj.toString());
- break;
- }
- super.handleMessage(msg);
- }
- };
- //end TCP server
- serverStop_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- public void onClick(Viewv){
- serverStart_btn.setEnabled(true);
- setport_btn.setEnabled(true);
- serverStop_btn.setEnabled(false);
- Intents = new Intent ();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity (i);
- finish();
- }});
- }
- }
- tcpserver_main.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnStart"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text= "Open service" />
- <Button
- android:id="@+id/btnStop"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text= "Close the service" />
- <Button
- android:id="@+id/btnSet"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text= "Port Settings" />
- </LinearLayout>
- <TextView
- android:id="@+id/tv_receive"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.70"
- android:text="TextView"/>
- <EditText
- android:id="@+id/et_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textMultiLine">
- <requestFocus/>
- </EditText>
- <Button
- android:id="@+id/btnSend"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="148dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text= "Send" />
- </LinearLayout>
- <uses-permissionandroid:name="android.permission.INTERNET"/>