Hello,

I am learning Socket programming. I used a code of server for communication on port 1500 and other for client. I don;t know server program is throwing exception as

java.net.SocketException: Socket is not connected
at java.net.Socket.getInputStream(Socket.java:790)
at serversocket.ServerSocket.main(ServerSocket.java:3 8)

ServerSocket.java
Java Code:
import java.io.*;
import java.net.*;

public class ServerSocket {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int port=1500;
        Socket socket;
        InputStream sin;
        OutputStream sout;
        DataInputStream in;
        DataOutputStream out;
        java.net.ServerSocket ss;
        try         
        {
        
        ss = new java.net.ServerSocket(port);
        System.out.println("Waiting for Client");
        
        socket = new Socket();
        ss.accept();
        System.out.println("Got Client");
        
        sin = socket.getInputStream();
        sout = socket.getOutputStream();
        
        in = new DataInputStream(sin);
        out = new DataOutputStream(sout);
        System.out.println("Got Client");
         String line = null;
         while (true) 
         {
             line = in.readUTF(); // wait for the client to send a line of text.

             System.out.println("The dumb client just sent me this line : " + line);
             System.out.println("I'm sending it back...");

             out.writeUTF(line); // send the same line back to the client.
             out.flush(); // flush the stream to ensure that the data reaches the other end.

             System.out.println("Waiting for the next line...");
             System.out.println();
        }
        
    }catch(Exception e)
    {
             e.printStackTrace();
    }}
}
clientSocket.java
Java Code:
import java.net.*;
import java.io.*;
public class ClientSocket {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        int serverPort = 1500; // make sure you give the port number on which the server is listening.
        String address = "127.0.0.1"; // this is the IP address of the server program's computer. // the address given here means "the same computer as the client".


        try {
            InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
            System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");


            Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
            System.out.println("Yes! I just got hold of the program.");


            // Get the input and output streams of the socket, so that you can receive and send data to the client.
            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();


            // Just converting them to different streams, so that string handling becomes easier.
            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);


            // Create a stream to read from the keyboard.
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));


            String line = null;
            System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
            System.out.println();


            while (true) {
                line = keyboard.readLine(); // wait for the user to type in something and press enter.


                System.out.println("Sending this line to the server...");
                out.writeUTF(line); // send the above line to the server.
                out.flush(); // flush the stream to ensure that the data reaches the other end.
                line = in.readUTF(); // wait for the server to send a line of text.


                System.out.println("The server was very polite. It sent me this : " + line);
                System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
                System.out.println();
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
 
        
    }
}
Thanks in advance