Java--socket Communication (bidirectional) __java

Source: Internet
Author: User
Tags readline server port

New two projects, one client, one server, first start the server and then start the client

The read-write operation thread classes for both projects are essentially identical

Service side:

Import Java.io.BufferedReader;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.ServerSocket;

Import Java.net.Socket; public class Server {public static final int port = 8000;//Listener port number public static void main (string[] args) {Ser  
		Ver Server = new server ();  
	Server.init ();
		public void init () {ServerSocket serversocket = null;  
			try {serversocket = new ServerSocket (PORT);  
				while (true) {Socket client = serversocket.accept ();  
				A client connection accounts for two threads to handle read and write new thread (new Readhandlerthread (client)). Start (); 
			New Thread (new Writehandlerthread (client)). Start ();  
		} catch (Exception e) {e.printstacktrace ();
				} finally{try {if (ServerSocket!= null) {serversocket.close ();
			} catch (IOException e) {e.printstacktrace (); }}}/* * thread that handles read operations/class Readhandlerthread implements runnable{private Socket client;
	Public Readhandlerthread (Socket client) {this.client = client;
		@Override public void Run () {datainputstream dis = null;
				try{while (true) {//read client data dis = new DataInputStream (Client.getinputstream ());
				String reciver = Dis.readutf (); 
			SYSTEM.OUT.PRINTLN ("Client sent over the content:" + reciver);
		}}catch (Exception e) {e.printstacktrace ();
				}finally{try {if (dis!= null) {dis.close ();
				} if (client!= null) {client = null;
			} catch (IOException e) {e.printstacktrace ();

	}}/* * The thread that handles the write operation/class Writehandlerthread implements runnable{private Socket client;
	Public Writehandlerthread (Socket client) {this.client = client;
		@Override public void Run () {DataOutputStream dos = null;
		BufferedReader br = null;  
				try{while (true) {//Reply message to client dos = new DataOutputStream (Client.getoutputstream ());  
				System.out.print ("Please enter: \ t"); Keyboard input BR = new BufferedReader (newInputStreamReader (system.in));  
				String send = Br.readline ();  
			Sending data Dos.writeutf (send);
		}}catch (Exception e) {e.printstacktrace ();
				}finally{try {if (Dos!= null) {dos.close ();
				} if (br!= null) {br.close ();
				} if (client!= null) {client = null;
			} catch (IOException e) {e.printstacktrace (); }
		}
	}
}


Client:

Import Java.io.BufferedReader;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Java.net.Socket;   

	public class Client {public static final String IP = "localhost";/server address public static final int port = 8000;//Server port number 
	public static void Main (string[] args) {handler ();
			private static void Handler () {try {//Instantiate a socket and specify server address and port Socket client = new Socket (IP, port);
			Open two threads, one responsible for reading and one responsible for writing new thread (new Readhandlerthread (client)). Start ();
		New Thread (new Writehandlerthread (client)). Start ();
		catch (Exception e) {e.printstacktrace ();

	}}/* * The thread that handles the read operation */class Readhandlerthread implements runnable{private Socket client;
	Public Readhandlerthread (Socket client) {this.client = client;
		@Override public void Run () {datainputstream dis = null;
				try {while (true) {//Read server-side data dis = new DataInputStream (Client.getinputstream ()); STring receive = Dis.readutf ();  
			SYSTEM.OUT.PRINTLN ("The server side returned is:" + receive);
		} catch (IOException e) {e.printstacktrace ();
				} finally{try {if (dis!= null) {dis.close ();
				} if (client!= null) {client = null;
			} catch (IOException e) {e.printstacktrace ();

	}}/* * The thread that handles the write operation/class Writehandlerthread implements runnable{private Socket client;
	Public Writehandlerthread (Socket client) {this.client = client;
		@Override public void Run () {DataOutputStream dos = null;
		BufferedReader br = null;
				try {while (true) {//get output stream dos = new DataOutputStream (Client.getoutputstream ());  
				System.out.print ("Please enter: \ t");
				Keyboard input BR = new BufferedReader (new InputStreamReader (system.in));  
				String send = Br.readline ();  
			Sending data Dos.writeutf (send);
		} catch (IOException e) {e.printstacktrace ();
				} finally{try{if (dos!= null) {dos.close (); } if (br!= null) {
					Br.close ();
				} if (client!= null) {client = null;
			}}catch (Exception e) {e.printstacktrace ();
 }
		}
	}
}


 

Related Article

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.