- 1. 迷你飛信 for Android(無需登錄,免費(fèi)發(fā)短線) V3.2....
- 2. 朦朧星際天空電腦桌面主題
- 3. Firefox for Android(火狐瀏覽器手機(jī)版) V5.0最新官...
- 4. 蜜度 for Android(只需gprs流量費(fèi)的手機(jī)對講聊天工...
- 5. 安卓星空 for Android(海量手機(jī)游戲軟件免費(fèi)下載安...
- 6. 盛名列車時刻表電腦版20110621最新下載 簡體中文綠...
- 7. 好聽 for Android(包含上百個精彩音頻節(jié)目) V2.17 ...
- 8. 掌上優(yōu)惠 for Android(手機(jī)下載優(yōu)惠券) V1.0 簡體中...
- 9. 簡潔空間電腦桌面主題
- 10. 安智市場 for Android(手機(jī)軟件下載平臺) V2.0 簡體...
在Android下巧妙使用Socket連接網(wǎng)絡(luò)中的電腦
既然是使用SOCKET連接網(wǎng)絡(luò)上的電腦,就分為兩部分
其一,Android上面的Client
其二,PC上的Server
Android上面的實(shí)現(xiàn)代碼如下
view plaincopy to clipboardprint?
package Hello.MySocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MySocket extends Activity {
private final String DEBUG_TAG="lengfeng";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("lengfeng","Android Start Connect");
Socket socket=null;
String mesg="my name is log \r\n";
try {
socket=new Socket("10.0.2.2",54321);
//向服務(wù)器發(fā)送信息
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
//接受服務(wù)器的信息
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String mstr=br.readLine();
if(mstr!=null)
{
Log.e(DEBUG_TAG,mesg);
}else
{
Log.e(DEBUG_TAG,"數(shù)據(jù)錯誤");
}
out.close();
br.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e)
{
Log.e(DEBUG_TAG,e.toString());
}
}
}
package Hello.MySocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MySocket extends Activity {
private final String DEBUG_TAG="lengfeng";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("lengfeng","Android Start Connect");
Socket socket=null;
String mesg="my name is log \r\n";
try {
socket=new Socket("10.0.2.2",54321);
//向服務(wù)器發(fā)送信息
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
//接受服務(wù)器的信息
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String mstr=br.readLine();
if(mstr!=null)
{
Log.e(DEBUG_TAG,mesg);
}else
{
Log.e(DEBUG_TAG,"數(shù)據(jù)錯誤");
}
out.close();
br.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e)
{
Log.e(DEBUG_TAG,e.toString());
}
}
}
PC上面的Server 創(chuàng)建一個java的項(xiàng)目
view plaincopy to clipboardprint?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class MyJavaServer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("MyJavaServer Start Runing\n");
try {
ServerSocket serverSocket=new ServerSocket(54321);
while(true)
{
System.out.println("接收用戶連接:");
//接受客戶端請求
Socket client=serverSocket.accept();
System.out.println("accept:");
try
{
//接受客戶端信息
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));