服务器端的反馈
我们上一节是客户端将图片发送给服务器,之后服务端将图片保存在本地。
接下来拓展一个问题,当服务器端接收到图片时,客户端不知道服务器端收到没,
这时候我们将上一节的代码进行拓展,让服务器端发送反馈给客户端。
客户端代码:
@Test
public void Client(){
FileInputStream fis= null;
OutputStream os= null;
InputStream is= null;
ByteArrayOutputStream baos= null;
try {
//指明端口号和ip
InetAddress ia=InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(ia,4567);
//流
fis = new FileInputStream(new File("123.jpg"));
os = socket.getOutputStream();
int len;
byte[] buf=new byte[1024];
while((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
//关闭流的输出
socket.shutdownOutput();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len2;
if((len2=is.read(buffer))!=-1){
baos.write(buffer,0,len2);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端代码:
@Test
public void Server(){
ServerSocket serverSocket = null;
Socket socket= null;
InputStream is= null;
FileOutputStream fos= null;
OutputStream os= null;
try {
//端口号
serverSocket = new ServerSocket(4567);
//等待连接
socket = serverSocket.accept();
//流的接收
is = socket.getInputStream();
fos = new FileOutputStream("789.jpg");
int len;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
os = socket.getOutputStream();
os.write("服务器已收到".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
为了方便阅读,附上没有try-catch的代码:
客户端:
@Test
public void Client() throws IOException {
//指明端口号和ip
InetAddress ia=InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(ia,4567);
//流
FileInputStream fis = new FileInputStream(new File("123.jpg"));
OutputStream os = socket.getOutputStream();
int len;
byte[] buf=new byte[1024];
while((len=fis.read(buf))!=-1){
os.write(buf,0,len);
}
//关闭流的输出
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len2;
if((len2=is.read(buffer))!=-1){
baos.write(buffer,0,len2);
}
System.out.println(baos.toString());
fis.close();
fis.close();
os.close();
baos.close();
is.close();
}
服务器端:
@Test
public void Server() throws IOException {
//端口号
ServerSocket serverSocket = new ServerSocket(4567);
//等待连接
Socket socket=serverSocket.accept();
//流的接收
InputStream is=socket.getInputStream();
FileOutputStream fos=new FileOutputStream("789.jpg");
int len;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
OutputStream os=socket.getOutputStream();
os.write("服务器已收到".getBytes());
serverSocket.close();
socket.close();
is.close();
fos.close();
os.close();
}
这里有以关键代码(客户端代码):
关闭流的输出语句:
socket.shutdownOutput();//关闭流的输出
首先要了解.read()这个方法,它是一个阻塞式的方法。
什么是阻塞式的方法:没有明确告诉什么时候结束,它就不会结束。
如果我们没有加入socket.shutdownOutput(),
那就会进入一个环节:客户端发送图片;服务端接收;服务端不知道客户端哪时候发完,就一直转,不会停止,也不会对客户端进行反馈,客户端也会一直转。
!!!所以客户端发送完图片后,加入关闭流输出语句,告诉服务端,我(客户端,发送完了);这时候服务器端就会停止传输,并且进行反馈。