import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("192.168.1.101",10011);
FileInputStream fis = new FileInputStream("C:\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
//告诉服务端数据已经写完。
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/*
服务端
*/
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10011);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"::::");
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("2.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1);
{
fos.write(buf,0,len);
System.out.println(len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
编译时没有问题,但就在运行的时候出现Collection reset 异常 ,这个我还没见过呢 |