美文网首页
命令行聊天小程序

命令行聊天小程序

作者: 勇者与王者 | 来源:发表于2019-10-18 10:48 被阅读0次

    分为客户端和服务端,
    客户端:

    package chat;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    /**
    * 
    * @author qz:
    * @version 创建时间:2019年10月16日 上午10:20:59
    * 
    */
    public class ChatClient {
    
        public static void main(String[] args) throws UnknownHostException, IOException {
            // TODO Auto-generated method stub
    
            String serverIp = "172.19.0.18";
            Socket socket = new Socket(serverIp,2345);
            
            byte[] bytes = new byte[1024];
            
            int len = 0;
            
            InputStream in = socket.getInputStream();
            
            //获取客户端的输出流 即上传到服务器的流
            OutputStream out = socket.getOutputStream();
            String file = "D:\\javaApp\\record.txt";
                    
    //      FileInputStream fis = new FileInputStream(file);
                    
            FileOutputStream fos = new FileOutputStream(new File(file));
            
            //接收服务器的 连接建立的输出信息:
            len = in.read(bytes);
            //将消息写入本地文件
            fos.write(bytes);
            System.out.println(new String(bytes,0,len));
            
            String name = "Jack";
            
            
            // 给服务器发送提示信息:
            byte[] message = "Please enter your name:".getBytes();
            out.write(message,0,message.length);
            
            
            len = in.read(bytes);
            String serverName = new String(bytes,0,len);
            System.out.println("Your name :"+serverName);
            
            
            
            len = in.read(bytes);
            fos.write(bytes);
            System.out.println(serverName+":"+new String(bytes,0,len));
            
            
            //向服务器发送名字
            System.out.println(name+":"+name);
            out.write(name.getBytes());
            
            Scanner input = new Scanner(System.in);
            
            String send="";
            while ( !send.equals("exit") ) {
                
                System.out.print(name+": ");
                send = input.nextLine();
                out.write(send.getBytes(),0,send.getBytes().length);
                
                len = in.read(bytes);
                fos.write(bytes,0,len);
                System.out.println(serverName+":"+new String(bytes,0,len));
                
                
                
                
                
            }
            
            // 调用socket的 shutdownOutput()方法 发送终止序列
            socket.shutdownOutput();
            
            //获取客户的输入流,即 服务器返回的消息
            
            len = in.read(bytes);
            
            System.out.println(new String(bytes,0,len));
            
            fos.close();
            socket.close();
            
        }
    
    }
    
    

    服务端:

    package chat;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    /**
    * 
    * @author qz:
    * @version 创建时间:2019年10月16日 下午2:21:18
    * 
    */
    public class ChatServer {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
    
            ServerSocket server = new ServerSocket(2345);
            Socket socket = server.accept();
            
            //获取客户端 的输出流 即向服务器写数据
            OutputStream out = socket.getOutputStream();
            out.write("Connection Established successfully!".getBytes());
            
            
            String path = "/home/qz/socket";
            
            File f = new File(path);
            
            if ( !f.exists()) {
                f.mkdirs();
            }
            
            FileOutputStream fos = new FileOutputStream(new File("record.txt"));
            
            //获取客户端的输入流 即 服务器的输出流
            InputStream in = socket.getInputStream();
            byte [] bytes = new byte[1024];
            int len = 0;
            
            
            //获取客户端的消息:
            len = in.read(bytes);
            fos.write(bytes);
            System.out.println(new String (bytes,0,len));
            
            String serverName = "Host";
            //将服务器名字发送给客户端
            System.out.println(serverName+":"+serverName);
            out.write(serverName.getBytes());
            
            
            
            //获取客户端姓名
            out.write("and your name?".getBytes());
            
            len = in.read(bytes);
            fos.write(bytes);
            
            String customerName = new String(bytes,0,len);
            System.out.println("from customer:"+customerName);
            
            
            
            Scanner input = new Scanner(System.in);
            
            while (  (len = in.read(bytes) )!= -1 ) {
                
                
                fos.write(bytes,0,len);
                System.out.println(customerName+":"+new String(bytes,0,len));
                
                
                System.out.print(serverName+":");
                String response = input.nextLine();
                fos.write( (serverName+":"+response).getBytes());
                out.write(response.getBytes(),0,response.getBytes().length);
                
                
                
                
            }
            
            //写往客户端 输出流
            out.write("Connection shutdown!".getBytes());
            
            fos.close();
            socket.close();
            
            
            
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:命令行聊天小程序

          本文链接:https://www.haomeiwen.com/subject/qacimctx.html