package temc03;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
public class Demo2Socket {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(2222);// 服务端
Socket socket = ss.accept();// 接受客户端的套接字
FileOutputStream o = new FileOutputStream("G:\\2"+System.currentTimeMillis()+".jpg");
InputStream i=socket.getInputStream();
byte[] b = new byte[1024];
int len;
while ((len = i.read(b)) != -1) {
// 输出流
o.write(b, 0, len);
System.out.println(Arrays.toString(b));
}
o.close();
// 告知客户端接受完毕
OutputStream i2 = socket.getOutputStream();
i2.write("接受完毕".getBytes());
ss.close();
}
}
package temc03;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class DomeSocket {
public static void main(String[] args) throws IOException, IOException {
// 客户端
Socket s = new Socket("127.0.0.1", 2222);
OutputStream socketoutputstream = s.getOutputStream();
FileInputStream is = new FileInputStream("G:\\1.jpg");// 输入流
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
// 输出流
socketoutputstream.write(b, 0, len);
socketoutputstream.flush();
}
is.close();
// 告知服务器传送完毕
s.shutdownOutput();
// 接受服务器相应
InputStream i2 = s.getInputStream();
int leng;
byte[] b1 = new byte[1024];
while ((leng = i2.read(b1)) != -1) {
// 输出流+sys
System.out.println("==========");
System.out.println(new String(b1, 0, leng));
}
i2.close();
}
}
==========
接受完毕
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.net.SocketInputStream.read(SocketInputStream.java:108)
at temc03.DomeSocket.main(DomeSocket.java:30)
打印结果老是出错,怎么解决?请指教
|