1. 程式人生 > >JAVA 百萬級的資料併發SOCKET程式設計(已經通過測試)

JAVA 百萬級的資料併發SOCKET程式設計(已經通過測試)

//伺服器端

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;
public class TalkServer {
	public static void main(String[] args) throws IOException{
		ServerSocket server = new ServerSocket(5203);
		while(true){
			Socket socket = server.accept();
			exSocketServer(socket);
		}
	}
	public static void exSocketServer(final Socket socket){		
		new Thread(new Runnable() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				BufferedReader in = null; 
				PrintWriter out = null; 
				try {
					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					out=new PrintWriter(socket.getOutputStream());
					while(true){
						String msg =in.readLine();
						if(msg == null){
							break;
						}
						System.out.println("Client :"+msg);
						//返回訊息給客戶端。
						out.println("OK !");
						out.flush();					
					}
				}catch(Exception e){
					//e.printStackTrace();
					System.out.println("Error:"+e);
				}finally {
					try {
						in.close();
						out.close();
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
											
			}
		}).start();
	}
}

//客戶端A

import java.io.*;
import java.net.*;
public class TalkClient {
			public static void main(String args[]) {
				String rootPath=TalkClient.class.getResource("/").getFile().toString();  
		    	String fileName =rootPath+"Initialize.txt";
				
					String file = "my name is YI! nice to meet you";//ReadFromFile.readFileByLinesln(fileName);
					exSocket(file);
		}
			
			public static void exSocket(String file){
				try{
					InetAddress addr = InetAddress.getByName("localhost");   
					Socket socket=new Socket(addr,5203);
					BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
					PrintWriter out=new PrintWriter(socket.getOutputStream());
					for(int i =0 ;i<1000000;i++){
	                                String readline= file+i+"AAAA" ;
					//將從系統標準輸入讀入的字串輸出到Server
					out.println(readline);
					//重新整理輸出流,使Server馬上收到該字串
					out.flush();
					System.out.println("Client:"+readline);
					
					
					System.out.println("Servlet :"+in.readLine());
					}
					socket.close(); //關閉Socket
					
					
					}catch(Exception e) {
					
					System.out.println("Error"+e); //出錯則打印出錯資訊					
					}
				
			}

}

//客戶端B

import java.io.*;
import java.net.*;
public class TalkClient {
			public static void main(String args[]) {
				String rootPath=TalkClient.class.getResource("/").getFile().toString();  
		    	String fileName =rootPath+"Initialize.txt";
				
					String file = "my name is YI! nice to meet you";//ReadFromFile.readFileByLinesln(fileName);
					exSocket(file);
		}
			
			public static void exSocket(String file){
				try{
					InetAddress addr = InetAddress.getByName("localhost");   
					Socket socket=new Socket(addr,5203);
					BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
					PrintWriter out=new PrintWriter(socket.getOutputStream());
					for(int i =0 ;i<1000000;i++){
					String readline= file+i+"BBBB" ;;  //sin.readLine(); //從系統標準輸入讀入一字串
					//將從系統標準輸入讀入的字串輸出到Server
					out.println(readline);
					//重新整理輸出流,使Server馬上收到該字串
					out.flush();
					System.out.println("Client:"+readline);										
					System.out.println("Servlet :"+in.readLine());
					}
					socket.close(); //關閉Socket									
					}catch(Exception e) {
					
					System.out.println("Error"+e); //出錯則打印出錯資訊
					}				
			}

}