2015-08-08 116 views
0

我最近想要发送请求到测试服务器(托管在同一本地wifi上),并在Android应用程序开发中探索教育。Android套接字连接到计算机上的服务器不连接

我所创建的,到目前为止是在我的笔记本电脑小迅捷服务器:

import java.io.PrintStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.Scanner; 


public class Main { 

public static void main(String[] args) throws Exception 
{ 
    System.out.println("Starting..."); 
    ServerSocket server = new ServerSocket(1234); 
    Socket socket = server.accept(); 
    loop(socket); 



} 


public static void loop(Socket socket) throws Exception 
{ 

    Scanner socketScanner = new Scanner(socket.getInputStream()); 
    int num = socketScanner.nextInt(); 
    System.out.println("Received: " + num); 
    num *= 2; 

    PrintStream out = new PrintStream(socket.getOutputStream()); 
    out.println(num); 

    loop(socket); 
} 


} 

测试客户端(在笔记本电脑上也一样)的伟大工程:

import java.io.PrintStream; 
import java.net.Socket; 
import java.util.Scanner; 


public class Main { 
public static void main(String[] args) throws Exception 
{ 
    Socket socket = new Socket("localhost",1234); 

    loop(socket); 


} 


public static void loop(Socket socket) throws Exception 
{ 
    Scanner scanner = new Scanner(System.in); 
    Scanner socketScanner = new Scanner(socket.getInputStream()); 
    System.out.println("Input number:"); 
    int num = scanner.nextInt(); 
    PrintStream p = new PrintStream(socket.getOutputStream()); 
    p.println(num); 

    int response = socketScanner.nextInt(); 
    System.out.println("Received: " + response); 
    loop(socket); 
} 

} 

但后来我尝试从Android Studio连接到Android模拟器,并在运行请求之后显示一条消息(不幸的是)应用程序已停止。

是的,我已经在用户权限添加允许连接?

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.artish1.testapplication" > 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

最后的实际代码正在运行:

public static TextView status; 
public static EditText ip; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    status = (TextView)findViewById(R.id.statusText); 
    ip = (EditText)findViewById(R.id.ipText); 
    Log.i(TAG, "Application: onCreate!"); 

    Button button = (Button)findViewById(R.id.payBenButton); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try{ 
       status.setText("Sending..."); 
       Log.i(TAG,"Starting socket sending... to: localhost"); 
       Socket socket = new Socket("localhost",1234); 
       Log.i(TAG, "Connected."); 
       int num = Integer.parseInt(((EditText) findViewById(R.id.numberText)).getText().toString()); 
       PrintStream out = new PrintStream(socket.getOutputStream()); 
       out.println(num); 
       Log.i(TAG, "Sending: " + num); 

       BufferedReader reader = new BufferedReader(new  InputStreamReader(socket.getInputStream())); 

       String response = reader.readLine(); 
       status.setText("Received: " + response); 
       Log.i(TAG, "Received: " + response); 


      }catch(SocketException e) 
      { 
       Log.i(TAG,e.getMessage()); 
      }catch(IOException e) 
      { 
       Log.i(TAG,e.getMessage()); 
      } 

     } 
    }); 


} 

我花了好3-4小时摆弄周围,但我只是不明白的问题是什么?

+0

双方都在等待输入。首先必须发送 – user210504

+0

服务器正在等待首先发送回来的东西,但客户端先发送然后接收回来的东西。我看不出两个人如何等待同时接收?那么这将是什么? – Artish1

回答

1

尝试用您的笔记本电脑IP或10.0.2.2 IP替换“localhost”。希望这个帮助!

+0

您的Android应用程序中的笔记本电脑IP而不是localhost是一种方法。 –

+0

可悲的是,也没有工作。我用192.168.1.84(这是我的笔记本电脑ip最后我检查)替换它。我也试过10.0.2.2,但也没有工作。 – Artish1

+0

提供您的logcat以获取更多信息 – BNK

0

没关系,我只是需要使用不同的线程。

相关问题