我正在尝试在服务器(PC)和客户端(Android 应用程序)之间制作一个简单的聊天应用程序。当我从客户端向服务器发送消息时一切正常并且消息显示在 JFrame 上,但是当我从服务器向客户端发送消息时,只有第一条消息有效并到达客户端,而不是以下其他消息。我认为问题出在 Thread 类的 run() 方法内部。
这是客户端代码:
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button;
private static Socket s;
String message="";
private static String ip="192.168.0.112";
private static int port=3336;
String messageReceived="";
static DataInputStream in;
static DataOutputStream out;
@Override
protected void onStart() {
super.onStart();
Log.d("myTag", "App Started");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
button=(Button)findViewById(R.id.button);
}
public void send(View v){
message=editText.getText().toString();
myTaskSend mt=new myTaskSend();
mt.execute();
editText.setText("");
}
public void connect(View v){
myTaskConnect mt2=new myTaskConnect();
mt2.execute();
}
class myTaskSend extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try{
Log.d("myTag", "Message Sent: "+message);
out.writeUTF(message);
if(message.toLowerCase().equals("exit")){
Log.d("myTag", "Connection Closed");
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
class myTaskConnect extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try{
s=new Socket(ip,port);
Log.d("myTag", "Connection Successful");
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
ReadThread t=new ReadThread();
t.start();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
class ReadThread extends Thread{
@Override
public void run() {
super.run();
try{
while(true){
messageReceived=in.readUTF();
Log.d("myTag", "Message Received: "+messageReceived);
textView.setText(textView.getText()+"Server: "+messageReceived+"\n");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
这是服务器端代码:
public class Server {
static TextArea textArea;
static TextField textField;
static ServerSocket ss;
static Socket s;
static DataInputStream in;
static DataOutputStream out;
static int port=3336;
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server window = new Server();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String msgin="";
try {
System.out.println("Starting the server...");
ss=new ServerSocket(port);
System.out.println("Server active on the port: "+port);
s=ss.accept();
System.out.println("Connection succesfull");
ss.close();
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
System.out.println("Waiting for a message from the client...");
while(!msgin.toLowerCase().equals("exit")) {
msgin=in.readUTF();
textArea.setText(textArea.getText()+"Client: "+msgin+"\n");
System.out.println("Message received");
}
s.close();
System.out.println("Closing the connection");
}catch(IOException e){
System.err.println(e);
}
}
public Server() {
initialize();
}
private void initialize() {
frame = new JFrame("SERVER");
frame.setBounds(100, 100, 582, 451);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textArea = new TextArea();
textArea.setBounds(23, 10, 510, 239);
frame.getContentPane().add(textArea);
textField = new TextField();
textField.setBounds(23, 319, 372, 49);
frame.getContentPane().add(textField);
JButton button = new JButton("New button");
frame.getRootPane().setDefaultButton(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String msgout="";
msgout=textField.getText().trim();
out.writeUTF(msgout);
System.out.println("Message sent to the client: "+msgout);
textField.setText("");
}catch(IOException e1) {
System.out.println(e1);
}
}
});
button.setBounds(425, 319, 108, 49);
frame.getContentPane().add(button);
}
}
谢谢!
答案 0 :(得分:-1)
请在向客户端发送数据后尝试方法out.flush();
:
out.writeUTF(msgout);
out.flush();
System.out.println("Message sent to the client: "+msgout);