Home > Back-end >  Java based network programming problems
Java based network programming problems

Time:09-16

Java network programming the client package could not be started without the main types, clearly I wrote, the server can start
 public static void main (String [] args) throws IOException {
//1. To create local FileInputStream object, read data
FileInputStream fis=new FileInputStream (" E: \ \ Java \ \ test JPG ");
//2. Create a client Socket object, the construction method of binding IP address and port number
The Socket s=new Socket (" 127.0.0.1 ", 8888);
//using methods on the Socket getOutStream, access to the network output byte stream OutputStream object
OutputStream OS=s.g etOutputStream ();
//4. Local FileInputStream the read method is used to read local data
byte[] bytes=new byte[1024];
int len=0;
While ((len=fis. Read (bytes))!=1) {
//5. Using the network output byte stream OutStream way to write, to read files uploaded to the server
OS. Write (bytes, 0, len);
}
//6. Use the method getInputStream Socket, to obtain network InputStream object in the input stream of bytes
InputStream is=s.g etInputStream ();
//7. Using the network byte input stream method of InputStream read read data server to write
While ((len=is. Read (bytes))!=1) {
System. The out. Println (new String (bytes, 0, len));
}
//8. Release resources
S.c lose ();
fis.close();

}


Have bosses have a look at ah, thank you

CodePudding user response:

Emmm, changed after a class, but why not show the server response message
[code=Java]//1. ServeSocket, create a server object and system to port
ServerSocket serve=new ServerSocket (8888);
//2. The use of ServeSocket object the accept () method gets the client object
The Socket Socket=serve. The accept ();
//3. Use the method getInputStream in the Socket, access network InputStream object in the input stream of bytes
InputStream ins=socket. GetInputStream ();
//4. Query whether there is a destination
The File File=new File (" E: \ \ Java \ \ Test ");
if (! File. The exists ()) {
File. The mkdir ();
}
/*
* custom naming conventions
*/
The String filename="\ \ yf" + System. CurrentTimeMillis () + new Random () nextInt + ". JPG "(8888);
//5. Create a local bytes output stream FileOutputStream, bind the output destination
//FileOutputStream fos=new FileOutputStream (file + "\ \ sc. JPG");
FileOutputStream fos=new FileOutputStream (file + filename);
//6. Using the method of InputStream network bytes read, read to upload file
byte[] bytes=new byte[1024];
int len=0;
While ((len=ins. Read (bytes))!=1) {
//7. Using local output stream object FileOutStream way to write, read the harbinger of file to the server hard disk
Fos. Write (0 bytes, len);
}
//8. Use a socket object getOutputStream () method gets to the network output byte stream OutputStream object

//9. Use the Internet OutputStream object, write back to upload success
Socket. GetOutputStream (). The write (" uploaded successfully. "getBytes ());
//10. Close the flow
fos.close();
Socket. The close ();
Serve. The close (); The
Quote:

CodePudding user response:

reference 1st floor qq_41096886 response:
emmm, changed after a class, but why not show the server response message


Server is sending a message to the client, your statement to get information from the client! Totally 2 thing, a good reading a book on how to write, the output of a data flow impossible 2 use, need and input matching,

socket. GetOutputStream (). The write (" uploaded successfully. "getBytes ());
  • Related