如题,我想编一个Android客户端与计算机上的Java服务端进行简单Socket通讯,问题却出现了,Android一直抛出异常,一直连不上计算机服务端,上代码,为什么呢?ip没错并且Androidmanife也有加interent权限!
服务端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class PcServer{
/**
* @param args
*/
ServerSocket ss;
Socket socket;
public static void main(String[] args) {
// TODO Auto-generated method stub
PcServer ps = new PcServer();
ps.setServer(8891);
ps.setListener();
}
public void setServer (int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("resource")
public void setListener() {
try {
while(true) {
System.out.println("正在作为服务器");
socket = ss.accept();
while(true) {
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
String result = dis.readUTF();
System.out.println(result);
String reply;
Scanner sc = new Scanner(System.in);
reply = sc.nextLine();
dos.writeUTF(reply);
}
//System.out.println("客户端已断开");
}
}catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("作为服务器失败");
}
}
}
客户端:
package com.example.aclient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView chatting;
EditText callText;
Button send;
Socket s;
DataInputStream dis;
DataOutputStream dos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
connectServer();
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String call = callText.getText().toString();
try {
dos.writeUTF(call);
chatting.append(call);
String answer = dis.readUTF();
chatting.append(answer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void initComponent() {
chatting = (TextView) findViewById(R.id.chatting);
callText = (EditText) findViewById(R.id.reply);
send = (Button) findViewById(R.id.send);
}
public void connectServer() {
try {//总是在这里抛出异常,哪里错了?
s = new Socket("172.27.35.1",8891);
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "连接服务器失败,请尝试重启软件!", 100).show();
return ;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------解决方案--------------------
楼主你手机和你server在一个内网么么?或者你的server的ip是可以在公网上面访问的?
------解决方案--------------------
你先看网络通不通。
------解决方案--------------------
联网的代码必须在非UI线程中运行。
------解决方案--------------------
你先看网络通不通。
无线都开了,有通的。我想问一下,Sokcet不能在Activity的oncreate方法里实例化?
新起一个线程去实例化
------解决方案--------------------
看一下你这个ip能不能ping到啊
------解决方案--------------------