0

The following code returns -1.Please any body tell solution to solve and tell something about this uses of localport .

import java.net.*;
import java.io.*;
public class Test 
{
    public static void main(String[] args) 
    {
        Socket s = new Socket();
        int i = s.getLocalPort();
        System.out.println(i);
    }
}

1 Answers1

0

A Socket created by new Socket() doesn't have a local port until you either bind() or connect() it.

user207421
  • 305,947
  • 44
  • 307
  • 483