[英]Client running on Android emulator cannot create socket
StackOverflow中提到的討論 - 對我沒有幫助
我正在嘗試讓客戶端在Android模擬器上運行,連接到在同一台機器上運行在eclipse上的服務器。 服務器配置為偵聽端口5000.服務器似乎不是問題。 當我在eclipse上運行客戶端時,他們可以打開一個套接字並進行通信。 當我嘗試在Android模擬器上運行Client類時,mainActivity會告訴客戶端在創建它時進行連接。 但是由於某種原因,dbugging流程到達catch塊,而不是創建socket。
我也試過使用client.connect("10.0.2.2",5000);
但它沒有幫助。
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Client client = new Client(this);
try {
client.connect("192.168.1.10",5000);
} catch (IOException e) {
e.printStackTrace();
}
}
Client.java
package com.example.oshri.p;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Observable;
public class Client extends Observable implements Runnable {
MainActivity creatingActivity; // the activity that creates Client
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private boolean connected;
private int port=5555; //default port
private String hostName="localhost";//default host name
public Client(MainActivity activity) {
connected = false;
this.creatingActivity = activity;
}
public void connect(String hostName, int port) throws IOException {
if(!connected)
{
this.hostName = hostName;
this.port = port;
socket = new Socket(hostName,port);
//get I/O from socket
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(),true);
connected = true;
//initiate reading from server...
Thread t = new Thread(this);
t.start(); //will call run method of this class
}
}
public void sendMessage(String msg) throws IOException
{
if(connected) {
pw.println(msg);
} else throw new IOException("Not connected to server");
}
public void disconnect() {
if(socket != null && connected)
{
try {
socket.close();
}catch(IOException ioe) {
//unable to close, nothing to do...
}
finally {
this.connected = false;
}
}
}
public void run() {
String msg = ""; //holds the msg recieved from server
try {
while(connected && (msg = br.readLine())!= null)
{
creatingActivity.displayServerAnswer("Server:"+msg);
this.setChanged();
this.notifyObservers(msg);
}
}
catch(IOException ioe) { }
finally { connected = false; }
}
public boolean isConnected() {
return connected;
}
public int getPort(){
return port;
}
public void setPort(int port){
this.port = port;
}
public String getHostName(){
return hostName;
}
public void setHostName(String hostName){
this.hostName = hostName;
}
}
@greenapps指出了問題:套接字是在主活動中創建的。
@Squonk指出了這樣的問題,IP應該是10.0.2.2 - 模擬器中用來連接到模擬器運行的機器的本地主機的轉換地址
代碼已編輯:
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ClientThread clientThread = new ClientThread(this);
try {
clientThread.startClientThread("10.0.2.2",5000);
} catch (IOException e) {
e.printStackTrace();
}
}
ClientThread.java
package com.example.oshri.parkit;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Observable;
public class ClientThread extends Observable implements Runnable {
MainActivity creatingActivity; // the activity that creates ClientThread
private Socket socket; //Uses to connect to the server
private BufferedReader br; //For reading input from server.
private PrintWriter pw; //For writing output to server.
private boolean connected; //Status of client.
private int port=5555; //default port
private String hostName="localhost";//default host name
public ClientThread(MainActivity activity) {
connected = false;
this.creatingActivity = activity;
}
public void startClientThread(String hostName, int port) throws IOException {
if(!connected)
{
this.hostName = hostName;
this.port = port;
Thread t = new Thread(this);
t.start(); //will call run method of this class, that first thing will create a client socket(cannot create socket in main thread)
}
}
public void sendMessage(String msg) throws IOException
{
if(connected) {
pw.println(msg);
} else throw new IOException("Not connected to server");
}
public void disconnect() {
if(socket != null && connected)
{
try {
socket.close();
}catch(IOException ioe) {
//unable to close, nothing to do...
}
finally {
this.connected = false;
}
}
}
private void connect(){
// create socket to connect the server
try {
socket = new Socket(hostName, port);
}
catch(SocketException e){
System.out.println("client socket could not be created");
}
catch (UnknownHostException e){
System.out.println("UnknownHostException");
}
catch (IOException e){
System.out.println("IOException");
}
// create buffers for socket IO
try{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(),true);
}
catch (IOException e){
System.out.println("socket buffers could not be created");
}
connected = true;
}
public void run() {
connect(); //connects the server
String msg = ""; //holds the msg recieved from server
try {
while(connected && (msg = br.readLine())!= null)
{
//System.out.println("Server:"+msg);
creatingActivity.displayServerAnswer("Server:"+msg);
//notify observers//
this.setChanged();
//notify+send out recieved msg to Observers
this.notifyObservers(msg);
}
}
catch(IOException ioe) { }
finally { connected = false; }
}
public boolean isConnected() {
return connected;
}
public int getPort(){
return port;
}
public void setPort(int port){
this.port = port;
}
public String getHostName(){
return hostName;
}
public void setHostName(String hostName){
this.hostName = hostName;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.