0

这是我的客户代码

package com.example.simple_client1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class MainActivity extends Activity {

     private Socket client;
     private PrintWriter printwriter;
     private EditText textField;
     private Button button;
     private String messsage;

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

        textField = (EditText) findViewById(R.id.editText1); //reference to the text field
          button = (Button) findViewById(R.id.button1);   //reference to the send button

          //Button press event listener
          button.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {

                messsage = textField.getText().toString(); //get the text message on the text field
                textField.setText("");      //Reset the text field to blank

                try {

                 client = new Socket("10.0.2.2", 4444);  //connect to server
                 printwriter = new PrintWriter(client.getOutputStream(),true);
                 printwriter.write(messsage);  //write the message to output stream

                 printwriter.flush();
                 printwriter.close();
                 client.close();   //closing the connection

                } catch (UnknownHostException e) {
                 e.printStackTrace();
                } catch (IOException e) {
                 e.printStackTrace();
                }
               }
              });
    }

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

}

我的服务器代码

包 a_server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class simple_server {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;
    public static void main(String[] args) {

        try {
            serverSocket = new ServerSocket(4444);  //Server socket
        } catch (IOException e) {
            System.out.println("Could not listen on port: 4444");
        }


        while (true) {
            try {
                 System.out.println("Server started. Listening to the port 4444");
                clientSocket = serverSocket.accept();//accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                message = bufferedReader.readLine();

                System.out.print(message);
                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex) {
                System.out.println("Problem in message reading");
            }
        }
    }
}

什么地方出了错 ?

  1. 为什么它在服务器部分一直显示 Null ?

  2. 在服务器控制台端,即使我重新运行程序,它也包含以前的空值!怎么冲洗呢?

提前致谢

4

1 回答 1

0
  1. 你在读台词,但你不是在写台词。使用 println() 而不是 write()。

  2. 在使用它之前,您没有检查 readLine() 的结果是否为 null。您必须先执行此操作,如果为真,请关闭套接字,并且不要像输入一样处理 null。它不是。

  3. 当你得到一个异常时,打印它的消息。不要只是完全替换你自己的一个。否则调试就变成了猜谜游戏。

于 2013-07-19T22:17:29.687 回答