0001    /**
0002     * Copyright IBM Corporation 2016
0003     *
0004     * Licensed under the Apache License, Version 2.0 (the "License");
0005     * you may not use this file except in compliance with the License.
0006     * You may obtain a copy of the License at
0007     *
0008     * http://www.apache.org/licenses/LICENSE-2.0
0009     *
0010     * Unless required by applicable law or agreed to in writing, software
0011     * distributed under the License is distributed on an "AS IS" BASIS,
0012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     * See the License for the specific language governing permissions and
0014     * limitations under the License.
0015     **/
0016    
0017    /**
0018    * Creates a simple HTTP server that listens for incoming connections on port 9080.
0019    * For each request receieved, the server simply sends a simple hello world message
0020    * back to the client.
0021    **/
0022    
0023    #if os(Linux)
0024    import Glibc
0025    #else
0026    import Darwin
0027    #endif
0028    import Utils
0029    
0030    // Create server socket
0031    let address
main.swift:32
let server_sockfd = createSocket(address)
main.swift:36
print("Server is listening on port: \(address.port)\n")
= parseAddress() 0032 let server_sockfd
main.swift:34
listen(server_sockfd, 5)
main.swift:39
fdSet(server_sockfd, set: &active_fd_set)
main.swift:55
      if i == server_sockfd {
main.swift:60
          var client_sockfd = accept(server_sockfd,
= createSocket(address) 0033 // Listen on socket with queue of 5 0034 listen(server_sockfd, 5) 0035 var active_fd_set
main.swift:39
fdSet(server_sockfd, set: &active_fd_set)
main.swift:50
  var read_fd_set = active_fd_set;
main.swift:64
            fdSet(client_sockfd, set: &active_fd_set)
main.swift:72
        fdClr(i, set: &active_fd_set)
= fd_set() 0036 print("Server is listening on port: \(address.port)\n") 0037 0038 // Initialize the set of active sockets 0039 fdSet(server_sockfd, set: &active_fd_set) 0040 0041 let FD_SETSIZE
main.swift:51
  select(FD_SETSIZE, &read_fd_set, nil, nil, nil)
main.swift:53
  for i in 0..<FD_SETSIZE {
= Int32(1024) 0042 0043 let httpResponse
main.swift:69
        write(i, httpResponse, httpResponse.characters.count)
main.swift:69
        write(i, httpResponse, httpResponse.characters.count)
= "HTTP/1.0 200 OK\n" + 0044 "Content-Type: text/html\n\n" + 0045 "<html><body>Hello from Swift on Linux!</body></html>" 0046 0047 var clientname
main.swift:59
        withUnsafeMutablePointers(&clientname, &size) { up1, up2 in
main.swift:63
            print("Received connection request from client: " + String(inet_ntoa (clientname.sin_addr)) + ", port " + String(UInt16(clientname.sin_port).bigEndian))
main.swift:63
            print("Received connection request from client: " + String(inet_ntoa (clientname.sin_addr)) + ", port " + String(UInt16(clientname.sin_port).bigEndian))
= sockaddr_in() 0048 while true { 0049 // Block until input arrives on one or more active sockets 0050 var read_fd_set = active_fd_set; 0051 select(FD_SETSIZE, &read_fd_set, nil, nil, nil) 0052 // Service all the sockets with input pending 0053 for i in 0..<FD_SETSIZE { 0054 if fdIsSet(i,set: &read_fd_set) { 0055 if i == server_sockfd { 0056 // Connection request on original socket 0057 var size = sizeof(sockaddr_in) 0058 // Accept request and assign socket 0059 withUnsafeMutablePointers(&clientname, &size) { up1, up2 in 0060 var client_sockfd = accept(server_sockfd, 0061 UnsafeMutablePointer(up1), 0062 UnsafeMutablePointer(up2)) 0063 print("Received connection request from client: " + String(inet_ntoa (clientname.sin_addr)) + ", port " + String(UInt16(clientname.sin_port).bigEndian)) 0064 fdSet(client_sockfd, set: &active_fd_set) 0065 } 0066 } 0067 else { 0068 // Send HTTP response back to client 0069 write(i, httpResponse, httpResponse.characters.count) 0070 // Close client socket 0071 close(i) 0072 fdClr(i, set: &active_fd_set) 0073 } 0074 } 0075 } 0076 } 0077