Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BlueSocket表示连接到Python套接字时连接被拒绝_Python_Ios_Swift_Sockets_Tcp - Fatal编程技术网

BlueSocket表示连接到Python套接字时连接被拒绝

BlueSocket表示连接到Python套接字时连接被拒绝,python,ios,swift,sockets,tcp,Python,Ios,Swift,Sockets,Tcp,对于我正在制作的iOS应用程序,它需要连接到python套接字服务器。对于我使用的iOS应用程序Swift 3,当我尝试使用正确的IP和端口连接到服务器时,它总是返回连接被拒绝,并且在服务器上看不到连接。BlueSocket被设置为TCP客户端,python服务器也是TCP服务器,所以我不明白为什么它不工作 有人知道/理解它为什么无法连接到服务器吗 iOS swift代码如下: import UIKit import Foundation import SystemConfiguration i

对于我正在制作的iOS应用程序,它需要连接到python套接字服务器。对于我使用的iOS应用程序Swift 3,当我尝试使用正确的IP和端口连接到服务器时,它总是返回连接被拒绝,并且在服务器上看不到连接。BlueSocket被设置为TCP客户端,python服务器也是TCP服务器,所以我不明白为什么它不工作

有人知道/理解它为什么无法连接到服务器吗

iOS swift代码如下:

import UIKit
import Foundation
import SystemConfiguration
import SystemConfiguration.CaptiveNetwork
import Socket

let ssid = "SSID"
let server = "192.168.0.24"
let port = 8000

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBOutlet weak var ideaLabel: UILabel!

@IBAction func generateButton(_ sender: Any) {

    func isInternetAvailable() -> Bool
    {
        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)

        let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
            }
        }

        var flags = SCNetworkReachabilityFlags()
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
            return false
        }
        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
        return (isReachable && !needsConnection)
    }
    func getSSID() -> String? {
        var ssid: String?
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                    ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                    break
                }
            }
        }
        return ssid
    }
    func generateIdea() -> String? {
        let RNumber = arc4random_uniform(23)


        var idea = ""

        //(A bunch of code to get a idea for offline mode)

        return String(idea)
    }



    let InternetConnect = isInternetAvailable()


    if (InternetConnect == isInternetAvailable()){
        print("Internet Connection Verified")
        if (getSSID() == ssid){
            print("SSID Verified")

            let s = try! Socket.create()
            try! s.connect(to: server, port: 8000)
            print("Connceted to server")

            try! s.write(from: "01")
            print("String send")

            let idea = try! s.readString()
            print("Idea Received")
            ideaLabel.text = idea

            s.close()
        }
        else{

            print("SSID Verification Failed")
            ideaLabel.text = generateIdea()
        }

    }
    else {

        ideaLabel.text = generateIdea()
        print("Internet Connection Verification Failed")

    }
}
}

swift客户说明:

当点击按钮生成创意时,应用程序首先检查是否可以连接到internet,如果可以,然后通过检查SSID(服务器将是lan服务器,因此仅在设置的网络上工作)检查其是否在设置的internet网络上,如果无法连接到internet或其不在正确的网络上,它从应用程序中存储的有限数量的想法中选择一个。如果它可以连接到internet,并且它位于正确的internet网络上,那么它将尝试连接到服务器,并从服务器获得想法

python套接字服务器代码:

import socket
import sqlite3
from random import randint

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 8000)
print("Starting Server")
s.bind(server_address)
print("Waiting for client")
s.listen(1)
alive = True

while(alive == True):
    connection, client_address = s.accept()
    print(client_address)

    try:
        command = connection.recv(2)

        if (command == "01"):
            conn = sqlite3.connect('ideas.db')
            c = conn.cursor()
            file = open("maxID.txt", "r")
            maxID = (int(file.read()) + 1)
            ideaNumber = (randint(1,maxID),)
            c.execute('SELECT * FROM ideas WHERE id=?', ideaNumber)
            idea1 = c.fetchone()
            idea = str(idea1)
            conn.close()
            idea = idea.translate(None, "1234567890'(),u")
            print("Your idea is:")
            print(idea)
            connection.send(str(idea))

    finally:
        connection.close()
python套接字服务器的说明:


当服务器获取客户机时,它会等待获取命令。在这个服务器中,只有一个命令,这是为了得到一个想法。该命令是“01”。当它得到这个命令时,它要做的第一件事就是获取存储在数据库中的想法的数量。这将写入maxID.txt文件,因此它会读取它。然后随机选择一个idea ID,然后使用该ID从数据库中获取该idea。然后将该idea发送给客户端。

问题出在python服务器上。问题是我将主机设置为localhost,而不是将其留空

这:

应该是:

server_address = ('', 8000)

问题出在python服务器上。问题是我将主机设置为localhost,而不是将其留空

这:

应该是:

server_address = ('', 8000)