0
我在“世界,你好” 客户端/服务器应用程序工作,我与服务器之间的连接,这是写在阶并运行我的电脑上挣扎和客户端,这是写在安卓并在我的手机上运行。简单使用客户端 - 服务器应用插座
我连接了两个应用程序在同一个子网上,我的路由器,并为他们设置相应的IP和端口。 问题是,用Android编写的客户端瞬间停止。
P.S.出于安全原因,我没有发布我的IP地址。
服务器代码是:
import java.net._
import java.io._
import scala.io._
object MyServer extends App
{
try
{
val server = new ServerSocket(4242)
println("Serve initialized:")
val client = server.accept
val in = new BufferedReader(new InputStreamReader(client.getInputStream()))
val out = new PrintStream(client.getOutputStream())
var message =in.readLine()
while(message != null)
{
println("Server received:" + message)
out.println("Message received")
out.flush
if (message.equals("Disconnect"))
{
client.close;
server.close;
println("Server closing:")
}
message= in.readLine()
}
}
catch
{
case e: Exception => println(e.getStackTrace); System.exit(1)
}
}
的客户端代码:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ghiurutan.clientv1.MainActivity">
<Button
android:text="@string/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="158dp" />
<TextView
android:text="@string/text_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView"
android:layout_alignBottom="@+id/editText"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:layout_marginBottom="125dp"
android:layout_alignBottom="@+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ghiurutan.clientv1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.ghiurutan.clientv1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private Socket socket;
private static final int SERVER_PORT=4242;
private static final String SERVER_IP="";
private EditText editText;
private Button button;
private PrintWriter printWriter;
private String messageToSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(
new Runnable()
{
public void run()
{
try{
socket=new Socket(InetAddress.getByName(SERVER_IP),SERVER_PORT);
}catch(UnknownHostException e)
{
System.out.println(e.getLocalizedMessage());
}catch(IOException e)
{
System.out.println(e.getLocalizedMessage());
}
}
}
).start();
}
@Override
protected void onStart()
{
super.onStart();
try{
printWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
}catch(UnknownHostException e)
{
System.out.println(e.getLocalizedMessage());
}catch(IOException e)
{
System.out.println(e.getLocalizedMessage());
}
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
messageToSend=editText.getText().toString();
editText.setText("");
printWriter.println(messageToSend);
}
}
);
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onPause()
{
super.onPause();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
我必须提到,我只是编程的初学者,因此欢迎任何帮助或建议。 我应该在这种情况下使用端口转发吗?有人告诉我,把服务器放在云上是一个好主意,因为那样会更明显。
'并为them'相应的IP和端口。不清楚。你做了什么? – greenapps
我看到的第一个问题是,你不是在一个线程中的所有网络代码。是的,你打开线程中的套接字。好。但不是印刷作家和println()。所有网络代码应该在线程或AsyncTask中执行。 – greenapps
所以你会在logcat中清楚地看到一个'NetworkOnMainThreadException'。而这会让你的应用崩溃。你没有报告。 – greenapps