Android 编程 socket问题 请教大神呀【高悬赏】【急!】
小弟刚学习Android编程,在做socket编程的时候,服务端无法监听到信息,是怎么回事?下面是服务端的java做的代码importjava.io.BufferedRe...
小弟刚学习Android编程,在做socket编程的时候,服务端无法监听到信息,是怎么回事?
下面是服务端的java做的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class testSocket {
public static void main(String[] args) {
new ServerThread().start();
}
}
// 创建一个线程在后台监听
class ServerThread extends Thread {
private static int Port = 10000;
ServerSocket serversocket = null;
public void run() {
try {
// 创建一个serversocket对象,并让他在Port端口监听
serversocket = new ServerSocket(Port);
System.out.println("正在Prot=10000监听!");
while (true) {
// 调用serversocket的accept()方法,接收客户端发送的请求
Socket socket = serversocket.accept(); //(这里过不去了,没连接成功)
System.out.println("已经连接上客户端!");
BufferedReader buffer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// 读取数据
String msg = buffer.readLine();
System.out.println("msg:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
serversocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
下面是客户端Android代码:
在manifest中也添加了相关的配置,
log里没输出什么东西,,小弟不知道哪里有什么错误了,请教大神帮忙,小弟先谢过了 展开
下面是服务端的java做的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class testSocket {
public static void main(String[] args) {
new ServerThread().start();
}
}
// 创建一个线程在后台监听
class ServerThread extends Thread {
private static int Port = 10000;
ServerSocket serversocket = null;
public void run() {
try {
// 创建一个serversocket对象,并让他在Port端口监听
serversocket = new ServerSocket(Port);
System.out.println("正在Prot=10000监听!");
while (true) {
// 调用serversocket的accept()方法,接收客户端发送的请求
Socket socket = serversocket.accept(); //(这里过不去了,没连接成功)
System.out.println("已经连接上客户端!");
BufferedReader buffer = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// 读取数据
String msg = buffer.readLine();
System.out.println("msg:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
serversocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
下面是客户端Android代码:
在manifest中也添加了相关的配置,
log里没输出什么东西,,小弟不知道哪里有什么错误了,请教大神帮忙,小弟先谢过了 展开
1个回答
展开全部
//客户端的实现
public class TestSocket extends Activity {
private TextView text1;
private Button but1;
private EditText edit1;
private final String DEBUG_TAG="mySocketAct";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1=(TextView)findViewById(R.id.text1);
but1=(Button)findViewById(R.id.but1);
edit1=(EditText)findViewById(R.id.edit);
but1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Socket socket=null;
String mesg=edit1.getText().toString()+"\r\n";
edit1.setText("");
Log.e("dddd", "sent id");
try {
socket=new Socket("172.29.107.1",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)
{
text1.setText(mstr);
}else
{
text1.setText("数据错误");
}
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());
}
}
});
}
}
-----------------------------------------------------------------------------
//服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class AndroidServer implements Runnable{
public void run() {
try {
ServerSocket serverSocket=new ServerSocket(54321);
while(true)
{
System.out.println("等待接收用户连接:");
//接受客户端请求
Socket client=serverSocket.accept();
try
{
//接受客户端信息
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
String str=in.readLine();
System.out.println("read: "+str);
//向客户端返回数据
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
out.println("Service return: "+str);
in.close();
out.close();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
client.close();
System.out.println("close");
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String [] args)
{
Thread desktopServerThread=new Thread(new AndroidServer());
desktopServerThread.start();
}
}
public class TestSocket extends Activity {
private TextView text1;
private Button but1;
private EditText edit1;
private final String DEBUG_TAG="mySocketAct";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1=(TextView)findViewById(R.id.text1);
but1=(Button)findViewById(R.id.but1);
edit1=(EditText)findViewById(R.id.edit);
but1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
Socket socket=null;
String mesg=edit1.getText().toString()+"\r\n";
edit1.setText("");
Log.e("dddd", "sent id");
try {
socket=new Socket("172.29.107.1",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)
{
text1.setText(mstr);
}else
{
text1.setText("数据错误");
}
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());
}
}
});
}
}
-----------------------------------------------------------------------------
//服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class AndroidServer implements Runnable{
public void run() {
try {
ServerSocket serverSocket=new ServerSocket(54321);
while(true)
{
System.out.println("等待接收用户连接:");
//接受客户端请求
Socket client=serverSocket.accept();
try
{
//接受客户端信息
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
String str=in.readLine();
System.out.println("read: "+str);
//向客户端返回数据
PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
out.println("Service return: "+str);
in.close();
out.close();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
client.close();
System.out.println("close");
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String [] args)
{
Thread desktopServerThread=new Thread(new AndroidServer());
desktopServerThread.start();
}
}
追问
172.29.107.1 这个ip是指什么? 我用10.0.2.2 不可以吗?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询