简体   繁体   中英

Java Socket Chat unable to establish connection to port

I am trying to code a simple server and client chat application, however my code jumps straight to the exception, meaning it cannot create the connection to the port.

Server:

public class Server
{
   public static void main(String args[])
   {
   try
   {
       ServerSocket ss = new ServerSocket(80);
       Socket sock = ss.accept();
       DataInputStream recive = new DataInputStream(sock.getInputStream());
       DataOutputStream sendOut = new DataOutputStream(sock.getOutputStream());
       BufferedReader readOut = new BufferedReader(new InputStreamReader(System.in));

       String in = "";
       String out = "";
       Scanner scan = new Scanner(System.in);
       System.out.println("Talk with client(y/n): ");
       String start = scan.nextLine();
       while(start == "y")
       {
       in = recive.readUTF();
       System.out.println(in);
       out = readOut.readLine();
       sendOut.writeUTF(out);
       sendOut.flush();
      }
    }
    catch(IOException e)
    {System.out.println("[!]CANNOT ESTABLISH CONNECTION[!]");}

  }
}

Make sure that the port that you have specified is not already in use (port number 80) . It is likely that you may be running another application on the same port (such as a web server).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM