net1.java

来自「简单清晰具有启发性的实例。 Net1和Client1类实现了TCP的简单应用。」· Java 代码 · 共 64 行

JAVA
64
字号
import java.net.*;
import java.io.*;
class Server1 extends Thread{
	    private Socket sock;
			private BufferedReader in;
			private PrintWriter out;
	Server1(Socket s){
			sock = s;
			System.out.println("Connection accepted: "+s);
		try{
			
				 in = new BufferedReader(
																new InputStreamReader(
																		s.getInputStream()));
				 out =
								new PrintWriter(
										new BufferedWriter(
												new OutputStreamWriter(
														s.getOutputStream())),true);
															
			start();
		}catch(IOException e){
				
			}
		}
	 public void run(){
	 	 try{
	 		while(true){
				String str = in.readLine();
				if(str.equals("end")) break;
				System.out.println("Echoing: "+str);
				out.println(str);
			}
		}catch(IOException e){
			
		 }finally{
		 	try{
		 		sock.close();
		 	}catch(IOException e){
		 			e.printStackTrace();
		 		}
		 }
	 	}
}
class Net1{
	public static final int PORT = 8888;
	public static void main(String[] args){
		try{
			InetAddress a = InetAddress.getByName("ms-edb97d663777");
			//System.out.println(a);
			ServerSocket s = new ServerSocket(PORT);
			System.out.println("Start:"+s);
			while(true){
			Socket socket1 = s.accept();
			new Server1(socket1);
		}
	}catch(Exception e){
		
		}
	finally{
			System.out.println("closing...");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?