求教,嘗試客戶端向服務(wù)端發(fā)信息,服務(wù)端沒收到,再發(fā)客戶端報(bào)錯(cuò)?socket write error
public class SocketClient {
public static void main(String[] args) {
SocketClient client = new SocketClient();
client.start();
}
public void start() {
BufferedReader inputReader = null;//收消息
BufferedWriter writer = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1",9898);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
inputReader = new BufferedReader(new InputStreamReader(System.in));
String inputContent;
while(!(inputContent = inputReader.readLine()).equals("bye")){
writer.write(inputContent+"\n");
writer.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
writer.close();
inputReader.close();
inputReader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2016-09-08
客戶端代碼沒有問題,檢查一下服務(wù)端代碼是否喲問題,先要啟動(dòng)服務(wù)器,再啟動(dòng)客戶端