Network programming TCP (five) __ programming

Source: Internet
Author: User
Supplemental: Network programming TCP (II)
Package com;
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 java.net.UnknownHostException;

Import Org.junit.Test;
		public class TESTTCP1 {//client @Test the public void client () {Socket socket=null;
		OutputStream Os=null;
			try {socket = new socket (Inetaddress.getbyname ("127.0.0.1"), 9000);
			OS = Socket.getoutputstream ();
			Os.write ("I am customer number 1th, please take care". GetBytes ());//outward output data} catch (Unknownhostexception e) {//TODO auto-generated catch block
		E.printstacktrace ();
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				}finally{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 ();
		}}//service-side @Test public void server () {ServerSocket server=null;
		Socket Socket=null;
		InputStream Is=null;
			try {server=new ServerSocket (9000);
			Socket=server.accept ()//accept get the Client socket object Is=socket.getinputstream ();//write data to the program byte[] B=new byte[20];
			int Len;
				while ((Len=is.read (b))!=-1) {string Str=new string (B,0,len);
			System.out.println (str);
		} System.out.println ("Lad: Received from" +socket.getlocaladdress ());
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				} finally{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 ();
 }
			}
		}
	}
}
The
example is only sent by the client, and the server receives it. The following example is server and client interaction. There are questions and answers.
Package com;
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 java.net.UnknownHostException;


Import Org.junit.Test; /** * Case: The client sends a message to the server, and the server prints the information. Send "received message" to client * @author Administrator */public class TESTTCP2 {//client @Test public void client () {Socket soc
		Ket=null;
		OutputStream Os=null;
		InputStream Is=null;
			try {socket = new socket (Inetaddress.getbyname ("127.0.0.1"), 9000);
			OS = Socket.getoutputstream (); Os.write ("Wo shi Kehu 2". GetBytes ())//outward output data for the server to read Socket.shutdownoutput (),//display notification, service end "sent out."
			", Is=socket.getinputstream ();
			Byte[] B=new byte[50];
			int Len;
				while ((Len=is.read (b))!=-1) {string Str=new string (B,0,len);
			System.out.println (str);
		The catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrace (); catch (IOException e) {//TODO Auto-geNerated Catch block E.printstacktrace ();
				}finally{if (is!=null) {try {is.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 ();
		}}//service-side @Test public void server () {ServerSocket server=null;
		Socket Socket=null;
		InputStream Is=null;
		OutputStream Os=null;
			try {server=new ServerSocket (9000); Socket=server.accept ()//accept get the Client socket Object Is=socket.getinputstream ()//Get the client socket data to the program to write data byte[b=new
			BYTE[20];
			int Len;
				while ((Len=is.read (b))!=-1) {string Str=new string (B,0,len);
			System.out.println ("Fuwqi: Received:" +socket.getlocaladdress () +str); //Then the server said that II also give you feedback os=socket.getoutputstream ();
			
		Os.write ("Shou Dao ni de xinxile, Yihouhuihaohao guanzhao ni de". getBytes ());
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
				} finally{if (os!=null) {try {os.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 ();
 }
			}
		}
	}
}


 

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.