网络编程server

下面中一个例子,用到了多线程.

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;

class ServerOneJabber extends Thread {
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;

	public ServerOneJabber(Socket s) throws IOException {
		socket = s;
		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		// auto flush
		out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
				socket.getOutputStream())), true);
		start();
	}

	public void run() {
		try {
			while (true) {
				String str = in.readLine();
				if (str.equals("END"))
					break;
				System.out.println("Ecoh:" + str);
				out.println(str);
			}
			System.out.println("closing");
			;
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			try {
				socket.close();
			} catch (IOException e2) {
				// TODO: handle exception
			}
		}
	}
}

public class MultiJabberServer {
	static final int PORT = 8080;

	public static void main(String args[]) throws IOException {
		ServerSocket s = new ServerSocket(PORT);
		System.out.println("server start");
		try {
			while (true) {
				Socket socket = s.accept();
				try {
					new ServerOneJabber(socket);
				} catch (IOException e) {
					socket.close();
				}
			}
		} finally {
			s.close();
		}
	}

}

  

然后是客户端

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.InetAddress;
import java.net.Socket;
import java.security.cert.TrustAnchor;


class JabberClientThread extends Thread{
	private final int PORT =8080 ;
	
	private Socket socket ;
	private BufferedReader in ;
	private PrintWriter out ;
	private static int counter =0;
	private int id =counter++ ;
	private static int threadcount =0;
	public static int threadCount (){
		return threadcount ;
	}
	public JabberClientThread (InetAddress addr){
		System.out.println("making client id:"+ id) ;
		threadcount ++  ;
		try {
			socket = new Socket(addr,PORT ) ;
		} catch (IOException e) {
			// TODO: handle exception
		}
		try {
			in = new BufferedReader(
					new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(
							socket.getOutputStream())) , true) ;
		} catch (Exception e) {
			try {
				socket.close() ;
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	public void run (){
		try {
			for (int i =0; i<25;i++){
				out.println("client id:"+i);
				String str = in.readLine() ;
				System.out.println(str) ;
			}
			out.println("END") ;
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			try {
				socket.close() ;
			} catch (IOException e2) {
				threadcount --;//end this thread 
			}
		}
	}
}
public class MultiJabberClient {	
	static  final int MAX_THREAD =40;
	public static void main (String args [])
	throws IOException, InterruptedException{
		InetAddress addr = InetAddress.getByName(null);
		while (true){
			if (JabberClientThread.threadCount()<MAX_THREAD){
				new JabberClientThread(addr) ;
			}
		}
	}
	

}

  

posted @ 2015-03-30 20:09  chuiyuan  阅读(181)  评论(0编辑  收藏  举报