Simply Implementing Communication By Muti-Thread From Socket

 <1>:Socket should always be instant by whichever both Server and Client
 <2>:Set conllection by ServerSocket.
 <3>:Getting data from Client and Responsing 
 <4>:Shut down system resource(include Stream and Socket)

//Server:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MServer {
    public static void main(String[] args){
	try {
	
	    int count=0;//Counting the total number of Client
		
	    @SuppressWarnings("resource")
	    //Setting ServerSocket's instance and signing the Port【>1024】
	    ServerSocket serverSocket=new ServerSocket(5545);
	    System.out.println("[SERVER]Server is Loading...\n");//
			
	    while(true){
	    //Tasking the accept()Method to get instance of Socket.
		Socket clientSocket=serverSocket.accept();
		    MThread mThread=new MThread(clientSocket);
		    mThread.start();
		    count++;
		    System.out.println("[SERVER]Current total count of Client: "+count);
			}
	//while() will run for ever until shuting down by yourself	
			//clientSocket.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	}
}

//Client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MClient {
	public static void main(String[] args){
		try {
       //Setting Socket and OutputStream for sending requsting infformation to Server
		//******************************************
		Socket clientSocket=new Socket("localhost",5545);
		OutputStream outputStream=clientSocket.getOutputStream();
		PrintWriter printWriter=new PrintWriter(outputStream);
		printWriter.write("[CLIENT]UserName:WXM Password:123");
		printWriter.flush();
		clientSocket.shutdownOutput();
		//******************************************
			
			
	//Setting InputStream for getting responsing from Server
		//********************************
		InputStream inputStream=clientSocket.getInputStream();
		BufferedReader bufferedReader=
		    new BufferedReader(new InputStreamReader(inputStream));
		String information=null;
		while((information=bufferedReader.readLine())!=null)
			{System.out.println("[CLIENT]Client is Logining.....\n"+
			information);}
		//********************************
			
			
		//Shutting down the resource when Finishing all of communicatin 
		//**************************************
		bufferedReader.close();
		inputStream.close();
		printWriter.close();
		outputStream.close();
		//*************************************
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    //We have to deal with the Exception from Input/Output-Stream
    //It is appropriate that Throwing the Exception by Throws and use try-catch-finally 
	}

}


//Thread

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MThread extends Thread{
	Socket clientSocket=null;
	public MThread(Socket clientSocket){
		this.clientSocket=clientSocket;
	}
	public void run(){
		
		InputStream input = null;
		try {
			input = clientSocket.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStreamReader inputStreamReader=new InputStreamReader(input);
		BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
		String information=null;
		try {
			while((information=bufferedReader.readLine())!=null)
				{System.out.println("[SERVER]Server is waiting and loading.....\n"+information);}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			clientSocket.shutdownInput();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//************************************
		
		
		//Setting a InputStream to response the request from Client
		//************************************
		OutputStream outputStream = null;
		try {
			outputStream = clientSocket.getOutputStream();
		} catch (IOException e) {

			e.printStackTrace();
		}
		PrintWriter  printWriter=new PrintWriter(outputStream);
		printWriter.write("[SERVER]Logined yet:");
		
		printWriter.flush();
		printWriter.close();
		try {
			if(outputStream!=null)
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if(bufferedReader!=null)
			bufferedReader.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
		try {
			if(inputStreamReader!=null)
			inputStreamReader.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
		try {
			if(input!=null)
			input.close();
		} catch (IOException e) {
	
			e.printStackTrace();
		}
		try {
			if(clientSocket!=null)
			clientSocket.close();
		} catch (IOException e) {
	
			e.printStackTrace();
		}
	}
	
}

/*
//Server
[CLIENT]Client is Logining.....
[SERVER]Logined yet:

//Client
[SERVER]Server is Loading...
[Client]IP Address of Current Client:127.0.0.1
[SERVER]Current total count of Client: 1
[SERVER]Server is waiting and loading.....
[CLIENT]UserName:WXM Password:123
[Client]IP Address of Current Client:127.0.0.1
[SERVER]Current total count of Client: 2
[SERVER]Server is waiting and loading.....
[CLIENT]UserName:WDH Password:456
*/

    

    TCP (Transmission Control Protocol)provides reliable connection, ordered, and error-checked delivery of a stream of octesed between applications running on hosts communicating over an IP network. TCP is the protocol that major Internet applications such as WWW ,Email and so on. 

    UDP(User datagram Protocol) provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram.

    Coding for TCP&UDP with Muti-Thread by Socket is so significant and you can get your programming for chating or others .

   Thx , Congratulations! 


你可能感兴趣的:(java,thread,socket)