自己写的一个IO流和网络编程相结合的小程序

来源:互联网 发布:mysql pdf 编辑:程序博客网 时间:2024/05/29 09:27
package netserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

import javax.xml.crypto.Data;

public class NetServer {

    ServerSocket ss;
    Socket s;
    BufferedReader br;
    String info;
    String usernames[]={"123","456","789"};
    PrintWriter pw=null;
    File[] mails=null;
    Map<Integer, String> mails2=null;
    
    public static void main(String[] args) {
        NetServer netServer =new NetServer();
        netServer.test();
    }
    
    public NetServer()
    {
        try {
            ss=new ServerSocket(9999);
            System.out.println("我在9999端口监听");
            
            s=ss.accept();
            System.out.println("有人连接到服务器");
            
            //返回给客户端的信息
            pw=new PrintWriter(s.getOutputStream(), true);
//            ObjectInputStream ois =new ObjectInputStream(s.getInputStream());
//            while(true){
//                Massage mag=(Massage) ois.readObject();
//                System.out.println(mag.getMag());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void receive()
    {
        try {
            //接受客户端发来的信息
            InputStreamReader isr=new InputStreamReader(s.getInputStream());
            br=new BufferedReader(isr);
            
            
            info=br.readLine();
            System.out.println(info);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //验证用户
    public boolean checkUser()
    {
        boolean b=false;
        pw.println("输入用户名");
        receive();
        
        int a=info.indexOf(" ");
        String username =info.substring(a+1);
        
        for(int i=0;i<usernames.length;i++)
        {
            if(username.trim().equals(usernames[i]))
            {
//                System.out.println(username);
                pw.println("输入密码");
                receive();
                int p=info.indexOf(" ");
                String password =info.substring(p+1);
                
                    if(password.equals("123"))
                    {
//                        System.out.println("你输入的密码是:"+password);
                        b=true;
                    }else{
                        pw.println("输入用户名密码错误");
                    }
            }
        }
//        System.out.println(b);
        return b;
    }
    
    
    public void test()
    {
        if(checkUser())
        {
            pw.println("进入系统");
            
            while(true){
                pw.println("输入你要进行的操作");
                receive();
                if(info.trim().equals("write"))
                {
                    this.write();
                }else if(info.trim().equals("list")){
                    this.list();
                }else if(info.trim().equals("read")){
                    this.read();
                }else if(info.trim().equals("remove")){
                    this.remove();
                }else if(info.trim().equals("tuichu"))
                {
                    pw.println("退出系统,断开连接");
                    break;
                }
                else{
                    pw.println("输入错误,重新输入");
                }
            }
        }
    }
    //删除邮件(这里的删除只是把map中的键值删掉了,源文件没删掉)
    private void remove() {
        pw.println("输入你要删除第几封邮件\r\n===提示:这里的删只是把map中的键值删掉了,源文件没删掉===");
        receive();
        String mailname=mails2.get(Integer.parseInt(info));
        mails2.remove(Integer.parseInt(info));
//        System.out.println("要删除的文件名"+mailname);
        pw.println("是否确定删除");
        receive();
        if(info.equals("yes")){
            File f=new File(".\\mail\\"+mailname);
            if(f.delete()){
                pw.println("源文件已删除");
                this.list();
            }
        }else{
            return;
        }
    }

    //写入
    public void write()
    {
        pw.println("输入你要存储的字段");
        receive();
        FileOutputStream fos=null;
        Long date=new Date().getTime();
//                System.out.println(date);
        
        File f=new File(".\\mail");
        if(!f.isDirectory()) f.mkdirs();
        try {
            fos=new FileOutputStream(".\\mail\\"+date+".txt");
            fos.write(info.getBytes());
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //list
    public void list()
    {
        File file=new File(".\\mail");
        mails=file.listFiles();
        mails2=new HashMap<Integer, String>();
        
//        System.out.println("这里我是index看看我执行了几次");
        for(int i=0;i<mails.length;i++)
        {
            mails2.put((i+1), mails[i].getName());
        }
        pw.println("一共有"+mails2.size()+"个邮件");
        
        for(Map.Entry<Integer,String > entry :mails2.entrySet())
        {
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
            pw.println("第"+entry.getKey()+"个邮件是:"+entry.getValue());
        }
    }
    
    public void read()
    {
        list();
        pw.println("输入你要读取第几个邮件");
        receive();
        String mailname=mails2.get(Integer.parseInt(info.trim()));
        pw.println("邮件名"+mailname);
        File f=new File(".\\mail\\"+mailname);
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(f);
            
            byte bytes[]=new byte[1024];
            int n=0;//得到实际读取得到字节数
            //循环读取
            while((n=fis.read(bytes))!=-1)
            {
                String s=new String(bytes,0,n);
                pw.println(s);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
    
}


在命令提示符中telnet,连接到端口后

在输入用户名密码后可以进行读,写,删,列表,退出,断开连接等操作








原创粉丝点击