views:

52

answers:

2

I am trying a program with Swing.
I am using a socket to connect to the server, and the client has the gui code.


public class FactClient extends JFrame implements ActionListener
{
    Socket s;
    InputStream in;
    OutputStream os;
    Scanner sin;
    PrintWriter out;
    JPanel jp;
    JTextField jt;
    JButton jb;
    JLabel jl;
    FactClient()
    {
        jp = new JPanel();

        jt = new JTextField("Enter number",15);
        jb = new JButton("Compute Factorial");
        jl = new JLabel("Answer");

        jb.addActionListener(this);
        jp.add(jt);
        jp.add(jb);
        jp.add(jl);

        add(jp);
        setVisible(true);
        setSize(200,100);
        try
        {
            s = new Socket("localhost",8189);
            try
            {
                in = s.getInputStream();
                os = s.getOutputStream();

                sin = new Scanner(in);
                out = new PrintWriter(os);
                out.println("Done with the bingo");
            }
            finally {}
        }
        catch(Exception e)
        {
            System.out.println("Error in client code " + e );
        }    
    }
    public void actionPerformed(ActionEvent ae)
    {
        try {
                System.out.println("Connection established " + jt.getText());
                String t = jt.getText();
                out.println("Ashish");
                System.out.println("Data Send");
                t = sin.nextLine();
                jl.setText(t);
        }
        catch(Exception e)
        {
            System.out.println("Error in client code " + e );
        }

    }
    public static void main(String args[])
    {
        new FactClient();
    }
}


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

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

        ServerSocket s;
        Socket socket;
        try
        {
            s= new ServerSocket(8189);
            socket = s.accept();
            try
            {
                InputStream in = socket.getInputStream();
                OutputStream os = socket.getOutputStream();

//              Scanner sin = new Scanner(in);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                PrintWriter ou = new PrintWriter(os);
                System.out.println("Connection Established : Stream initailzed");
                try
                {
                    String data = br.readLine();
                    System.out.println("Data Recvd." + data);
                    data = br.readLine();
                }
                catch(Exception e)
                {
                    System.out.println("EEEEEEEEEEEEE" + e);
                }
                //int fact = data +20;
                ou.println("40");
            }
            catch (Exception e)
            {
                System.out.println("ERROR :P");
            }
            finally
            {
                socket.close();
            }
        }
        catch(Exception e)
        {
            System.out.println("ERROR" + e);
        }
    }
}

The server code simply reads the data that I send using System.out.println. But the problem is it hangs up; the server never gets the data!

       out.println("Done with the bingo");

This is the first string that server should get. But it stays in the wait state as if nothing is received.

+3  A: 

You must use flush() after each println() or activate automatic flushing on the PrintWriter so the data gets really sent:

...
out = new PrintWriter(os);
out.println("Done with the bingo");
out.flush();
...

or

...
out = new PrintWriter(os, true);    // autoflush
out.println("Done with the bingo");
...

don't forget the server...

Carlos Heuberger
+2  A: 

Enabling autoflush in your PrintWriter as Carlos said should solve your main question. A couple of other thoughts you might consider:

Wrap the server logic in a loop (e.g. while(true) {...}) if you want it to handle multiple client requests, and handle each request in a separate thread.

Since you are making the client request on the Swing Event Dispatch Thread (i.e. the actionPerformed() method) you might consider wrapping it in a Runnable or SwingWorker so you don't block the dispatch thread. Otherwise you might notice the UI appear to hang or not paint while the socket communication is happening.

Chris B.