gpt4 book ai didi

java - bufferwriter 不适用于 java 服务器和 android 客户端

转载 作者:行者123 更新时间:2023-11-30 01:08:26 25 4
gpt4 key购买 nike

大家好,我正在用 android 开发一个聊天应用程序,为此我用 java 开发了一个服务器来路由消息。该系统的工作方式是在连接到服务器后,客户端将其 id 发送到服务器。现在我遇到的问题是,有时服务器会从客户端接收到 id,而大多数时候服务器不会收到 id。请帮助我,我不知道为什么会这样

服务器代码

System.out.println("server starting at port: "+ portnumber);
serverSocket = new ServerSocket(portnumber);
while(true){
System.out.println("waiting for client# "+ clientno);

//connecting to client's call
Socket socket = serverSocket.accept();
System.out.println("Client# "+ clientno +" connected!!!");
//recieving clients name
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int idOfClient = br.read();
System.out.println("" + idOfClient);
}

和客户端代码

public class MainActivity extends Activity {
private static final String TAG = "ChatActivity";

private static final String hostname="192.168.0.100";
private static final int portnumber=60123;
private ChatArrayAdapter chatArrayAdapter;
private ListView listView;
private EditText chatText;
private Button buttonSend;
private boolean side = false;
private BufferedWriter bw;
private BufferedReader br;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

buttonSend = (Button) findViewById(R.id.send);

listView = (ListView) findViewById(R.id.msgview);

chatArrayAdapter = new ChatArrayAdapter(getApplicationContext(), R.layout.right);
listView.setAdapter(chatArrayAdapter);



chatText = (EditText) findViewById(R.id.msg);
chatText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
try {
return sendChatMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
Thread thread=new Thread(){
public void run() {
try {
chatArrayAdapter.add(new ChatMessage(!side, "Connecting to server"));
final Socket socket = new Socket(hostname, portnumber);
chatArrayAdapter.add(new ChatMessage(!side, "Connected to server"));

bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(2);
bw.newLine();
bw.flush();

br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true) {
//if (br.ready()) {
//chatText.setText(br.readLine());
chatArrayAdapter.add(new ChatMessage(!side, br.readLine()));
//}
}
} catch (Exception e) {


}

}
};
thread.start();


buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try{
sendChatMessage();
}
catch(Exception e){

}
}
});

listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
listView.setAdapter(chatArrayAdapter);

//to scroll the list view to bottom on data change
chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(chatArrayAdapter.getCount() - 1);
}
});


}

private boolean sendChatMessage() throws IOException {
String msg=chatText.getText().toString();
try {
bw.write(msg);
bw.newLine();
bw.flush();
chatArrayAdapter.add(new ChatMessage(side, msg));
}
catch(Exception e){
chatArrayAdapter.add(new ChatMessage(side,"Message not sent"));
}
chatText.setText("");

return true;
}
}

最佳答案

像这样在打印后尝试关闭 bufferedReader

int idOfClient=br.read();
System.out.println(""+idOfClient);
br.close();

关于java - bufferwriter 不适用于 java 服务器和 android 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38694596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com