Java TCP 发送第一条消息,然后无限等待 [英] Java TCP sending first message, then infinite wait

查看:32
本文介绍了Java TCP 发送第一条消息,然后无限等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题基于以下代码:

My Issue based on code below:

  • 运行 TCPServer.java
  • 然后运行 ​​TCPClient.java

我希望让客户打印出来

服务器说(1):嘿伙计 1

Server Said(1): HEY DUDE 1

服务器说(2):嘿伙计 2

Server Said(2): HEY DUDE 2

...但它只是停留在 HEY DUDE 1.我在做什么没有产生我想要的结果?

... but it just stays on HEY DUDE 1. What am I doing that is not producing the results I want?

TCPServer.java

TCPServer.java

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

class TCPServer {
    public static void main (String args[]) throws Exception{
        new TCPServer();
    }
    TCPServer() throws Exception{
        //create welcoming socket at port 6789
        ServerSocket welcomeSocket = new ServerSocket(6789);

        while (true) {
            //block on welcoming socket for contact by a client
            Socket connectionSocket = welcomeSocket.accept();
            // create thread for client
            Connection c = new Connection(connectionSocket);
        }
    }
    class Connection extends Thread{
        Socket connectionSocket;
        Connection(Socket _connectionSocket){
            connectionSocket = _connectionSocket;
            this.start();
        }
        public void run(){
            try{
                //create input stream attached to socket
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream()));
                //create output stream attached to socket
                PrintWriter outToClient = new PrintWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
                //read in line from the socket
                String clientSentence = inFromClient.readLine();
                System.out.println("Client sent: "+clientSentence);
                //process
                String capitalizedSentence = clientSentence.toUpperCase() + '\n';
                //write out line to socket
                outToClient.print(capitalizedSentence);
                outToClient.flush();
            }catch(Exception e){}
        }
    }
}

TCPClient.java

TCPClient.java

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

class TCPClient {
    //String name="";
    String host = "localhost";
    int port = 6789;
    Socket socket = null;
    public static void main(String args[]) throws Exception{
        TCPClient client = new TCPClient();
        client.SendToServer("Hey dude 1");
        System.out.println("Server Said(1): "+client.RecieveFromServer());
        client.SendToServer("Hey dude 2");
        System.out.println("Server Said(2): "+client.RecieveFromServer());
        client.close();
    }

    TCPClient(String _host, int _port) throws Exception{
        host = _host;
        port = _port;
        socket = new Socket(host, port);
    }
    TCPClient() throws Exception{
        socket = new Socket(host, port);
    }
    void SendToServer(String msg) throws Exception{
        //create output stream attached to socket
        PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        //send msg to server
        outToServer.print(msg + '\n');
        outToServer.flush();
    }
    String RecieveFromServer() throws Exception{
        //create input stream attached to socket
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader (socket.getInputStream()));
        //read line from server
        String res = inFromServer.readLine(); // if connection closes on server end, this throws java.net.SocketException 
        return res;
    }
    void close() throws IOException{
        socket.close();
    }
}

推荐答案

您的服务器线程在您处理第一条消息后立即结束.您需要将服务器代码放入这样的循环中:

Your server thread ends as soon as you process first message. You need to put server code into a loop like this:

String clientSentence;
while ((clientSentence = inFromClient.readLine()) != null) {
    System.out.println("Client sent: "+clientSentence);
    //process
    String capitalizedSentence = clientSentence.toUpperCase() + '\n';
    //write out line to socket
    outToClient.print(capitalizedSentence);
    outToClient.flush();
}

这篇关于Java TCP 发送第一条消息,然后无限等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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