Network programming TCP (VI) __ Programming

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.