Android3.2模拟器客户端怎么和pc服务端通过socket连接?
Android3.2模拟器客户端怎么和pc服务端通过socket连接??急:怎么整模拟器和pc都连不起来,不知道问题在那里?求解...
Android3.2模拟器客户端怎么和pc服务端通过socket连接??急:怎么整模拟器和pc都连不起来,不知道问题在那里?求解
展开
2个回答
展开全部
我最近也写了一个同样的程序,我可以给你发过去,你参考一下吧:有疑问加我QQ:457993185
我的程序对你有帮助的话,就把10分给我哦!
pc机服务器端:
package com.android.Server;
import java.io.*;
import java.net.*;
import java.util.Hashtable;
public class AndroidServer extends Thread
{
Socket socket;
AndroidServer(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
Hashtable<String,String> hash = (Hashtable<String,String>)Ois.readObject();
System.out.println(hash);
PrintStream pstream = new PrintStream(socket.getOutputStream());
for(int i = 0;i<5;i++)
{
pstream.println(i +" test_AndroidSocket!");
}
pstream.close();
socket.close();
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception
{
ServerSocket server = new ServerSocket(4567);
System.out.println("Listening...");
while(true)
{
Socket sock = server.accept();
System.out.println("Connected");
new AndroidServer(sock).start();
}
}
}
android模拟器客户端:
package com.heiye.client;
import java.io.DataInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Hashtable;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HeiyeClientActivity extends Activity {
/** Called when the activity is first created. */
TextView txt_tcp=null;
Button btn=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_tcp=(TextView)findViewById(R.id.txt_tcp);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try{
Socket socket = new Socket("192.168.60.46",4567);
Hashtable<String, String> hash = new Hashtable<String,String>();
hash.put("姓", "西门");
hash.put("名字", "可可");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(hash);
DataInputStream dis = new DataInputStream(socket.getInputStream());
String s = dis.readLine();
System.out.println(s);
socket.close();
}catch(Exception ex)
{
System.out.println(ex);
}
}
}
);
}
}
程序如上,但还要配置main.xml Strings.xml AndroidManifest.xml有什么不懂得来和我交流
我的程序对你有帮助的话,就把10分给我哦!
pc机服务器端:
package com.android.Server;
import java.io.*;
import java.net.*;
import java.util.Hashtable;
public class AndroidServer extends Thread
{
Socket socket;
AndroidServer(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
ObjectInputStream Ois = new ObjectInputStream(socket.getInputStream());
Hashtable<String,String> hash = (Hashtable<String,String>)Ois.readObject();
System.out.println(hash);
PrintStream pstream = new PrintStream(socket.getOutputStream());
for(int i = 0;i<5;i++)
{
pstream.println(i +" test_AndroidSocket!");
}
pstream.close();
socket.close();
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception
{
ServerSocket server = new ServerSocket(4567);
System.out.println("Listening...");
while(true)
{
Socket sock = server.accept();
System.out.println("Connected");
new AndroidServer(sock).start();
}
}
}
android模拟器客户端:
package com.heiye.client;
import java.io.DataInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Hashtable;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HeiyeClientActivity extends Activity {
/** Called when the activity is first created. */
TextView txt_tcp=null;
Button btn=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_tcp=(TextView)findViewById(R.id.txt_tcp);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try{
Socket socket = new Socket("192.168.60.46",4567);
Hashtable<String, String> hash = new Hashtable<String,String>();
hash.put("姓", "西门");
hash.put("名字", "可可");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(hash);
DataInputStream dis = new DataInputStream(socket.getInputStream());
String s = dis.readLine();
System.out.println(s);
socket.close();
}catch(Exception ex)
{
System.out.println(ex);
}
}
}
);
}
}
程序如上,但还要配置main.xml Strings.xml AndroidManifest.xml有什么不懂得来和我交流
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询