Java ServerSocket.accept()

Syntax

ServerSocket.accept() has the following syntax.

public Socket accept()  throws IOException

Example

In the following code shows how to use ServerSocket.accept() method.


//www .  j  a va  2 s.  com

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

  public static void main(String args[]) {
    try {

      int port = 5555;
      ServerSocket ss = new ServerSocket(port);

      while (true) {
        // Accept incoming requests
        Socket s = ss.accept();

        // Write result to client
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeInt(100);

        s.close();
      }
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}




















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder