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)