List of usage examples for java.net ServerSocket ServerSocket
public ServerSocket(int port) throws IOException
From source file:HTTPServer.java
public static void main(String[] args) throws Exception { ServerSocket sSocket = new ServerSocket(1777); while (true) { System.out.println("Waiting for a client..."); Socket newSocket = sSocket.accept(); System.out.println("accepted the socket"); OutputStream os = newSocket.getOutputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream())); String inLine = null;//from ww w. ja v a 2 s. c om while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) { System.out.println(inLine); } System.out.println(""); StringBuffer sb = new StringBuffer(); sb.append("<html>\n"); sb.append("<head>\n"); sb.append("<title>Java \n"); sb.append("</title>\n"); sb.append("</head>\n"); sb.append("<body>\n"); sb.append("<H1>HTTPServer Works!</H1>\n"); sb.append("</body>\n"); sb.append("</html>\n"); String string = sb.toString(); byte[] byteArray = string.getBytes(); os.write("HTTP/1.0 200 OK\n".getBytes()); os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes()); os.write("Content-Type: text/html\n\n".getBytes()); os.write(byteArray); os.flush(); os.close(); br.close(); newSocket.close(); } }
From source file:EchoServer.java
public static void main(String[] args) throws Exception { ServerSocket m_ServerSocket = new ServerSocket(12111); int id = 0;//from w w w. j a va 2 s . c om while (true) { Socket clientSocket = m_ServerSocket.accept(); ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); cliThread.start(); } }
From source file:Main.java
public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(123456); File myFile = new File("s.pdf"); while (true) {/*w ww . jav a 2 s . co m*/ Socket sock = servsock.accept(); byte[] mybytearray = new byte[(int) myFile.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush(); sock.close(); } }
From source file:MultiThreadServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); System.out.println("Listening"); while (true) { Socket sock = ssock.accept(); System.out.println("Connected"); new Thread(new MultiThreadServer(sock)).start(); }//w ww . j a va 2s. co m }
From source file:MainClass.java
public static void main(String[] args) { for (int port = 1; port <= 65535; port++) { try {//from ww w. ja va2 s . c o m // the next line will fail and drop into the catch block if // there is already a server running on the port ServerSocket server = new ServerSocket(port); } catch (IOException ex) { System.out.println("There is a server on port " + port + "."); } } }
From source file:SquareServer.java
public static void main(String args[]) throws Exception { int port = Integer.parseInt(args[0]); ServerSocket ss = new ServerSocket(port); while (true) { Socket s = ss.accept();//from ww w. java2 s. c o m InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); double value = dis.readDouble(); value *= value; OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); dos.writeDouble(value); s.close(); } }
From source file:LoopingSocketServer.java
public static void main(String args[]) throws Exception { ServerSocket servSocket;/*from w w w. j ava 2 s .co m*/ Socket fromClientSocket; int cTosPortNumber = 1777; String str; servSocket = new ServerSocket(cTosPortNumber); System.out.println("Waiting for a connection on " + cTosPortNumber); fromClientSocket = servSocket.accept(); System.out.println("fromClientSocket accepted"); ObjectOutputStream oos = new ObjectOutputStream(fromClientSocket.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(fromClientSocket.getInputStream()); while ((str = (String) ois.readObject()) != null) { System.out.println("The message from client: " + str); if (str.equals("bye")) { oos.writeObject("bye bye"); break; } else { str = "Server returns " + str; oos.writeObject(str); } } oos.close(); fromClientSocket.close(); }
From source file:GenericServer.java
public static void main(String args[]) { int serverPort = 1234; try {//from w ww. j ava2s .c o m ServerSocket server = new ServerSocket(serverPort); do { Socket client = server.accept(); (new ServerThread(client)).start(); } while (true); } catch (IOException ex) { System.exit(0); } }
From source file:MTServerBase.java
static public void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(8080); while (true) { Socket esock = null;//w w w. j ava 2 s .c o m esock = ssock.accept(); MTServerBase t = new MTServerBase(); t.socket = esock; t.start(); esock.close(); } }
From source file:SimpleSocketServer.java
public static void main(String args[]) throws Exception { ServerSocket serverSocket;/*w w w . java2s . co m*/ int portNumber = 1777; Socket socket; String str; str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>"; str += "<ticketRequest><customer custID=\"1\">"; str += "</ticketRequest>"; serverSocket = new ServerSocket(portNumber); System.out.println("Waiting for a connection on " + portNumber); socket = serverSocket.accept(); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(str); oos.close(); socket.close(); }