服务器在客户端 - 服务器应用程序中不接收消息 [英] Server doesn't receive message in client-server application

查看:143
本文介绍了服务器在客户端 - 服务器应用程序中不接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的客户端 - 服务器应用程序中,客户端向服务器发送消息,服务器应显示消息。但在我的情况下,客户端只能发送,但服务器无法实现。

In my client-server application, the client sends message to Server and the Server should display the message. But in my case, the client is only able to send but the server can't achieve it.

我尝试过不同的端口号(即8080,8000,4444等等)。看起来套接字可以建立连接,但我真的不知道为什么服务器不能从客户端读取输入。

I have tried with different port numbers (i.e. 8080, 8000, 4444 etc). It seems that the socket can set up the connection, but I really don't know why the server can't read the input from client.

这是我的完整项目(我已经忽略了这两个应用程序的主类,因为我只有调用方法):

This is my complete project (I have ignored the main classes for both application here, because I have nothing more than just calling the methods):

EchoServer.java:

EchoServer.java:

    package client.server;

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

public class EchoServer {

    public EchoServer() {
    }

    public void establish() {
        ServerSocket serverSocket = null;
        try {

            serverSocket = new ServerSocket(8080);
        } catch (IOException e) {
            System.out.println("Could not listen on port: 1234");
            System.exit(-1);
        }
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("Accept failed: 1234");
            System.exit(-1);
        }
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            out = new PrintWriter(
                    clientSocket.getOutputStream(), true);
            in = new BufferedReader(
                    new InputStreamReader(
                            clientSocket.getInputStream()));
        } catch (IOException ioe) {
            System.out.println("Failed in creating streams");
            System.exit(-1);
        }
        String inputLine, outputLine;
        try {
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
                if (inputLine.equals("Bye.")) {
                    break;
                }
            }
        } catch (IOException ioe) {
            System.out.println("Failed in reading, writing");
            System.exit(-1);
        }
        try {
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            System.out.println("Could not close");
            System.exit(-1);
        }
    }
}

EchoClient.java:

EchoClient.java:

    package server.client;

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

public class EchoClient {

    public EchoClient() {
    }

    public void establish() {
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            //echoSocket = new Socket(InetAddress.getLocalHost(), 1234);
            echoSocket = new Socket(InetAddress.getLocalHost(), 8080);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O");
            System.exit(1);
        }
        BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
        String userInput;
        try {
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                if (userInput.equals("Bye.")) {
                    break;
                }
                System.out.println("echo: " + in.readLine());
            }
            out.close();
            in.close();
            stdIn.close();
            echoSocket.close();
        } catch (IOException ioe) {
            System.out.println("Failed");
            System.exit(
                    -1);

        }

    }
}


推荐答案

您的服务器不会将传入的文本写入控制台,而只会返回到未处理来自服务器的传入文本的客户端。

Your server doesn't write the incoming text to the console but only back to the client which doesn't handle incoming text from the server yet.

(out不是System.out,而是Socket.out!)

(out isn't System.out but Socket.out!)

这篇关于服务器在客户端 - 服务器应用程序中不接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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