import java.io.*;
import java.net.*;
class PicClien
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.2.71",10009);
FileInputStream fis=new FileInputStream("d:\\2.jpg");
OutputStream os=s.getOutputStream();//从套接字获取输出流
byte[] by=new byte[1024];
int len=0;
while ((len=fis.read(by))!=-1)
{
os.write(by,0,len);
}
s.shutdownOutput();
InputStream is=s.getInputStream();//从套接字获取输入流
byte[] bys=new byte[1024];//创建一个缓冲区
int lens=is.read(bys);
System.out.println(new String(bys,0,lens));
s.close();
fis.close();
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10009);
Socket s=ss.accept();//获取所有接收到的 数据源
InputStream is=s.getInputStream();//从套接字获取输入流
FileOutputStream fos=new FileOutputStream("picserver.jpg");
byte[] by=new byte[1024];
int len=0;
while ((len=is.read(by))!=-1)
{
fos.write(by,0,len);
}
OutputStream out =s.getOutputStream();
out.write("上传成功".getBytes());
ss.close();
s.close();
fos.close();
}
}
|