客户端无法将消息发送到服务器Java套接字

时间:2017-02-05 04:02:09

标签: java sockets

我尝试使用客户端和服务器处理一个简单的项目,客户端发送一个字符串,服务器将其反转以发送回客户端。然而,他们都在发送和接收消息时被挂断了。所以几乎要告诉你我在我的程序停止的位置......这是我的客户端和服务器的控制台输出。

客户端

Connecting to server...
S: Connected to server at 4446
You sent Oranges, sending message...

服务器

Waiting for a connection...
Connection received from /127.0.0.1 on port 4446
Waiting for response...

我已经读过一些关于你需要使用确切消息输出和输入的内容。因此,如果客户端正在发送带有新行的输入,那么服务器应该期望读取它。虽然我在代码中看不到,但我的预感是某种程度上问题。

我的代码如下......

Client.java

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client
{
    PrintWriter out;
    Scanner conIn;

    public static void main(String[] args) throws UnknownHostException, IOException
    {
        Client c = new Client();
        c.run();
    }

    public void run() throws UnknownHostException, IOException
    {
        System.out.println("Connecting to server...");
        Socket connection = new Socket("localhost", 4446); //Connects to server
        out = new PrintWriter(connection.getOutputStream());
        conIn = new Scanner(connection.getInputStream());
        String msg = conIn.nextLine();
        System.out.println(msg);
        String s = "Oranges";
        System.out.println("You sent " + s + ", sending message...");
        out.println(s); //STOPS HERE
        msg = conIn.nextLine();
        System.out.println(msg);
        out.flush();
    }
}

Server.java

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server implements Runnable
{
    ServerSocket serverSocket;
    Scanner in;
    PrintWriter out;
    Socket connection = null;

    public static void main(String[] args)
    {
        Server s = new Server();
        s.run();
    }

    public void run()
    {
        try
        {
            serverSocket = new ServerSocket(4446); //Create server
            System.out.println("Waiting for a connection...");
            this.connection = serverSocket.accept(); //Accept connection
            System.out.println("Connection recieved from " + connection.getInetAddress() + " on port " + connection.getLocalPort());
            out = new PrintWriter(connection.getOutputStream()); //Gets output to connection
            out.flush();
            in = new Scanner(connection.getInputStream()); //Gets input to connection
            out.println("S: Connected to server at " + serverSocket.getLocalPort());
            out.flush();
            System.out.println("Waiting for response...");
            String msg = in.nextLine(); //STOPS HERE
            System.out.println("Message recieved! Reversing now.");
            String rMsg = reverse(msg);
            System.out.println("Returning message...");
            out.println("S: Your message was: " + msg + " and it is now " + rMsg);
            out.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public String reverse(String s)
    {
        String revS = "";

        for(int i = s.length() - 1; i >= 0; i--)
        {
            revS = revS + s.charAt(i);
        }
        return revS;
    }

}

1 个答案:

答案 0 :(得分:1)

out.flush();放在out.println(s);

之后