- 1. 迷你飞信 for Android(无需登录,免费发短线) V3.2....
- 2. 朦胧星际天空电脑桌面主题
- 3. Firefox for Android(火狐浏览器手机版) V5.0最新官...
- 4. 蜜度 for Android(只需gprs流量费的手机对讲聊天工...
- 5. 安卓星空 for Android(海量手机游戏软件免费下载安...
- 6. 盛名列车时刻表电脑版20110621最新下载 简体中文绿...
- 7. 好听 for Android(包含上百个精彩音频节目) V2.17 ...
- 8. 掌上优惠 for Android(手机下载优惠券) V1.0 简体中...
- 9. 简洁空间电脑桌面主题
- 10. 安智市场 for Android(手机软件下载平台) V2.0 简体...
在Android下巧妙使用Socket连接网络中的电脑
既然是使用SOCKET连接网络上的电脑,就分为两部分
其一,Android上面的Client
其二,PC上的Server
Android上面的实现代码如下
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);
//向服务器发送信息
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
//接受服务器的信息
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,"数据错误");
}
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);
//向服务器发送信息
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
//接受服务器的信息
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,"数据错误");
}
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 创建一个java的项目
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()));