Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> Android學習之Socket 使用

Android學習之Socket 使用

編輯:Android編程入門

1socket的作用

通過http去獲取服務器的數據在有些情況下是行不通的,所有使用socket與服務器通信也是必須掌握的

2.代碼

好了上代碼,代碼中有解釋,首先是簡單的服務端代碼

package org.tonny;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SockServer implements Runnable
{
private static final int PORT = 55555;

@Override
public void run()
{
//用於響應客戶端
Socket client = null;

//服務端socket
ServerSocket server = null;
try
{
//綁定端口,准備接受客戶端的連接
server = new ServerSocket(PORT);
while (true)
{
System.out.println("Wait for connecting...");

//這個操作是阻塞的,有客戶端連接才會進行下一步操作
client = server.accept();

//讀取客戶端發送的數據
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String msg = in.readLine();
System.out.println("Client: " + msg);
}
}
catch (Exception e)
{
System.out.println("Server Receive: Error");
}
finally
{
try
{
//關閉套接字
server.close();
client.close();
}
catch (IOException e)
{
System.out.println("Server Receive: Error" + e.getMessage());
}
}
}

public static void main(String[] args)
{
Thread thread = new Thread(new SockServer());
thread.start();
}
}


接著是android客戶端的代碼

package org.tonny.client;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class ClientActivity extends Activity
{
// 需要發送的信息寫在這個控件中
private EditText txtInfo = null;
private static final int PORT = 55555;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
txtInfo = (EditText) this.findViewById(R.id.txtInfo);
}

public void onBtnSendClick(View view)
{
String msg = txtInfo.getText().toString();

//不適用綫程通不過去,直接異常處理,這是因為android 3.0+以上 已經不建議在activity中添加耗時操作,要界面和數據脫離。4.0以上的通信都必須放到線程裡去做 不能在UI線程。
//解決辦法,另起線程或Service處理socket。
SockClient sock = new SockClient("192.168.1.106", PORT, msg);
sock.start();
}

private final class SockClient extends Thread
{
private String ip;
private int port;
private String msg;

public SockClient(String ip, int port, String msg)
{
this.ip = ip;
this.port = port;
this.msg = msg;
}

@Override
public void run()
{
Socket client = null;
try
{
Log.d("CLIENT", "Connecting...");
// 與服務器獲取連接
client = new Socket(ip, port);
// 獲取 Client 端的輸出流
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
out.println(msg);
}
catch (UnknownHostException e)
{
Log.e("CLIENT", "Error", e);
}
catch (IOException e)
{
Log.e("CLIENT", "Error", e);
}
catch (Exception e)
{
Log.e("CLIENT", "Error", e);
}
finally
{
try
{
if (client != null && !client.isClosed())
{
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved