大家好我正在开发一个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;
}
}
答案 0 :(得分:1)
尝试在打印后关闭bufferedReader
int idOfClient=br.read();
System.out.println(""+idOfClient);
br.close();