packagesocket; importjava.io.BufferedReader; importjava.io.DataOutputStream; importjava.io.IOException; importjava.io.InputStreamReader; importjava.net.ServerSocket; importjava.net.Socket; publicclassSocketSingleServer{ publicstaticvoidmain(String[]args)throwsIOException{ ServerSocketserverSocket=null; Socketclient=null; try{ serverSocket=newServerSocket(8888); client=serverSocket.accept(); BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in)); DataOutputStreamdos=newDataOutputStream(client.getOutputStream()); while(!client.isClosed()){ Stringstr=""; if((str=br.readLine())!=null){ dos.writeUTF(str); dos.flush(); } } }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }finally{ client.close(); } } } |