Socket communication between two apps on Android

China☆狼群 提交于 2019-12-03 16:15:20

As I see, you create a new socket whenever user click button send, right?
I recommend you should init it only one time when user click connect, then you use it in send click event ( because this is TCP, you will disconnect to server if you create new instance of socket)
So, you should remove these lines in sendMessageToServer :

s = new Socket(serverIpAddress, SERVERPORT);
s.close();

and this line in runTcpConnection

s.close();

Socket should close whenever you don't want communicate with the server (onstop is an example, or when change activity...)
Also you should create only one instance of BufferedWriter too.
Hope this help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!