swiftsocket / SwiftSocket

The easy way to use sockets on Apple platforms
BSD 3-Clause "New" or "Revised" License
1.68k stars 401 forks source link

read nothing #128

Closed Time-Jang closed 6 years ago

Time-Jang commented 6 years ago

I test it by c language and it worked. But in swift, using swift socket, It won't work. I want to get a xml text. but swiftsocket has no response.

let song_title: String = "yesterday"
        let song_singer: String = "The Beatles"
        let msg_1: String = "POST /alsongwebservice/service1.asmx HTTP/1.1\r\nHost: lyrics.alsong.co.kr\r\nUser-Agent: gSOAP/2.7\r\nContent-Type:application/soap+xml;charset=utf-8\r\nContent-Length:"
        let msg_2: String = "\r\nConnection: close\r\nSOAPAction: \"ALSongWebServer/GetLyric5\"\r\n\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:SOAP-ENC=\"http://www.w3.org/2003/05/soap-encoding\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns2=\"ALSongWebServer/Service1Soap\" xmlns:ns1=\"ALSongWebServer\" xmlns:ns3=\"ALSongWebServer/Service1Soap12\"><SOAP-ENV:Body><ns1:GetResembleLyric2><ns1:stQuery><ns1:strTitle>"
        let msg_3: String = "</ns1:strTitle><ns1:strArtistName>"
        let msg_4: String = "</ns1:strArtistName><ns1:nCurPage>0</ns1:nCurPage></ns1:stQuery></ns1:GetResembleLyric2></SOAP-ENV:Body></SOAP-ENV:Envelope>"
        // getting Lyric from alsong with socket programming
        guard let client = client else { return }
        switch client.connect(timeout:10){
        case .success:
            let total_len: Int = msg_2.count + msg_3.count + msg_4.count + song_title.count + song_singer.count
            let message : String = msg_1+String(total_len)+msg_2+song_title+msg_3+song_singer+msg_4
            switch client.send(string: message){
            case .success:
                print(8)
                guard let data = client.read(1024*100, timeout:5)
                    else { return }
                print(9)
                if let response = String(bytes: data, encoding: .utf8){
                    Lyric.text = response
                    print(10)
                }
            case .failure(let error):
                print(error)
                print(-2)
            }
        case .failure(let error):
            print(error)
            print(-3)
        }

If It is not enough, you can get whole project file https://github.com/Time-Jang/LyricRightNow. Help me plz..

Time-Jang commented 6 years ago

I just use URLRequest.