為什么我寫的javaSocket通信,客戶端連接服務(wù)端了,然后客戶端發(fā)送消息給服務(wù)端,服務(wù)端接收,這個(gè)時(shí)候服務(wù)端接收的文件獲取不到,只有等到客戶端關(guān)閉的時(shí)候,服務(wù)端才能接到。這是代碼
package?server;
import?java.io.DataInputStream;
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
import?java.net.InetAddress;
import?java.net.ServerSocket;
import?java.net.Socket;
public?class?Server?{
public?static?void?main(String[]?args){
//1.創(chuàng)建一個(gè)服務(wù)器端socket即serversocket指定綁定的端口并監(jiān)聽此端口
ServerSocket?serverSocket=null;
String?info=null;
InputStream?is=null;
OutputStream?os=null;
try?{
serverSocket?=?new?ServerSocket(8888);
//2.調(diào)用accept()方法開始監(jiān)聽,等待客戶端的連接
//讓客戶端一直訪問
Socket?socket?=null;
while(true){
socket?=?serverSocket.accept();
InetAddress?ss?=?socket.getInetAddress();
System.out.println(ss.getHostAddress());
//3.獲取輸入流,并讀取客戶端信息
is=socket.getInputStream();//字節(jié)輸入流
os=socket.getOutputStream();
DataOutputStream?dos=new?DataOutputStream(os);
DataInputStream?dis=new?DataInputStream(is);
// InputStreamReader?isr=new?InputStreamReader(is);//將字節(jié)流轉(zhuǎn)化為字符輸入流
// BufferedReader?br=new?BufferedReader(isr);//為輸入流添加緩沖
String?s=null;
s=dis.readUTF();
while((s=dis.readUTF())!=null){
System.out.println("我是服務(wù)器,客戶端說"+s);
System.out.println("IP"+socket.getInetAddress());
System.out.println("port"+socket.getPort());
}
if(socket.isClosed()){
System.out.println("關(guān)閉");
}else{
System.out.println("沒關(guān)閉");
}
//4.獲取輸出流,響應(yīng)客戶端的請(qǐng)求
dos.writeUTF("hello");
dis.close();
socket.close();
}
}?catch?(IOException?e)?{
System.out.println("客戶端異常斷開");
}
}
}小弟跪求大家?guī)蛶兔α耍?
為什么我寫的javaSocket通信 服務(wù)端接收不到?
qq_風(fēng)云_13
2016-05-05 14:46:23