VB写的服务端,android写的客户端,在模拟器上能通信,把android客户端按到手机上就不连接不上了,小弟是菜鸟,正在学习android的scoket通信

wangruben 2012-05-07 10:23:44
客户端程序:
package wyf.wpf; //声明包语句

import java.io.DataInputStream; //引入相关类
import java.io.DataOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket; //引入相关类
import android.app.Activity; //引入相关类
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle; //引入相关类
import android.view.View;
import android.widget.Button;
import android.widget.EditText; //引入相关类
public class Sample_10_1_Client extends Activity {
private String msg;
private EditText et;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Socket socket;
DataOutputStream dout = null;
DataInputStream din = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //设置当前屏幕
et = (EditText)findViewById(R.id.et); //获得EditText对象
button1= (Button)findViewById(R.id.Button01);
button2= (Button)findViewById(R.id.Button02);
button3= (Button)findViewById(R.id.Button03);
button4= (Button)findViewById(R.id.Button04);
button5= (Button)findViewById(R.id.Button05);
button1.setOnClickListener(new buttonListener1());
button2.setOnClickListener(new buttonListener2());
button3.setOnClickListener(new buttonListener3());
button4.setOnClickListener(new buttonListener4());
button5.setOnClickListener(new buttonListener5());
connectToServer(); //连接服务端
}
class buttonListener1 implements OnClickListener, android.view.View.OnClickListener{
public void onClick(View v) {
String str="one";
try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(DialogInterface dialog, int which) {

}
}
class buttonListener2 implements OnClickListener, android.view.View.OnClickListener{
public void onClick(View v) {
String str="two";
try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(DialogInterface dialog, int which) {

}
}
class buttonListener3 implements OnClickListener, android.view.View.OnClickListener{
public void onClick(View v) {
String str="three";
try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(DialogInterface dialog, int which) {

}
}
class buttonListener4 implements OnClickListener, android.view.View.OnClickListener{
public void onClick(View v) {
String str="four";
try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(DialogInterface dialog, int which) {

}
}
class buttonListener5 implements OnClickListener, android.view.View.OnClickListener{
public void onClick(View v) {
String str="five";
try {
dout.write(str.getBytes("UTF-8"));
dout.flush();
et.setText("向电脑发送了"+"“"+str+"”");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onClick(DialogInterface dialog, int which) {

}
}
public void connectToServer(){ //方法:连接客户端
try{
socket = new Socket("127.0.0.1", 9958);//创建Socket对象 10.200.53.56 60.213.232.43
//msg = new PrintWriter(socket.getOutputStream());
din = new DataInputStream(socket.getInputStream()); //获得DataInputStream对象
dout = new DataOutputStream(socket.getOutputStream()); //获得DataInputStream对象
//din.read(msg.getBytes("UTF-8"));//getBytes("UTF-8")
//msg = din.readUTF(); //读取服务端发来的消息
//et.setText(msg); //设置EditText对象\
//button1.setText(msg);
}
catch(Exception e){ //捕获并打印异常
e.printStackTrace();
}
}
}
...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangruben 2012-05-09
  • 打赏
  • 举报
回复
还是不行,我用无线安路由试的可以,就是已换成网路IP就不行了
menghnhhuan 2012-05-08
  • 打赏
  • 举报
回复
socket = new Socket("127.0.0.1", 9958);//创建Socket对象 10.200.53.56 60.213.232.43
不是这个9958端口,把这个port转化为16进制,再交换高低位。
即:port = ((port& 0xFF00)>>8)|(port& 0x00FF)<<8);
AMinfo 2012-05-07
  • 打赏
  • 举报
回复
两步就可以了:

步骤:
1、打开百度搜索“IP查询”,然后会有一个本机IP: xx.xx.xx.xx

2、将以下代码中的127.0.0.1改成步骤1查到的本机IP:xx.xx.xx.xx
socket = new Socket("127.0.0.1", 9958);

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧