码迷,mamicode.com
首页 > 其他好文 > 详细

网络编程server

时间:2015-03-30 21:03:47      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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

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) ;
			}
		}
	}
	

}

  

网络编程server

标签:

原文地址:http://www.cnblogs.com/chuiyuan/p/4379031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!