This site requires JavaScript, please enable it in your browser!
Greenfoot back
stefan42
stefan42 wrote ...

2016/2/8

Problem with socket's InputStream

stefan42 stefan42

2016/2/8

#
Hello Greenfoot community, I want to write a sipmel server client conection. The server works very good but i got some problems with the InputStream which should be "hello_world_from_server" of the client it doesn't get any informations from the server but if I use my browser as client I get the Stream. So what is wrong with my code? That is my server code
import greenfoot.*;
import java.net.*;
import java.io.*;

/**
 * Write a description of class world here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class server extends World
{
    
    /**
     * Constructor for objects of class world.
     * 
     */

    public server()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 50);
    }
    public void act() 
    {     
        try
        {
            ServerSocket server = new ServerSocket(1234);
            while (true) new ServConn(server.accept());
        }
        catch(IOException e)
        {
            System.out.println("I/O Error "+e);
        }
    }
}
class ServConn
{
            Socket sock;
        ServConn(Socket s)
        {
            sock=s;
            run();
        }
        public void run()
        {
            try
            {
                InputStream in = sock.getInputStream();
                System.out.println(in.available());
                BufferedReader buff = new BufferedReader(new InputStreamReader(in));
                while (buff.ready()) 
                {
                    System.out.println(buff.readLine());
                }
                OutputStream out = sock.getOutputStream();
                PrintStream ps = new PrintStream(out, true);
                ps.println("hello_world_from_server"); 
                sock.close();
            }
            catch(IOException e)
            {
                System.out.println("I/O Error "+e);
            }
        }
}
And that is the client code
import greenfoot.*;
import java.net.*;
import java.io.*;

/**
 * Write a description of class world here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class client extends World
{

    /**
     * Constructor for objects of class world.
     * 
     */

    public client()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 50); 
    }
    public void act()
    {
        try{
             new ServConn(new Socket( "127.0.0.1", 1234 ));
        }
        catch (IOException e){System.out.println("I/O Error "+e);};
    }
}
class ServConn
{
            Socket sock;
        ServConn(Socket s)
        {
            sock=s;
            run();
        }
        public void run()
        {
            try
            {
                InputStream in = sock.getInputStream();
                System.out.println(in.available());
                BufferedReader buff = new BufferedReader(new InputStreamReader(in));
                while (buff.ready()) 
                {
                    System.out.println(buff.readLine());
                }
                OutputStream out = sock.getOutputStream();
                PrintStream ps = new PrintStream(out, true);
                ps.println("hello_world_from_client");
                sock.close();
            }
           catch(IOException e)
            {
                System.out.println("I/O Error "+e);
            }
        }
}
Thank you.
davmac davmac

2016/2/10

#
Both your server and client wait for input before they send any output. That's the problem, I think; they both just sit waiting for the other to send something. Perhaps you should make the client send a hello message before it waits for a response from the server.
stefan42 stefan42

2016/2/10

#
So I changed my client code for run to this
public void run()
        {
            try
            {
                OutputStream out = sock.getOutputStream();
                PrintStream ps = new PrintStream(out, true);
                ps.println("hello_world_from_client");
                InputStream in = sock.getInputStream();
                //System.out.println(in.available());
                BufferedReader buff = new BufferedReader(new InputStreamReader(in));
                while (buff.ready()) 
                {
                    System.out.println(buff.readLine());
                }
                sock.close();
            }
           catch(IOException e)
            {
                System.out.println("I/O Error "+e);
            }
        }
And I changed my server's code for run to this
public void run()
        {
            try
            {
                OutputStream out2 = sock.getOutputStream();
                PrintStream ps2 = new PrintStream(out2, true);
                ps2.println("hello_world_from_server"); 
                InputStream in = sock.getInputStream();
                //System.out.println(in.available());
                BufferedReader buff = new BufferedReader(new InputStreamReader(in));
                while (buff.ready()) 
                {
                    System.out.println(buff.readLine());
                }
                sock.close();
            }
            catch(IOException e)
            {
                System.out.println("I/O Error "+e);
            }
        }
But now I got the problem that the client gets the OutputStream only sometimes but the server gets always the OutputStream of the client. How can I fix this?
davmac davmac

2016/2/10

#
Because:
            while (buff.ready()) 
            {
                System.out.println(buff.readLine());
            }
buff.ready() checks if input has been received from the other end of the socket. It may not have been received yet. If it hasn't, buff.ready() returns false, the while loop never executes, and the client doesn't read whatever the server sent.
stefan42 stefan42

2016/2/11

#
OK, now it works perfectly. Thank you!
You need to login to post a reply.