server端:
ServerSocket ss = null;
Socket socket = null;
try {
ss=new ServerSocket(8888);
System.out.println("server is starting");
socket=ss.accept();
BufferedReader br=new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
System.out.println(br.readLine());
bw.write(br.readLine()+"received");
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
client端:
Socket socket = null;
try {
socket=new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
InputStream is=socket.getInputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
BufferedReader br=new BufferedReader(new InputStreamReader(is));
bw.write("hello");
System.out.println(br.readLine());
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端的数据传不到服务器端,服务器端的数据不能返回到客户端
这样写我这儿也没问题,问题是这个类得readline是过期的啊,我要读一行数据还是不太方便~~~
flush都加过了,还是不行,如果单独在client端写,在server端读,完全没有问题,但是在两端同时写和读就有问题