python外连接_与(外部)Python服务器的网络连接

博主创建了一个Java客户端,能够成功连接并从Python服务器接收数据。然而,当尝试在Android应用中运行此客户端时,连接失败,没有数据返回,且没有抛出异常。博主检查了权限设置和防火墙配置,并尝试使用AsyncTask进行异步处理,但问题依然存在。最终发现是由于防火墙导致的连接问题,关闭防火墙后,连接成功。
摘要由CSDN通过智能技术生成

I created a Java client, which should connect to a python server. As a standalone it works perfectly fine, connection is established and data will be sending through.

public class ClientTest {

public static void main(String args[]) {

requestGazeData();

}

public static void requestGazeData() {

ZMQ.Context context = ZMQ.context(1);

ZMQ.Socket request = context.socket(ZMQ.REQ);

request.connect("tcp://xxx.x.x.x:50020");

// Ask for the sub port

request.send("SUB_PORT");

String subPort = request.recvStr();

System.out.println("subPort: " + subPort);

// open a sub port to listen to pupil

ZMQ.Socket subscriber = context.socket(ZMQ.SUB);

subscriber.connect("tcp://xxx.x.x.x:" + subPort);

String gaze = "gaze";

subscriber.subscribe(gaze.getBytes(Charset.forName("UTF-8")));

while (true) {

String msg = subscriber.recvStr();

System.out.println(msg);

subscriber.close();

context.term();

}

}

}

Now this client has to run after the app started, but nothing happens. I don't get any data from the server and it get stuck. It is definitely a problem from the Android side, I reckon moreover especially the activity class. This one looks as follows:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Set function for button

findViewById(R.id.button).setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

ClientTest.requestGazeData();

}

});

}

}

I don't get an exception either. In my Manifest.xml I put all needed permissions:

Lastly, I allowed the connection on the ports in my firewall (via ufw in Ubuntu). I've run out of ideas how to fix it and make it work.

UPDATE:

Due to the one possible answer I tried the AsyncTask, but still anything happens. Here's my updated activity class:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Set function for the button

findViewById(R.id.connectButton).setOnClickListener(

new View.OnClickListener() {

@Override

public void onClick(View v) {

new NetworkCall().execute("");

}

});

}

private class NetworkCall extends AsyncTask {

@Override

protected String doInBackground(String... params) {

ZMQ.Context context = ZMQ.context(1);

ZMQ.Socket request = context.socket(ZMQ.REQ);

request.connect("tcp://xxx.x.x.x:50020");

System.out.println("Connected");

// ask for the sub port

request.send("SUB_PORT");

String subPort = request.recvStr();

System.out.println("subPort: " + subPort);

//int subPortNo = Integer.parseInt(subPort);

// open a sub port to listen to pupil

ZMQ.Socket subscriber = context.socket(ZMQ.SUB);

subscriber.connect("tcp://xxx.x.x.x:" + subPort);

String gaze = "gaze";

subscriber.subscribe(gaze.getBytes(Charset.forName("UTF-8")));

while (true) {

String msg = subscriber.recvStr();

System.out.println(msg);

//subscriber.close();

//context.term();

}

}

@Override

protected void onPostExecute(String result) {

System.out.println("DONE");

}

@Override

protected void onPreExecute() {}

@Override

protected void onProgressUpdate(Void... values) {}

}

@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;

}

}

解决方案

So after trying literally everything, I disabled the firewall and finally the connection worked. Sometimes it can be the small details.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值