⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptpserver.java

📁 网络变程事例
💻 JAVA
字号:
import java.net.*;
import java.io.*;
public class PtPserver extends Thread
{	
	Thread th;
	ServerSocket server;
	Socket clent;
	public PtPserver(){
		try{	
		/*	File path=new File(".");
			String[] list =path.list();
			
			for(int i=0;i<list.length;i++)System.out.println(list[i]);
		*/
		//	System.out.println("dsfaasd");
				server=new ServerSocket(6666);
		//		clent=server.accept();
			System.out.println("skdljf");
			th=new Thread(this);
				th.start();
		}catch(IOException e){
			       	
			System.out.println("监听端口6666");
			//th.start();
		}
	
	}
	public void run(){
		try{
			System.out.println("线程启动");
		
		while(true){
			System.out.println("监听端口6666");
		ServerListen();
		sleep(10);
		}
	}catch(InterruptedException ex){
		System.out.println(ex.toString());
	}
	}
	public void ServerListen()
	{
		
		try{
			System.out.println("监听端口6666");
		
		 clent=server.accept();
	//	 while(clent!=null)
		System.out.println("连接成功");
		BufferedReader in=new BufferedReader(new InputStreamReader(clent.getInputStream()));
		PrintWriter out=new PrintWriter(clent.getOutputStream(),true);
		File path=new File(".");
		String[] list =path.list();
		//for(int i=0;i<list.length;i++)System.out.println(list[i]);
	//	if(in.readLine()==""){	 
		for(int i=0;i<list.length;i++){
			out.println(list[i]);
			System.out.println(list[i]);
		}
		
		
		out.flush();
	//	}
	/*	else
		{
			String s=in.readLine();
			for(int j=0;j<list.length;j++){
				if(list[j].equals(s))
				{
					this.send(s);
					System.out.println("此文件存在");
					}
			}

		}
	*/	}
		catch(Exception e){
			System.out.println("对不起,此程序存在问题!");
		}
	//	server.close();
	//	clent.close();
	//	in.close();
	//	out.close();
	}
	public void send(String fileName)
	{
		try{
		
		     File file=new File(fileName);
 
              FileInputStream fos=new FileInputStream(file);
              
 
            //    创建网络服务器接受客户请求
 
         	    ServerSocket ss=new ServerSocket(3108);
 
          	    Socket client=ss.accept();
              
 
              //创建网络输出流并提供数据包装器
 
              OutputStream netOut=client.getOutputStream();

 
              OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
					//创建文件读取缓冲区
 
              byte[] buf=new byte[2048];
 
              int num=fos.read(buf);
 
              while(num!=(-1)){//是否读完文件
 
                     doc.write(buf,0,num);//把文件数据写出网络缓冲区
 
                     doc.flush();//刷新缓冲区把数据写往客户端
 
                     num=fos.read(buf);//继续从文件中读取数据
 
              }
            fos.close();
 
            doc.close();
              }catch(Exception e){
              	System.out.println("存在问题!");
           
              }
 
            
 
       }
    /*   public static void main(String[] args)
       {
       	PtPserver p=new PtPserver();
       //	p.ServerListen();

       }
*/
	
}

⌨️ 快捷键说明

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