Pregunta

I am trying to work through a socket chat with just one client and the server. I have it successfully running, as in the server is capable of passing messages back and forth, but when I attempt to make the server side implementation a bit more complex, adding commands and such, that the client can use, the communication fails. It appears it might go out of sync even as using the same commands over and over again can produce different results, even though I flush everything after every command.

Example of simplistic output, this works as expected, every time:

Client:

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

public class Test1Client
{
public static void main(String args[])
{


    InputStreamReader convert = new InputStreamReader(System.in);
    BufferedReader stdin = new BufferedReader(convert);

    try
    {
        Socket echoClient = new Socket("localhost", 17);
        PrintStream outs = new PrintStream(echoClient.getOutputStream());
        BufferedReader ins = new BufferedReader(new InputStreamReader(echoClient.getInputStream()));
        while(true){
        System.out.print("Type whatever you want: ");
        String line = stdin.readLine();
        outs.println(line);
        System.out.println("Server says: " + ins.readLine());
        }

    }
    catch (IOException e)
    {
        System.out.println(e);
    }
}
}

Server:

import java.net.*;
import java.util.ArrayList;
import java.io.*;


public class Test1Server
{
    public static void main(String args[])
    {
        try
        {
             ServerSocket socket= new ServerSocket(12167);
             //Try not to use port number < 2000. 
             System.out.println("Waiting for a client to connect..."); 
             Socket s = socket.accept();
            System.out.println("Client Connected.");
            BufferedReader ins = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintStream outs = new PrintStream(s.getOutputStream());                 

      while (true)
             {

            String line = ins.readLine();
            outs.println(line); 

        }

    }
    catch (IOException e)
        {
            e.getStackTrace();
         }
    }
}

I get output like this, it works every time just spitting it back out:

Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login
Type whatever you want: login
Server says: login

But when I make the server side a bit more complex by replacing its while(true) block with the following, I get a much more messy result:

                String line = ins.readLine();
                String response = manager.process(line);
                outs.println(response); 
                outs.flush();

process:

msg= "User logged in successfully \n";
return msg;

You'll also notice some commented lines in the process command code. When I give back a simple message the server seems to keep up, but when I use the login function as well it gives the terrible output like this:

Type whatever you want: login ryanne 
Server says: ryanne logged in successfully 
Type whatever you want: login ryanne 
Server says: 
Type whatever you want: login ryanne 
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: login ryanne 
Server says: 
Type whatever you want: newuser jeff 
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: newuser jeff 12345
Server says: 
Type whatever you want: new user jeff 12345
Server says: You may already be logged in or did not use correct username or password 
Type whatever you want: 

Again, notice the blanks where nothing comes back from the server, and then even the change in the commands does not prompt different responses. Its as if it went out of sync, just by using one additional function?

¿Fue útil?

Solución

You have some "\n" at the end of some strings. If you both put "\n" and use println, you will have double carriage returns, which will mess up your protocol. Remove the "\n"'s, and it should work better.

Otros consejos

Maybe, data you sent was not flushed. Use outs.flush(); after outs.println(line); or change it's constructor call to PrintStream(echoClient.getOutputStream(),true); (enable auto-flush on printing new line)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top