自定义了一个服务端,用浏览器客户端访问,服务端能够读到客户端发的消息,可是客户端却读不到服务器的消息,话说刚开始是可以读到的,后来不知道为什么读不到了,什么也没改。
package com.itheimaexercise.day24;
import java.io.*;
import java.net.*;
public class ServerTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
try{
serverSocket = new ServerSocket(8977);
}
catch(IOException e){
e.printStackTrace();
}
Socket clientSocket = null;
try{
clientSocket = serverSocket.accept();
}
catch(IOException e){
e.printStackTrace();
}
String ip = clientSocket.getInetAddress().getHostAddress();
System.out.println(ip+"......connected");
//看看浏览器客户端访问服务器的时候都说了什么:
BufferedInputStream bufIn = null;
try{
bufIn = new BufferedInputStream(clientSocket.getInputStream());
}
catch(IOException e){
e.printStackTrace();
}
byte[] buf = new byte[1024];
int len = 0;
try{
while((len=bufIn.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
}
catch(IOException e){
e.printStackTrace();
}
BufferedWriter bufOut = null;
try{
bufOut =
new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
catch(IOException e){
e.printStackTrace();
}
try{
bufOut.write("<font color='red' size=6>欢迎你客户端");
bufOut.newLine();
bufOut.flush();
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
clientSocket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
|
|