简体   繁体   中英

Client unable to send messages to server Java socket

I'm trying to work on a simple project with a client and a server, where the client sends a string and the server will reverse it to send back to the client. However, they are both getting hung up on sending and receiving the message. So pretty much to show you where I am in my program where it stops...here is my console outputs for the client and server.

Client

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

Server

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

I've read a little about how like you need to use the exact message output and input. So like if the client is sending an input with a new line, the server should expect to read that in. Though I don't see that in my code, but my hunch is that is somehow the problem.

My code is as follows...

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;
    }

}

Put out.flush(); after the out.println(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM