The sends files from the client to the server, and the server saves the files locally. and return "Save success" to the client. and close the corresponding connection.
Package com;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.net.InetAddress;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Org.junit.Test; Sends files from the client to the server, and the server saves the files locally. and return "Save success" to the client.
and close the appropriate connection public class TESTTCP3 {@Test the public void client () {Socket socket=null;
OutputStream Os=null;
FileInputStream Fis=null;
InputStream Is=null;
Try {//1 creates a socket object Socket=new socket (inetaddress.getbyname ("127.0.0.1"), 8989);
2 Get local picture Os=socket.getoutputstream ();
Fis=new FileInputStream (New File ("IP mark. png"));
Byte[]b=new byte[1024];
int Len;
while ((Len=fis.read (b))!=-1) {os.write (B,0,len); The//5 display tells the service side.
I'm done.
Socket.shutdownoutput ();
3 Receiving service-side information is=socket.getinputstream ();
Byte[] B1=new byte[1024];
int len1;
while ((Len1=is.read (B1))!=-1) {string Str=new string (B1,0,LEN1); System.ouT.println (str);
} catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
}finally{//4 Close if (is!=null) {try {is.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Fis!=null) {try {fis.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Os!=null) {try {os.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Socket!=null) {try {socket.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
@Test public void Server () {ServerSocket server=null;
Socket Socket=null;
InputStream Is=null;
FileOutputStream Fos=null;
OutputStream Os=null;
try {//define port number for client communication server=new ServerSocket (8989); Call the Accept method to get a socket object soCket=server.accept ();
Save the data sent from the client to the local is=socket.getinputstream ();
Fos=new FileOutputStream (New File ("2.png"));
Byte[] B=new byte[1024];
int Len;
while ((Len=is.read (b))!=-1) {fos.write (B,0,len);
//Feedback client os=socket.getoutputstream (); Os.write ("Nifasongdetupianwojieshoudaole.")
". GetBytes ());
catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
}finally{//Close if (os!=null) {try {os.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Fos!=null) {try {fos.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Is!=null) {try {is.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} if (Socket!=null) {try {socket.close (); catch (IOException e) {//TODO auto-generated catch BloCK E.printstacktrace ();
} if (Server!=null) {try {server.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
}
}
TCP ends here .... After that is the UDP communication protocol.