首先進去命令提示字元(按開始->輸入cmd)
輸入"telnet localhost 5554"(這邊要特別注意因為android emulator是5554所以才用telnet登入5554)
進入畫面如上,記得模擬器要開起來,否則Telnet會無法連線!
接著我們要重新定址(增加Port)
輸入指令redir add udp:5000:4000(這兩個數字可以任意變換, Local端:Guest端)
測試程式,隨意輸入文字,可以看到另一邊有收到(但是是ASCII碼)
如此就完成初步了,當然!!!!最重要的是程式碼,否則即使開啟了模擬器的port也無法傳送。
package com.example.sendudp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button Sender;
TextView err;
TextView check_send_data;
EditText input_str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Sender = (Button) findViewById(R.id.sender);
err = (TextView) findViewById(R.id.error_txv);
check_send_data = (TextView) findViewById(R.id.send_txv);
input_str = (EditText) findViewById(R.id.edt);
Sender.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatagramSocket socket = null;
try
{
socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("10.192.172.130");
String str = input_str.getText().toString();
//創建一個用於發送的DatagramPacket對象
DatagramPacket packet=new DatagramPacket(str.getBytes(),str.length(),serverAddress,4000);
//發送數據
//socket.connect(serverAddress, 6000);
socket.send(packet);
check_send_data.setText(str);//顯示傳送的文字
}
catch(SocketException e)
{
e.printStackTrace();
String error = e.toString();
err.setText(error);
}
catch(UnknownHostException e)
{
e.printStackTrace();
String error = e.toString();
err.setText(error);
}
catch(IOException e)
{
e.printStackTrace();
String error = e.toString();
err.setText(error);
}
catch(Exception e)
{
e.printStackTrace();
String error = e.toString();
err.setText(error);
}
finally{
if(socket != null){
socket.close();
}
}
}
}
然後一定會有人發現,即使如此還是沒法傳送,會出現Permission denied的exception
因此我們需要在bin/res/AndroidManifest.xml這邊修改,修改如下
記得要存檔,才會改變設定。
把權限開了,程式碼也沒有問題,Port也有設定好,大致上Android UDP Socket就算完成了。
不過因為我還沒寫接收的部分,所以接收的部分要在等寫完才能有教學
沒有留言:
張貼留言