s = new Socket("192.168.1.102", 11000);// 这个地方报java.net.ConnectException ,我是先开启服务端了的
System.out.println(s.getInetAddress());
FileInputStream fins = new FileInputStream("F://1.txt");
OutputStream ous = s.getOutputStream();
byte[] buf = new byte[1024];
int len = -1;
System.out.println("haha");
while((len=fins.read(buf))!=-1){
ous.write(buf, 0, len);
}
//打一个结束标记
s.shutdownOutput();
InputStream ins = s.getInputStream();
byte[] buf_2 = new byte[1024];
int num = ins.read(buf_2);
System.out.println(new String(buf_2, num));
s.close();
fins.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new UpLoadDemo();
}
}
//服务端
class SocketServer{
public SocketServer(){
try {
ServerSocket ss = new ServerSocket(11000);
Socket s =ss.accept();
InputStream ins = s.getInputStream();
FileOutputStream fos = new FileOutputStream("F://copy.txt");
byte[] buf = new byte[1024];
int len ;
while((len =ins.read(buf))!=-1){
fos.write(buf, 0, len);
}