我正在尝试用 Java 创建一个客户端-服务器应用程序。此应用程序将图像从客户端传输到服务器。要求是应用程序需要按网络字节顺序排列。(下图)
这将帮助我学习如何在未来部署其他协议,因此寻找一些如何实现这一点的技巧。目前我的服务器-客户端正在工作和传输图像,但不确定如何实现该协议。
谢谢
这是我当前的服务器/客户端代码:
公共类网络服务器 {
public static void main(String[] args) { NetworkServer servidor = new NetworkServer(); System.out.println("Server started()"); BufferedInputStream bis; BufferedOutputStream bos; int num; File file = new File("/images"); if (!(file.exists())){ file.mkdir(); } try { ServerSocket socket = new ServerSocket(15000); Socket incoming = socket.accept(); try { try{ if (!(file.exists())){ file.mkdir(); } InputStream inStream = incoming.getInputStream(); OutputStream outStream = incoming.getOutputStream(); BufferedReader inm = new BufferedReader(new InputStreamReader(inStream)); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); String filelength = inm.readLine(); String filename = inm.readLine(); System.out.println("Server() Filename = " + filename); System.out.println("Server() File lenght: " + filelength + " bytes"); System.out.println("Server() ACK: Filename received = " + filename); //RECIEVE and WRITE FILE byte[] receivedData = new byte[1000000]; bis = new BufferedInputStream (incoming.getInputStream()); bos = new BufferedOutputStream (new FileOutputStream("/images" + "/" + "image.jpg")); while ( (num = bis.read(receivedData)) > 0){ bos.write(receivedData,0,num); } bos.close(); bis.close(); File receivedFile = new File(filename); long receivedLen = receivedFile.length(); System.out.println("Server() ACK: Length of received file = " + receivedLen); } finally { incoming.close(); } } catch (IOException e){ e.printStackTrace(); } } catch (IOException e1){ e1.printStackTrace(); } } }