Click here to Skip to main content
15,886,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying a simple Java program to send and receive data... Server is sending the data and Client receives that... But this generates Socket Exception : Connection Reset

This is my Server side code to sent Integer value...

Java
try
    {
        ServerSocket server = new ServerSocket(9089);

        Socket socket = server.accept();

        OutputStream out = socket.getOutputStream();
        out.write(12);
        out.flush();
        System.out.println("Data Sent....");
        Thread.sleep(5000);
    }
    catch(Exception  e)
    {
        System.out.println("Server Error : " + e.toString());
    }


This is my Client side code to receive that Int value...

Java
try
    {
        Socket client = new Socket(InetAddress.getLocalHost(), 9089);
        System.out.println("Connected ....");

        InputStream in = client.getInputStream();
        while(in.available()>0)
        {
            System.out.println("Unavailable...");
        }
        System.out.println("Received : " + in.read() );
        in.close();
    }
    catch(Exception e)
    {
        System.out.println("Sender Error : " + e.toString());
    }

Please help me solving this....
Posted

1 solution

I got the solution...

Instead of

Java
while(in.available()>0)


I need to do

Java
while(in.available()==0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900