聊天服务器:打印聊天记录Java [英] Chat Server: Print chat history java

查看:125
本文介绍了聊天服务器:打印聊天记录Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我需要在客户端显示聊天记录而不传输聊天文件.服务器和客户端在同一台机器上,即我的电脑.服务器从文件中逐个读取char并将其写入套接字.客户端需要将其读取为字符串以获取保存在文件中的确切聊天记录.但是按照我的方法,它是在单行中打印单个char
导入java.io.*;

The problem is that I need to show chat history on the client side without transferring the chat file. The server and the client are on the same machine ie my pc. The server reads char by char from file and writes it on the socket. The client needs to read it as string to get exact chat history as saved in file. But by my method it is printing single char in single line
import java.io.*;

    class Server
    {
        public static void main(String dt[])
        {
            ServerSocket sskt=null;Socket skt=null;
            InputStreamReader isrin=null,isrout=null;
            BufferedReader brin=null,brout=null;PrintWriter pw=null;
            FileWriter fw=null;FileReader fr=null;
            DataInputStream dis=null;DataOutputStream dos=null;
            try
            {
                sskt=new ServerSocket(1234);
                System.out.println("Waiting for Client");
                skt=sskt.accept();
                System.out.println("Connected to client");

                isrin=new InputStreamReader(skt.getInputStream());
                brin=new BufferedReader(isrin);

                isrout=new InputStreamReader(System.in);
                brout=new BufferedReader(isrout);
                pw=new PrintWriter(skt.getOutputStream(),true);
                dis=new DataInputStream(skt.getInputStream());
                dos=new DataOutputStream(skt.getOutputStream());

                SimpleDateFormat sdf=new SimpleDateFormat("dd_MM_yy");
                Date date=new Date();
                String ing=sdf.format(date);
                fw=new FileWriter(ing+".txt",true);

                //do{
                String str;
                str=brin.readLine();
                int c=Integer.parseInt(str);
                switch(c)
                {
                    case 1:
                    {
                        System.out.println("New Chat Stared");
                        String msg="";
                        do
                        {
                        SimpleDateFormat sdf1=new SimpleDateFormat("hh:mm:ss");
                        Date d=new Date();
                        String in=sdf1.format(d);

                        msg=brin.readLine();
                        System.out.println("Client says " + msg);
                        fw.write("Client "+in+" "+msg);

                        msg=brout.readLine();
                        pw.println(msg);
                        fw.write("Server "+in+" "+msg);
                        }
                        while(!msg.equals("bye"));
                        fw.close();
                    break;
                    }
                    case 2:
                    {
                        String d=brin.readLine();
                        File file=new File(d+".txt");
                        if(file.exists())
                        {
                            dos.writeBoolean(true);
                            System.out.println("Displaying Contents of File");
                            fr=new FileReader(d+".txt");
                            int z;
                            while((z=fr.read())!=-1)
                            {
                                pw.println((char)z);
                            }
                            fr.close();
                            dos.writeBoolean(true);
//it writes the contents of the file on the socket one char by char
                            }
                            else
                            {
                                dos.writeBoolean(false);
                            }
                            break;
                        }
                        case 3:
                        {
                            System.out.println("Client Exited");
                            System.exit(0);
                            break;
                        }
                        default:
                        {
                            System.out.println("Invalid choice");
                            break;
                        }
                    }
                //}while(true);

            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            finally
            {
                try
                {
                    pw.close();
                    brin.close();
                    isrin.close();
                    skt.close();
                    sskt.close();
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
            }
        }
    }

    class Client
    {
        public static void main(String dt[])
        {
            Socket skt=null;
            InputStreamReader isrout=null,isrin=null;
            BufferedReader brout=null,brin=null;
            PrintWriter pw=null;
            DataOutputStream dos=null;
            DataInputStream dis=null;
            FileReader fw=null;
        try
        {
                skt=new Socket("127.0.0.1",1234);
                System.out.println("Connected to Server");

                isrout=new InputStreamReader(System.in);
                brout=new BufferedReader(isrout);
                pw=new PrintWriter(skt.getOutputStream(),true);
                isrin=new InputStreamReader(skt.getInputStream());
                brin=new BufferedReader(isrin);
                dos=new DataOutputStream(skt.getOutputStream());
                dis=new DataInputStream(skt.getInputStream());

            //do
            //{
                System.out.println("1. To start a chat");
                System.out.println("2. To view chat history");
                System.out.println("3. Exit");
                String str="";
                str=brout.readLine();
                pw.println(str);
                int a=Integer.parseInt(str);
                switch(a)
                {
                    case 1:
                    {
                        String msg="";
                        do
                        {
                        msg=brout.readLine();
                        pw.println(msg);
                        msg=brin.readLine();
                        System.out.println("Server Says " + msg);
                        }
                        while(!msg.equals("bye"));
                        break;
                    }
                    case 2:
                    {
                        System.out.println("Enter date of chat history");
                        String date=brout.readLine();
                        pw.println(date);
                        if(dis.readBoolean())
                        {
                            System.out.println("Chat Exists");
                            fw=new FileReader("H:\\Java Programs\\chatser\\chat\\client\\"+date+".txt");
                            int ab;
                            while((ab=fw.read())!=-1)
                            {
                                System.out.println(brin.readLine());
                            }
                            fw.close();
                        } //this syntax will read from the socket char by char and print one char after another in next line. i need to get chat history as string without copying the file at client side
                        else
                        {
                            System.out.println("Incorrect Entity");
                        }
                        break;
                    }
                    case 3:
                    {
                        System.out.println("Thank you");
                        System.exit(0);
                        break;
                    }
                    default:
                    {
                        System.out.println("Incorrect Entity");
                    }
                }
            //}while(true); 

        }
            catch(Exception e)
            {
                System.out.println(e);
            }
            finally
            {
                try
                {
                    pw.close();
                    brout.close();
                    isrout.close();
                    skt.close();
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                }
            }
        }
}

推荐答案

查看以下代码段:

int z;
while((z=fr.read())!=-1) {
   pw.println((char)z);
}

您正在将读取的整数转换为char,该char将仅包含单个字符.这不会打印出您要查找的全部值.如果要将该整数转换为要打印的字符串,请尝试如下操作:

You are converting the integer read to a char, which will only hold a single character. This will not print the entire value you're looking for. If you want to convert that integer to a String to be printed, try something like this:

pw.println(Integer.toString(z))

这篇关于聊天服务器:打印聊天记录Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆