bobby9699 2017-04-05 13:06 采纳率: 100%
浏览 2464
已采纳

关于java实现一对一聊天连续发消息的问题

下面的代码只能实现服务端和客户端你一句我一句聊天,要是有一方连续发几条消息就会崩溃,请问如何实现连续发消息这个功能,老师说要用到线程,求提供一点思路

客户端

 import java.io.*;
import java.net.*;

public class SingleTalkClient
{
    public static void main(String[] args) throws IOException
    {

        Socket client = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            client = new Socket("172.16.122.27", 6677);
            out = new PrintWriter(client.getOutputStream(), true); //auto flush
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: 127.0.0.1.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: 127.0.0.1.");
            System.exit(1);
        }

        //从标准输入流(键盘)中获取信息
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

    String fromServer, fromUser;
    boolean sbye = false;
    boolean ubye = false;

    System.out.print("Client input:");
    fromUser = stdIn.readLine();
        while( true )  
    {
        if( ubye == false )
        {
            out.println(fromUser);
            out.flush();
            //System.out.println("Client: " + fromUser);
            if (fromUser.equals("Bye."))
                ubye = true;
        }

        if( sbye == false )
        {
            fromServer = in.readLine();
            System.out.println("from Server: " + fromServer);
            if (fromServer.equals("Bye."))
                sbye = true;
        }

        if( ubye == false )
        {
            System.out.print("Client input:");
            fromUser = stdIn.readLine();
        }

        if( ubye == true && sbye == true )
            break;
        }

        out.close();
        in.close();
        stdIn.close();
        client.close();
    }
}

服务端

 import java.net.*;
import java.io.*;

public class SingleTalkServer
{
    public static void main(String[] args) throws IOException
    {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(6677);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();  //程序将在此等候客户端的连接
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
    System.out.println("Accept OK!");

        //打开输入/输出流
    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);  //auto flush
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));

    //从标准输入流(键盘)中获取信息
        BufferedReader sin = new BufferedReader( new InputStreamReader( System.in ) );

    boolean  sinbye = false;
    boolean  inbye = false;
    String sinputLine, inputLine;

    inputLine = in.readLine();
    System.out.println( "from Client: " + inputLine );

    System.out.print("Server input:");
    sinputLine = sin.readLine();

        while( true )
    {
        if( sinbye == false )
        {
            out.println(sinputLine);
            out.flush();
            //System.out.println("Server: " + sinputLine);

            if (sinputLine.equals("Bye."))
                sinbye = true;
        }

        if( inbye == false )
        {
            inputLine = in.readLine();
            System.out.println( "from Client: " + inputLine );

            if (inputLine.equals("Bye."))
                inbye = true;
        }

        if( sinbye == false )
        {
            System.out.print("Server input:");
            sinputLine = sin.readLine();
        }

        if( sinbye == true && inbye == true )
            break;
        }

        out.close();
        in.close();
    sin.close();

        clientSocket.close();
        serverSocket.close();
    }
}

  • 写回答

4条回答 默认 最新

  • WeYoungg 2017-04-05 13:27
    关注

    那就发短信跟对方说别说太多

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!