Telnet后期服务器回复

时间:2014-03-07 00:32:03

标签: java networking telnet

我对telnet通信有一个非常奇怪的问题。当我向服务器发送消息时,服务器将在下一条消息后每次回复。服务器回复如下所示:

  

//错误的IP发送器地址消息

如果我发送“第一条消息”服务器将无法回复。发送“第二条消息”后,服务器将回复第一条消息。每次来自服务器的回复都是迟到的一条消息。 我的沟通如下:

enter image description here

如果我使用Google Play商店中的Telnet应用程序,那么它的工作非常完美,通信就像这样:

 C: first message
 S: [// bad IP transmitters adress message 'first message']
 C: second message
 S: [// bad IP transmitters adress message 'second message']
 C: third message
 S: [// bad IP transmitters adress message 'third message']

有什么方法可以解决它吗?非常感谢你!

代码:

TCPclass

public class TCPclass {

private boolean mRun = false;
String messageFromServer;
private OnMessageReceived mMessageListener = null;
PrintWriter out;
BufferedReader in;

public TCPclass(OnMessageReceived listener) {
    mMessageListener = listener;
}

public void stopClient() {
    mRun = false;
}

public void sendMessage(String message) {
    if (out != null && !out.checkError()) {
        message = message + '\r' + '\n';
        out.print(message);
        out.flush();
        Log.i("Terminal", "Message sent.");
    }
}

public void start(String hostname, int port) {
    mRun = true;
    try {
        InetAddress serverAddr = InetAddress.getByName(hostname);

        Socket socket = new Socket(serverAddr, port);

        try {

            out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())), true);

            // receive the message which the server sends back
            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));

            while (mRun) {
              do
              {

               String messageFromServer = in.readLine();


                if (messageFromServer != null && mMessageListener != null) {
                    mMessageListener.messageReceived(messageFromServer); //append message to Text View
                }
                messageFromServer = null;


             } while (in.ready());
        }

        } catch (Exception e) {
            Log.e("Terminal", "S: Error ", e);
        } finally {
            socket.close();
        }

    } catch (Exception e) {
        Log.e("Terminal", "Establish connection error " + hostname + " "
                + port);
    }
}

public interface OnMessageReceived {
    public void messageReceived(String message);
}
}

MainActivity类:

public class MainActivity extends Activity {

private TCPclass mTcpClass;
public String hostname = "127.0.0.1";
public int port = 7011;
TextView serverMessage;
private connectTask mTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText IPaddress = (EditText) findViewById(R.id.eAddress);
    final EditText ePort = (EditText) findViewById(R.id.ePort);
    final EditText eMessage = (EditText) findViewById(R.id.eMessage);
    serverMessage = (TextView) findViewById(R.id.tOutput);

    Button okButton = (Button) findViewById(R.id.bOK); //Connect button
    okButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) { // connect
            // TODO Auto-generated method stub
            hostname = IPaddress.getText().toString(); 
            String strPort = ePort.getText().toString(); 

            try {
                port = Integer.parseInt(strPort); 

            } catch (NumberFormatException e) {
                Log.e(strPort, "Error port");
            }
            mTask = new connectTask();
            mTask.execute("");
        }
    });

    Button sendButton = (Button) findViewById(R.id.bSend);
    sendButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String message = eMessage.getText().toString();
            serverMessage.append("C: " + message + "\n");
            Log.i("Terminal", "Sending message " + message);
            if (mTcpClass != null) {
                mTcpClass.sendMessage(message);
            }
            eMessage.setText("");
        }
    });
}

public class connectTask extends AsyncTask<String, String, TCPclass> {
    @Override
    protected TCPclass doInBackground(String... message) {
        Log.i("Terminal", "doInBackground.");

        mTcpClass = new TCPclass(new TCPclass.OnMessageReceived() {
            @Override
            // here the messageReceived method is implemented
            public void messageReceived(String message) {
                // this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        mTcpClass.start(hostname, port);
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        serverMessage.append("S: " + Arrays.toString(values) + "\n");
    }
}

1 个答案:

答案 0 :(得分:0)

在我看来,服务器可能会在您未考虑的地方发送空白或额外的行。我是世界上最不忠实的呼唤available()ready()的粉丝,但也许就是这样:

do
{
    String response = in.readLine();
    // ...
} while (in.ready());

...至少为了调试目的,直到你完全理清你发送的每一行的响应。请注意,在此使用while循环肯定是不正确的。