本文整理汇总了Java中java.net.ServerSocket.accept方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocket.accept方法的具体用法?Java ServerSocket.accept怎么用?Java ServerSocket.accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.ServerSocket
的用法示例。
在下文中一共展示了ServerSocket.accept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.net.ServerSocket; //导入方法依赖的package包/类
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
ref("Server start to listen on port " + String.valueOf(PORT));
prepared = true;
Socket clientSocket = serverSocket.accept();
WireIO wireIO = createWireIO(clientSocket);
while (running) {
running &= simpleLogCommands(wireIO);
}
serverSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
示例2: listen
import java.net.ServerSocket; //导入方法依赖的package包/类
private void listen() throws IOException {
ServerSocket ss = new ServerSocket();
ss.bind(listenAddress);
TokenBucket bucket = new TokenBucket();
bucket.start();
try {
while (true) {
Socket s = ss.accept();
System.out.println("Incoming connection");
bucket.waitForToken();
new ReportSaver(s).start();
}
} catch (InterruptedException e) {
System.err.println("Interrupted while listening");
} finally {
tryToClose(ss);
}
}
示例3: StreamOverHttp
import java.net.ServerSocket; //导入方法依赖的package包/类
public StreamOverHttp(MetaFile2 f, String forceMimeType) throws IOException{
mMetaFile = f;
mUri= f.getUri();
mName = f.getName();
fileMimeType = forceMimeType!=null ? forceMimeType : "*/*";
serverSocket = new ServerSocket(0);
mainThread = new Thread(new Runnable(){
public void run(){
try{
while(true) {
Socket accept = serverSocket.accept();
new HttpSession(accept,fileMimeType);
}
}catch(IOException e){
if (debug)
Log.w(TAG, e);
}
}
});
mainThread.setName("Stream over HTTP");
mainThread.setDaemon(true);
mainThread.start();
}
示例4: SetReceiveBufferSize
import java.net.ServerSocket; //导入方法依赖的package包/类
public SetReceiveBufferSize() throws Exception {
ServerSocket ss = new ServerSocket(0);
Socket s = new Socket("localhost", ss.getLocalPort());
Socket accepted = ss.accept();
try {
s.setReceiveBufferSize(0);
} catch (IllegalArgumentException e) {
return;
} catch (Exception ex) {
} finally {
ss.close();
s.close();
accepted.close();
}
throw new RuntimeException("IllegalArgumentException not thrown!");
}
示例5: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String args[]) throws IOException {
final int portNumber = 12408;
System.out.println("Creating server socket on port " + portNumber);
ServerSocket serverSocket = new ServerSocket(portNumber);
boolean isServerStarted = true;
while (isServerStarted) {
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os, true);
pw.println("What's you name?");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String clientMessage = br.readLine();
if (clientMessage.equalsIgnoreCase("shut down")) {
isServerStarted = false;
pw.println(clientMessage );
} else {
pw.println("Hello, " + clientMessage );
}
pw.close();
socket.close();
System.out.println("Just said hello to:" + clientMessage );
}
serverSocket.close();
}
示例6: checkConfig
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Checks that the certificate is compatible with the enabled cipher suites.
* If we don't check now, the JIoEndpoint can enter a nasty logging loop.
* See bug 45528.
*/
private void checkConfig() throws IOException {
// Create an unbound server socket
ServerSocket socket = sslProxy.createServerSocket();
initServerSocket(socket);
try {
// Set the timeout to 1ms as all we care about is if it throws an
// SSLException on accept.
socket.setSoTimeout(1);
socket.accept();
// Will never get here - no client can connect to an unbound port
} catch (SSLException ssle) {
// SSL configuration is invalid. Possibly cert doesn't match ciphers
IOException ioe = new IOException(sm.getString(
"jsse.invalid_ssl_conf", ssle.getMessage()));
ioe.initCause(ssle);
throw ioe;
} catch (Exception e) {
/*
* Possible ways of getting here
* socket.accept() throws a SecurityException
* socket.setSoTimeout() throws a SocketException
* socket.accept() throws some other exception (after a JDK change)
* In these cases the test won't work so carry on - essentially
* the behaviour before this patch
* socket.accept() throws a SocketTimeoutException
* In this case all is well so carry on
*/
} finally {
// Should be open here but just in case
if (!socket.isClosed()) {
socket.close();
}
}
}
示例7: TCPForwardServer
import java.net.ServerSocket; //导入方法依赖的package包/类
public TCPForwardServer(int SOURCE_PORT, String DESTINATION_HOST, int DESTINATION_PORT) throws IOException {
this.SOURCE_PORT = SOURCE_PORT;
this.DESTINATION_HOST = DESTINATION_HOST;
this.DESTINATION_PORT = DESTINATION_PORT;
ServerSocket serverSocket = new ServerSocket(DESTINATION_PORT);
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("client Accepted with "+clientSocket.getRemoteSocketAddress());
ClientThread clientThread =
new ClientThread(clientSocket, DESTINATION_HOST, SOURCE_PORT);
clientThread.start();
}
}
示例8: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
final ServerSocket server = new ServerSocket(8080);
System.out.println("Listening for connection on port 8080 ....");
while (true) {
try (Socket socket = server.accept()) {
String response = "HTTP/1.1 200 OK\n\nHello World";
socket.getOutputStream().write(response.getBytes("UTF-8"));
socket.close();
}
}
}
示例9: startServer
import java.net.ServerSocket; //导入方法依赖的package包/类
public void startServer()
{
// create executor for server runnables
serverExecutor = Executors.newCachedThreadPool();
try // create server and manage new clients
{
// create ServerSocket for incoming connections
ServerSocket serverSocket =
new ServerSocket( SERVER_PORT, 100 );
System.out.printf( "%s%d%s", "Server listening on port ",
SERVER_PORT, " ..." );
// listen for clients constantly
while ( true )
{
// accept new client connection
Socket clientSocket = serverSocket.accept();
// create MessageReceiver for receiving messages from client
serverExecutor.execute(
new MessageReceiver( this, clientSocket ) );
// print connection information
System.out.println( "Connection received from: " +
clientSocket.getInetAddress() );
} // end while
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
}
示例10: doServerSide
import java.net.ServerSocket; //导入方法依赖的package包/类
void doServerSide() throws Exception {
ServerSocket serverSock = new ServerSocket(serverPort);
// signal client, it's ready to accecpt connection
serverPort = serverSock.getLocalPort();
serverReady = true;
// accept a connection
Socket socket = serverSock.accept();
System.out.println("Server: Connection accepted");
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
// read the bindRequest
while (is.read() != -1) {
// ignore
is.skip(is.available());
break;
}
byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
// write bindResponse
os.write(bindResponse);
os.flush();
// ignore any more request.
while (is.read() != -1) {
// ignore
is.skip(is.available());
}
is.close();
os.close();
socket.close();
serverSock.close();
}
示例11: startServer
import java.net.ServerSocket; //导入方法依赖的package包/类
@Override
public void startServer() {
try {
serverSocket = new ServerSocket(PORT, 100);
while (shouldRun) {
clientSocket = serverSocket.accept();
MultithreadedServer serverThread = new MultithreadedServer(clientSocket, this);
serverThread.start();
connections.add(serverThread);
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例12: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String[] args) {
try{
ServerSocket serverSocket = new ServerSocket(10241);
System.out.println("SocketServer is waiting");
socketService mySocket = new socketService(serverSocket.accept());
System.out.println("A client connected");
mySocket.startSocket();
mySocket.close();
}catch(IOException e) {
System.out.println(e);
}
}
示例13: run
import java.net.ServerSocket; //导入方法依赖的package包/类
@Override
public void run() {
final ServerSocket socket;
try {
socket = startServerSocket(findNextOpenPort(50));
if(socket != null){ //nullcheck if there is already a service running
//DirectStoreHandler dhs = null;
while(serverSocketKeepAlive){ //keep alive until socket should die
this.socketAcceptingClient = true;
final Socket client = socket.accept();
client.setReuseAddress(true);
client.setSoTimeout(60*1000); //one minute timeout (60 * 1000 ms)
if(!this.unblockConnection && store != null){
log.log(Level.WARNING, "Sorry there is currently a test running. I cant open up an another Store in this configuration.");
client.close();
}
if(this.storeType.equals(StoreType.DirectStore)){
store = new DirectStoreHandler(client,writerBufferSize,networkBufferSize,networkType,
writerType,filePath,getFileName(),takeNetworkMeasurements,
takeWriterMeasurements,byteTarget,measurementVolume,automaticFileRemoval);
//mBeanHelper.registerElement(dhs, dhs.getObjectName());
//the jmx implementation of the stores is for the current use-case unused.
threadPool.execute(store);
} else{
log.log(Level.WARNING,"No StoreType set!");
client.close();
}
}
socket.close();
this.socketAcceptingClient = false;
//if(dhs != null) //check if the dhs is null , for some unknown reasons
// mBeanHelper.unregisterElement(dhs.getObjectName());
}else{
log.log(Level.WARNING, "Socket is null. Is there any other service running simultaneously?");
}
} catch (IOException e) {
log.log(Level.WARNING, "IO Exception occured inside the DirectStore. Check Stacktrace",e);
}
}
示例14: main
import java.net.ServerSocket; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
for (;;) {
Socket socket = serverSocket.accept();
processReq(socket);
}
}
示例15: start
import java.net.ServerSocket; //导入方法依赖的package包/类
/**
* Starts the server
* <p/>
* Throws an IOException if the socket is already in use
*/
public void start() throws IOException {
Log.i("NanoHTTPD", "server start");
myServerSocket = new ServerSocket();
myServerSocket.bind((hostname != null) ? new InetSocketAddress(
hostname, myPort) : new InetSocketAddress(myPort));
myThread = new Thread(new Runnable() {
@Override
public void run() {
do {
try {
final Socket finalAccept = myServerSocket.accept();
Log.i("NanoHTTPD",
"accept request from "
+ finalAccept.getInetAddress());
InputStream inputStream = finalAccept.getInputStream();
OutputStream outputStream = finalAccept
.getOutputStream();
TempFileManager tempFileManager = tempFileManagerFactory
.create();
final HTTPSession session = new HTTPSession(
tempFileManager, inputStream, outputStream);
asyncRunner.exec(new Runnable() {
@Override
public void run() {
session.run();
if (finalAccept != null) {
try {
finalAccept.close();
} catch (IOException ignored) {
}
}
}
});
} catch (IOException e) {
}
} while (!myServerSocket.isClosed());
}
});
myThread.setDaemon(true);
myThread.setName("NanoHttpd Main Listener");
myThread.start();
}