程序在模拟器上能接收到消息,但是在真机上接收不到
例子1
客户端发送信息,服务器端输出信息到输出台上
核心代码:
客户端:
this.button.setOnClickListener(new OnClickListener() {
Socket socket = null;
OutputStream os = null;
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1.创建一个socket对象,通过构造器致命服务端的IP地址,以及接收程序的端口号
socket = new Socket(InetAddress.getByName("192.168.1.102"), 9090);
//2.getOutputStream()发送数据,方法返回OutputStream子类的对象
os = socket.getOutputStream();
//3.具体的输出过程
os.write("sdadf".getBytes());
//4.关闭流和socket对象
} catch (Exception e) {
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//finally
}
}).start();
}
});
服务器端:
/**
* 完成数据的接收
* @throws IOException
*/
public static void socketSer() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
try {
System.out.println("服务器准备好了。。。");
//1.创建一个serverSocket的对象,通过构造器指明自身的端口号
serverSocket = new ServerSocket(9090);
//2.调用accept返回socket对象
socket = serverSocket.accept();
//3.调用socket的getInputStream()获得一个从客户端发过来的输入流
is = socket.getInputStream();
//4.对获取输入流进行操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len);
System.out.println("接收到的数据" + s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流以及serverSocket和socket对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//finally
}//socketSer
例子2:
客户端发送信息,服务器端输出信息到输出台上,同时给客户端返回已收到信息。
客户端:
this.button.setOnClickListener(new OnClickListener() {
Socket socket = null;
OutputStream os = null;
InputStream is = null;
String s = null;
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1.创建一个socket对象,通过构造器致命服务端的IP地址,以及接收程序的端口号
socket = new Socket(InetAddress.getByName("192.168.1.102"), 9090);
//2.getOutputStream()发送数据,方法返回OutputStream子类的对象
os = socket.getOutputStream();
//3.具体的输出过程
os.write("我是客户端".getBytes());
//告诉服务端已经发送完毕,否则下面代码不执行
socket.shutdownOutput();
/**
* 接收到服务器返回的数据
*/
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
s = new String(b, 0, len);
}
Log.i("1", ">>>" + s);
} catch (Exception e) {
} finally {
//4.关闭流和socket对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//finally
}
}).start();
}
});//onclick
服务器端:
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
OutputStream os = null;
try {
System.out.println("服务器准备好了。。。");
//1.创建一个serverSocket的对象,通过构造器指明自身的端口号
serverSocket = new ServerSocket(9090);
//2.调用accept返回socket对象
socket = serverSocket.accept();
//3.调用socket的getInputStream()获得一个从客户端发过来的输入流
is = socket.getInputStream();
//4.对获取输入流进行操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len);
System.out.println("接收到的数据" + s);
}
//System.out.println("收到来自于" + socket.getInetAddress().getHostAddress() + "的信息");
/**
* 给客户端返回数据
*
*/
os = socket.getOutputStream();
os.write("woYiJiShouDaoShuJu".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭流以及serverSocket和socket对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}//finally
如果没有明白下载我的源代码:
socket1
http://download.csdn.net/detail/zhaihaohao1/8572549
socket2
http://download.csdn.net/detail/zhaihaohao1/8572553