按照视频TCP那章的前三节写的程序,服务器端接受不了客户端发来的数据?
//服务器端的程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server =new ServerSocket(9887);
while(true)
{
Socket socket =server.accept();
InputStream it =socket.getInputStream();
InputStreamReader its=new InputStreamReader(it);
BufferedReader br=new BufferedReader(its);
System.out.println("我是服务器 ,等待客户端输入数据");
String info =null;
while((info=br.readLine())!=null)
{
System.out.println(info);
info=br.readLine();
}
socket.shutdownInput();
br.close();
its.close();
it.close();
socket.close();
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端的程序
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
try {
Socket socket =new Socket("localhost",9887);
OutputStream os =socket.getOutputStream();
PrintWriter pw=new PrintWriter(os);
pw.write("用户名");
pw.flush();
socket.shutdownOutput();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}