TCP / IP连接和发送但在初始发送后不起作用

时间:2012-11-26 20:43:59

标签: java android tcp tcpclient tcp-ip

HELP!我要疯了!我正在为服务器使用Visual Basic 6.0 Winsock。我能够保持活跃的连接,我甚至从我的Android应用程序收到VB应用程序“VER:Android,LAT:28.111921,LNG:-81.950433,ID:1038263,SND:0,VDO:0”>我解析并将数据放在他们的字段中。

在我的初始连接后,我尝试从VB向服务器发送一条简单的消息,但我从未收到过。每当我关闭我的VB.NET应用程序时,我会注意到我在LogCat中接收到这一点:

11-26 15:38:16.567:I / TcpClient(986):收到:null

我是Android新手,任何帮助都会受到高度赞赏。我需要帮助尝试通过我的客户端(Android)和服务器(VB)来回接收和发送消息

 package com.WheresMySon;


 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View.OnClickListener;
 import android.widget.TextView;



 public class TcpClient extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new TcpClientTask().execute();

}


class TcpClientTask extends AsyncTask<Void, Void, Void> {
    private static final int TCP_SERVER_PORT = 1234;
    private boolean error = false;
    Boolean SocketStarted = false;

    protected Void doInBackground(Void... arg0) {
        try {
            Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);


                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            //send output msg
            String outMsg = "VER:Android,LAT:28.111921,LNG:-81.950433,ID:1038263,SND:0,VDO:0";
            out.write(outMsg);
            out.flush();

            Log.i("TcpClient", "sent: " + outMsg);

            //accept server response

            String inMsg = in.readLine() + System.getProperty("line.separator");

            Log.i("TcpClient", "received: " + inMsg);



            //close connection
        } catch (UnknownHostException e) {
            error = true;
            e.printStackTrace();
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } 

        return null;
    }

    protected void onPostExecute() {
        if(error) {
            // Something bad happened

        }
        else {
            // Success


        }

    }
}

//replace runTcpClient() at onCreate with this method if you want to run tcp client as a service
private void runTcpClientAsService() {
    Intent lIntent = new Intent(this.getApplicationContext(), TcpClientService.class);
    this.startService(lIntent);
}

 }

0 个答案:

没有答案