kotlin tcp 套接字客户端不工作 python 服务器
kotlin tcp socket client not working python server
我想制作一个使用套接字连接到我的 python 服务器的应用程序。
当我按下连接按钮时,它甚至没有在我的 PS 上打印得到的连接,请帮忙。谢谢
我在 kotlin 中有这个基本代码:
//Kotlin Code
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import java.io.DataOutputStream
import java.net.Socket
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.start_page)
fun connect(v : View) {
try{
val soc = Socket("192.168.1.5", 1419)
val dout = DataOutputStream(soc.getOutputStream())
dout.writeUTF("1")
dout.flush()
dout.close()
soc.close()
}
catch (e:Exception){
e.printStackTrace()
}
}
}
单击按钮时连接功能被激活,这是我的开始屏幕的xml代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/connect"
android:layout_width="108dp"
android:layout_height="50dp"
android:layout_marginBottom="127dp"
android:layout_marginEnd="228dp"
android:layout_marginStart="256dp"
android:onClick="connect"
android:text="@string/connect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
这是我在 python 服务器中的代码:
#Python code
import socket
s = socket.socket()
s.bind(('0.0.0.0', 1419))
s.listen(5)
c, addr = s.accept()
print ('Got connection from', addr)
code_encoded = c.recv(1024)
code_decoded = code_encoded.decode('utf-8')
print(code_decoded)
c.close()
s.close()
我通过在我的函数中实现 asynctask 来修复它,并使用 java 而不是 kotlin,但它在 kotlin 中也应该类似地工作。
现在的功能是这样的,
class ServerConnection extends AsyncTask<MainActivity.ConnParams, Void, Void> {
@Override
protected Void doInBackground(MainActivity.ConnParams... params)
{
String ip = params[0].ip;
int port = params[0].port;
String message = params[0].message;
try
{
Socket socket = new Socket(ip, port);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write(message);
printWriter.flush();
printWriter.close();
socket.close();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
尽管 asynctask 不适用于需要在后台运行较长时间的任务,但此时我建议使用 android services.
我想制作一个使用套接字连接到我的 python 服务器的应用程序。
当我按下连接按钮时,它甚至没有在我的 PS 上打印得到的连接,请帮忙。谢谢
我在 kotlin 中有这个基本代码:
//Kotlin Code
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import java.io.DataOutputStream
import java.net.Socket
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.start_page)
fun connect(v : View) {
try{
val soc = Socket("192.168.1.5", 1419)
val dout = DataOutputStream(soc.getOutputStream())
dout.writeUTF("1")
dout.flush()
dout.close()
soc.close()
}
catch (e:Exception){
e.printStackTrace()
}
}
}
单击按钮时连接功能被激活,这是我的开始屏幕的xml代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/connect"
android:layout_width="108dp"
android:layout_height="50dp"
android:layout_marginBottom="127dp"
android:layout_marginEnd="228dp"
android:layout_marginStart="256dp"
android:onClick="connect"
android:text="@string/connect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
这是我在 python 服务器中的代码:
#Python code
import socket
s = socket.socket()
s.bind(('0.0.0.0', 1419))
s.listen(5)
c, addr = s.accept()
print ('Got connection from', addr)
code_encoded = c.recv(1024)
code_decoded = code_encoded.decode('utf-8')
print(code_decoded)
c.close()
s.close()
我通过在我的函数中实现 asynctask 来修复它,并使用 java 而不是 kotlin,但它在 kotlin 中也应该类似地工作。
现在的功能是这样的,
class ServerConnection extends AsyncTask<MainActivity.ConnParams, Void, Void> {
@Override
protected Void doInBackground(MainActivity.ConnParams... params)
{
String ip = params[0].ip;
int port = params[0].port;
String message = params[0].message;
try
{
Socket socket = new Socket(ip, port);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write(message);
printWriter.flush();
printWriter.close();
socket.close();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
尽管 asynctask 不适用于需要在后台运行较长时间的任务,但此时我建议使用 android services.