Use the TCP client ---- & gt; server to transfer files to the specified path and return a friendly feedback, tcp ----

Source: Internet
Author: User

Use the TCP client ----> server to transfer the file to the specified path and return a friendly feedback, tcp ----

First, a method for blind writing to transfer files, but the test found a very difficult problem, here first describes.

The error code is as follows:

1 package com. TCP. java; 2 3 import java. io. file; 4 import java. io. fileInputStream; 5 import java. io. fileNotFoundException; 6 import java. io. fileOutputStream; 7 import java. io. IOException; 8 import java. io. inputStream; 9 import java. io. outputStream; 10 import java.net. inetAddress; 11 import java.net. serverSocket; 12 import java.net. socket; 13 14 import org. junit. test; 15 16 // send a file from the client to the server, which is saved locally. And return "sent successfully" to the client. And close the connection. 17 public class TestTCP3 {18 // client 19 @ Test 20 public void client () {21 Socket socket = null; 22 OutputStream OS = null; 23 FileInputStream FCM = null; 24 InputStream is = null; 25 try {26 // create a Socket object 27 socket = new Socket (InetAddress. getByName ("192.168.1.101"), 9090); 28 // obtain a local file and send it to the server 29 OS = socket. getOutputStream (); 30 FD = new FileInputStream (new File ("findLei.jpg"); 31 byte [] B = New byte [1024]; 32 int length; 33 while (length = Fi. read (B ))! =-1) {34 OS. write (B, 0, length); 35} 36 socket. shutdownOutput (); 37 // receives information from the server 38 is = socket. getInputStream (); 39 byte [] b1 = new byte [1024]; 40 int length1; 41 while (length1 = is. read (b1 ))! =-1) {42 String str = new String (b1, 0, length1); 43 System. out. println (str); 44} 45} catch (IOException e) {46 e. printStackTrace (); 47} 48 finally {49 // close the corresponding stream and Socket object 50 if (is! = Null) {51 try {52 is. close (); 53} catch (IOException e) {54 e. printStackTrace (); 55} 56} 57 if (FS! = Null) {58 try {59 FCM. close (); 60} catch (IOException e) {61 e. printStackTrace (); 62} 63} 64 if (OS! = Null) {65 try {66 OS. close (); 67} catch (IOException e) {68 e. printStackTrace (); 69} 70} 71 if (socket! = Null) {72 try {73 socket. close (); 74} catch (IOException e) {75 e. printStackTrace (); 76} 77} 78} 79} 80 81 // server 82 @ Test 83 public void server () {84 ServerSocket ss = null; 85 Socket s = null; 86 InputStream is = null; 87 FileOutputStream fos = null; 88 OutputStream OS = null; 89 try {90 // create a ServerSocket object 91 ss = new ServerSocket (9090 ); 92 // call the accept () method and return a Socket object 93 s = ss. accept (); 94 // Save the information sent from the client to the local 95 is = s. getInputStream (); 96 fos = new FileOutputStream ("D:/Test"); 97 // Random random = new Random (); 98 // fos = new FileOutputStream ("D: /Test/"+ String. valueOf (random. nextInt (90) + ". jpg "); 99 // fos = new FileOutputStream (" find2.jpg "); 100 byte [] B = new byte [1024]; 101 int length; 102 while (length = is. read (B ))! =-1) {103 fos. write (B, 0, length); 104} 105 // message sent and received 106 OS = s. getOutputStream (); 107 OS. write ("received successfully ". getBytes (); 108} catch (FileNotFoundException e) {109 e. printStackTrace (); 110} catch (IOException e) {111 e. printStackTrace (); 112} 113 finally {114 // close the corresponding stream and ServerSocket, Socket object 115 if (OS! = Null) {116 try {117 OS. close (); 118} catch (IOException e) {119 e. printStackTrace (); 120} 121} 122 if (fos! = Null) {123 try {124 fos. close (); 125} catch (IOException e) {126 e. printStackTrace (); 127} 128} 129 if (is! = Null) {130 try {131 is. close (); 132} catch (IOException e) {133 e. printStackTrace (); 134} 135} 136 if (s! = Null) {137 try {138 s. close (); 139} catch (IOException e) {140 e. printStackTrace (); 141} 142} 143 if (ss! = Null) {144 try {145 ss. close (); 146} catch (IOException e) {147 e. printStackTrace (); 148} 149} 150} 151} 152}


Have you seen the problem? No, no, no problem at all, but an error is reported during execution, and the error location is really a headache, I haven't found it for a long time ......

The following is the Error message.

 1 java.net.SocketException: Software caused connection abort: socket write error 2     at java.net.SocketOutputStream.socketWrite0(Native Method) 3     at java.net.SocketOutputStream.socketWrite(Unknown Source) 4     at java.net.SocketOutputStream.write(Unknown Source) 5     at com.TCP.java.TestTCP3.client(TestTCP3.java:35) 6     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 7     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 8     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 9     at java.lang.reflect.Method.invoke(Unknown Source)10     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)11     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)12     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)13     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)14     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)15     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)16     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)17     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)18     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)19     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)20     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)21     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)22     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)23     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)24     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)25     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)26     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)27     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)28     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


The specified location is a 35-row write problem, but I haven't found the cause for a long time. I 'd also like to thank Yao for helping me find this problem.

First, let's explain what this Error means, which is probably because the Socket fails during writing. The process of investigating this code is not described in detail. Let's look at the real cause, the real cause is that there is a problem with the folder directory to be written to on the server. I wrote fos = new FileOutputStream ("D:/Test "); this path has a problem when writing files directly. During copying, you must specify the format and name of the path, which obviously lacks the two, if you change the file to a relative path, it will not be so troublesome. fos = new FileOutputStream ("find2.jpg"); this will make the execution successful, but if we want to put the file under the specified directory, this method can be used

Random random = new Random ();

Fos = new FileOutputStream ("D:/Test/" + String. valueOf (random. nextInt (90) + ". jpg ")

In this way, you can automatically add a random number to avoid the risk of renaming. The problem is solved, and you only need to open comments to the successful code.

 

The following describes the operations and functions required for TCP transmission.

1. Select the server and start it with JUnit. When transferring files, the service must be enabled first. This is common sense.

2. Select the client and start it with JUnit. Here there are two parameters that need to be manually modified. One is the IP address, that is, the IP address of the computer you want to transmit to (this computer wants to start the server); the other is the port number, you can name it by yourself. You also need a port number (Tomcat is 8080) Like Tomcat ).

3. Successfully copied the file and printed friendly feedback

 

* 4. The next day I tested on two machines on the same LAN, but it was difficult. I have not found the reason. I hope to give some suggestions to this blog ~

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.