android用socket传递数据给服务器端

vicky-y 2014-11-11 05:42:54
近期在做一个小项目,想在多个activity之间用同一个socket往服务器端发送数据,因为socket是长连接嘛,如果发送一次关一次这个长连接就没意义了。所以写了一个单独的类用来socket连接。现在问题是:我每次只能连接到服务器并发送一次数据,之后再发送就没反应了。下面贴上代码:
这是用来socket连接服务器的类:

//现在直接在这里设定一个socket只为了连接用,
public class ConnectJudgment {

public PrintWriter out;
public String foreMessage="connect before";
public String laterMessage="connect before";
public static String Ip="";
public static int port=50001;
public static int flag = 0;
public static Socket socket = null;
protected ConnectJudgment() {
connectTask task = new connectTask(this);
task.execute();
}

class connectTask extends AsyncTask<String,Integer, String>
{
public connectTask(ConnectJudgment connectJudgment) {
// TODO Auto-generated constructor stub
}
@Override
protected String doInBackground(String... params) {
try{
//connect to server with ip&port
socket = new Socket(InetAddress.getByName(Ip),port);
flag = 1;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(ConnectJudgment.socket.getOutputStream())), true);
while(socket.isConnected())
{
if(!foreMessage.equals(laterMessage))
{
out.println(laterMessage);
out.flush();
foreMessage = laterMessage;
}
}
//socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
}


这个是调用上面那个socket连接类,并发送数据:

public class Connection_Activity extends ActionBarActivity{

//this is for hide soft keyboard.
private View connectionLayout;
//sp is to save data from last time.
private SharedPreferences sp;
//show information when you r logging.
private ProgressDialog progressDialog;
private EditText Ip;
private String lastIp = "";
private Button connectionButton;
protected PrintWriter out;
private ConnectJudgment con;
//if ip is the right rule.
public boolean isIPAddress(String str)
{
Pattern pattern = Pattern.compile( "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b" );
return pattern.matcher( str ).matches();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_connection);
//initpush();
//hide soft keyboard from current view.
connectionLayout = (View)findViewById(R.id.connectionlayout);
connectionLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
});
//initial sp object .default mode is 0.
sp=this.getSharedPreferences("ip",0);
//get the control
Ip = (EditText)findViewById(R.id.IpAddress);
//to read if there is any data from last time inputing.
readSharedPreferences();
connectionButton = (Button)findViewById(R.id.connectButton);
connectionButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
//这里得到了ip地址,是另外的函数不管这个
if(isIPAddress(ConnectJudgment.Ip))
{
saveSharedPreferences();
con = new ConnectJudgment();
try
{
//show waiting info
progressDialog = ProgressDialog.show(Connection_Activity.this, "",
getResources().getString(R.string.waitConnection));
progressDialog.setCancelable(false);
new Handler().postDelayed(run,5000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(getApplicationContext(), R.string.IpWrong, Toast.LENGTH_LONG).show();
}
}
});
}
//wait a few seconds to jump to another activity.
Runnable run = new Runnable()
{
public void run()
{
if(ConnectJudgment.flag == 1)
{
//理论上来讲,后台的循环程序一直不断地检测两次message有没有变化
//而message一旦变了,就进入out.println()发送数据。
con.laterMessage = "";
//click the connect button
Intent intent = new Intent();
intent.setClass(Connection_Activity.this, SocketTest.class);
startActivity(intent);
progressDialog.cancel();
finish();
}
else
{
progressDialog.cancel();
Toast.makeText(getApplicationContext(), R.string.socketWrong, Toast.LENGTH_LONG).show();
}
}
};
//save data to use it next time.
private void saveSharedPreferences(){
SharedPreferences.Editor editor=sp.edit();
editor.putString("Ip", Ip.getText().toString());
editor.commit();
}
//get data from last inputing.
private void readSharedPreferences() {
String IpAddress=sp.getString("Ip", "");
Ip.setText(IpAddress);
}
}

紧接着我在另外一个Activity中调用这个socket再传递数据就不行了,

public class SocketTest extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.socket_test);
String message = "sent failed";
sendMessage(message);
}
public void sendMessage(String mess)
{
try {
PrintWriter out = null;
if(ConnectJudgment.socket.isConnected())
{
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(ConnectJudgment.socket.getOutputStream())), true);
out.println(mess);
}
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



服务器端:

public void run() {
// TODO Auto-generated method stub
try
{
//get a port from system which is free now.
serverSocket= new ServerSocket(0);
Port = serverSocket.getLocalPort();
while(true)
{
//accept the data from client
Socket client = serverSocket.accept();
System.out.println("accept");
try
{
//get the data info
//inputstream to receive data,outputstream to write data
BufferedReader in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
//convert the info into string
info = in.readLine();
System.out.println("info: "+info);
runCommand(info);
//turn off the source
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
client.close();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}


服务器端打印了第一次的info: 之后就什么都没有了。

不知道为什么只传递成功了一次数据。
...全文
2821 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大鲸鱼鱼 2016-11-14
  • 打赏
  • 举报
回复
加心跳帧啊!!!!!!!!!!!!!!11111
yfijwan135 2016-05-26
  • 打赏
  • 举报
回复
我也遇到同样的问题,楼主有具体demo吗?求赐教,只要服务端的就行,我现在作为服务端,就第一次有效,之后就接收不到了,写在service里的
vicky-y 2014-12-05
  • 打赏
  • 举报
回复
结贴, 不知道为什么没人来回答=。 = 最后自己还是查到了。 之所以每次只能成功发送一次数据有两个原因: 1. 在server端的serverSocket.aceept()放在while(true)的里面,这句话的意思是接受新建立的socket连接并阻塞等待这个socket发送过来数据,而这个只可以接受一次当前socket的数据。 2.因为BufferedReader在一次接收数据完成后就直接关闭。 综上所述,这个服务器这样写是接受不了同一socket用户的多条数据的。 修改:将serverSocket.accept()这句话放在while(true)的前面,然后重新写一个函数用来关闭和socket相关的对象。 而在while(true)接收数据的代码中重新开启一个Thread来执行接收数据的代码,在这个Thread的run函数中加入while(...)循环获取数据的代码。记得不要在这里就关闭BufferedReader对象。 因为这个错误阻塞我两个周,哎,,, 基础知识不懂造成的。
wangzi_17 2014-11-25
  • 打赏
  • 举报
回复
试下ConnectJudgment里的socket设置get/set方法
vicky-y 2014-11-24
  • 打赏
  • 举报
回复
怎么没人来回答阿 快来人啊,,,,,大神们

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧