BlueSocket说连接连接到Python插座插座时拒绝



对于我正在制作的iOS应用程序,它需要连接到Python套接字服务器。对于iOS应用程序,我使用的是Swift 3,并且在尝试使用正确的IP和端口连接到服务器时,我正在使用BlueSocket,它总是返回连接的连接,并且我看不到服务器上的连接。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(服务器是SSID(在设置的Internet网络上查看将成为LAN服务器,因此仅在设置网络上使用(如果它无法连接到Internet或在正确的网络上连接,则它将从应用程序上存储的有限想法中选择a。如果它可以连接到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插座服务器的说明:

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

问题在Python服务器上。问题是我将主机设置为Local主机,而不是将其留为空白。

这个:

server_address = ('localhost', 8000)

应该是:

server_address = ('', 8000)

最新更新