我正在开发项目,我需要将Android应用程序连接到服务器,现在我要连接并能够从客户端向服务器发送消息,但不能将服务器发送到客户端。
我尝试使用BufferedReader和PrintWriter,但没有帮助我使用套接字编程。
不要用重复标记这个问题,因为我看到了很多解决方案,但是那些单向客户端服务器也不是服务器到客户端。
我尝试了很多解决方案,但没有帮助他们中的任何一个,所以我猜测将有一些关于Android应用程序从服务器接收消息,我错过了。
急需帮助我没有选择所以发布这个问题帮助我们......
我的服务器代码是这样的
public class SimpleTextServer {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static OutputStreamWriter outputStreamWriter;
private static BufferedReader bufferedReader;
private static BufferedWriter bufferedWriter;
private static String message;
private static PrintWriter printwriter;
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
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.println(message);
printwriter = new PrintWriter(clientSocket.getOutputStream(), true);
printwriter.write("u r dere"+message+"\n"); // write the message to output stream
printwriter.flush();
inputStreamReader.close();
bufferedReader.close();
printwriter.close()
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
我的Android客户端代码就是这个,我对那个给我带来麻烦的部分发表评论。
公共类SlimpleTextClientActivity扩展了Activity {
private Socket client;
private PrintWriter printwriter;
private BufferedReader inFromUser;
//private static ObjectInputStream printreader;
private static BufferedReader bufferedReader;
private static InputStreamReader inputStreamReader;
private EditText textField;
private Button button;
private String messsage;
private String message;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slimple_text_client);
Calendar c1 = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String strdate1 = sdf1.format(c1.getTime());
messsage=strdate1;
textField = (EditText) findViewById(R.id.editText1); // reference to the text field
button = (Button) findViewById(R.id.button1); // reference to the send button
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = messsage+textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("10.129.26.120", 4444); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
/*
inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
bufferedReader.ready();*/
//message = bufferedReader.readLine();
// System.out.println(message);
// TextView txtdate1 = (TextView) findViewById(R.id.textView1);
// txtdate1.setText(message);
printwriter.close();
bufferedReader.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.slimple_text_client, menu);
return true;
}
}