1: client code:
@ Test
Public void socketclient () throws ioexception {
Socket S = NULL;
String filepath = "f :\\ yingtao. inw ";
File file = new file (filepath );
S = new socket ("127.0.0.1", 10000 );
Datainputstream FD = new datainputstream (New bufferedinputstream (New fileinputstream (File )));
Dataoutputstream PS = new dataoutputstream (S. getoutputstream ());
Try {
Long filesize = file. Length ();
System. Out. println ("filesize --->" + filesize );
String filename = file. getname ();
// Send the file name and length to the receiver
PS. writeutf (filename );
PS. Flush ();
// Send the file length to the receiver
PS. writelong (filesize );
PS. Flush ();
Byte [] Buf = new byte [1, 1024];
While (true ){
Int size = 0;
Int Len = FCM. Read (BUF );
Size + = Len;
If (LEN =-1 ){
Break;
}
PS. Write (BUF, 0, Len );
}
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
} Finally {
Try {
If (PS! = NULL ){
PS. Flush ();
PS. Close ();
}
If (FS! = NULL)
FCM. Close ();
If (s! = NULL)
S. Close ();
} Catch (exception E2 ){
// Todo: handle exception
}
}
}
2: receiver code.
@ Test
Public void socket_server (){
Serversocket Ss = NULL;
Try {
Ss = new serversocket (10000 );
System. Out. println ("waiting for client connection! ");
Int I = 1;
While (true ){
Socket S = ss. Accept ();
System. Out. println ("the" + I + "client is connected! ");
Acceptfilethread at = new acceptfilethread (S, I );
At. Start ();
I ++;
}
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
}
}
Class acceptfilethread extends thread {
Private socket S;
Private int filenumber;
Public acceptfilethread (socket S, int filenumber ){
This. S = s;
This. filenumber = filenumber;
}
@ Override
Public void run (){
// Todo auto-generated method stub
Datainputstream in = NULL;
Dataoutputstream out = NULL;
Try {
In = new datainputstream (New bufferedinputstream (S. getinputstream ()));
System. Out. println ("no." + filenumber + "file start upload ");
String filename = in. readutf ();
System. Out. println ("file name ---->" + filename );
Long filesize = in. readlong ();
System. Out. println ("file length --->" + filesize );
System. Out. println ("start to accept files! ");
Stringbuilder builder = new stringbuilder ();
Builder. append ("F: \ server_socket \"). append (filename );
File SaveFile = new file (builder. tostring ());
Out = new dataoutputstream (New bufferedoutputstream (New fileoutputstream (SaveFile )));
Byte [] buff = new byte [1024];
Int size = 0;
While (true ){
Int Len = in. Read (buff, 0,1024 );
System. Out. println ("Len --->" + Len );
If (LEN =-1 ){
Break;
}
Out. Write (buff, 0, Len );
Size + = Len;
System. Out. println ("size ---->" + size );
System. Out. println ("file accepted" + (size * 100/filesize) + "% ");
}
System. Out. println ("file" + filenumber + "accepted! ");
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
} Finally {
Try {
If (OUT! = NULL ){
Out. Flush ();
Out. Close ();
}
If (in! = NULL ){
In. Close ();
}
} Catch (exception E2 ){
// Todo: handle exception
E2.printstacktrace ();
}
}
Super. Run ();
}
}