Swfit调用C实现socket白屏 财富值97

2016-10-22 16:50发布

如题,本来成功了的,不知道改错了哪里就一直白屏了。。。大家帮忙看看
分三个文件

  1. Scene.swift

  2. tcp_IPv4_Client.swift

  3. tcp_IPv4_Client.c


// Scene.swift // ... // ... func Socket()->Bool{ let Socket:tcp_IPv4_Client = tcp_IPv4_Client(addr: "192.168.0.106", port: 45892)     if (Socket.connect(timeout: 5) == false){         return false     }     if (Socket.send(str:"_2345678_") == false){         return false     }     if let Received = Socket.read(10){         if let Message_Received = String(bytes: Received, encoding: String.Encoding.utf8){             print ("-------------------------------------------------")             print (Message_Received)             print ("-------------------------------------------------")         }     }else {         return false     }     _ = Socket.close() return true } // ... // ... 

// tcp_IPv4_Client.swift import Foundation @_silgen_name("tcp_IPv4_connect") func c_tcp_IPv4_connect(_ IP:UnsafePointer<Int8>,port:Int32,timeout:Int32)->Int32 @_silgen_name("tcp_IPv4_close")   func c_tcp_IPv4_close(_ Given_Socket:Int32) @_silgen_name("tcp_IPv4_send")    func c_tcp_IPv4_send(_ Given_Socket:Int32, buff:UnsafePointer<UInt8>, len:Int32)->Int32 @_silgen_name("tcp_IPv4_receive") func c_tcp_IPv4_receive(_ Given_Socket:Int32, buff:UnsafePointer<UInt8>, len:Int32,timeout:Int32) -> Int32   open class tcp_IPv4_Client:tcp_IPv4_Client_Master{     open func connect(timeout t:Int)->Bool{         self.Given_Socket = c_tcp_IPv4_connect(self.addr, port: Int32(self.port), timeout: Int32(t))         if (self.Given_Socket == -1){             return false         }         return true     }                    open func close(){         c_tcp_IPv4_close(self.Given_Socket!)     }                   open func send(str s:String)->Bool{         if (c_tcp_IPv4_send(self.Given_Socket!, buff: s, len: Int32(strlen(s))) == -1){             return false         }         return true     }                    open func read(_ expectlen:Int, timeout:Int = -1)->[UInt8]?{         var buff:[UInt8]    = [UInt8](repeating: 0x0, count: expectlen)         let Receiver:Int32  = c_tcp_IPv4_receive(self.Given_Socket!, buff: &buff, len: Int32(expectlen), timeout: Int32(timeout))         if (Receiver == -1){             return nil         }         let rs=buff[0...Int(Receiver-1)]         let data:[UInt8] = Array(rs)         return data     } }    open class tcp_IPv4_Client_Master{     var addr:String     var port:Int     var Given_Socket:Int32?     init(){         self.addr   = ""         self.port   = 0     }     public init(addr a:String,port p:Int){         self.addr   = a         self.port   = p     } }

// tcp_IPv4_Client.c #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <netinet/in.h> #include <dirent.h> #include <netdb.h> #include <unistd.h> #include <fcntl.h> #include <signal.h>  #include <sys/select.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/types.h>     int tcp_IPv4_connect(char *IP, int PORT, int Timeout){     int Socket = socket(AF_INET, SOCK_STREAM, 0);     if (Socket == -1){         return -1;     }                    struct timeval TIMEOUT;     TIMEOUT.tv_sec  = Timeout;     TIMEOUT.tv_usec = 0;     if (setsockopt(Socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&TIMEOUT, sizeof(TIMEOUT)) == -1){         close(Socket);         return -1;     }     if (setsockopt(Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&TIMEOUT, sizeof(TIMEOUT)) == -1){         close(Socket);         return -1;     }                    struct sockaddr_in Socket_Info;     Socket_Info.sin_family      = AF_INET;     Socket_Info.sin_addr.s_addr = inet_addr(IP);     Socket_Info.sin_port        = htons(PORT);                    if (connect(Socket, (struct sockaddr *)&Socket_Info, sizeof(Socket_Info)) == -1){         close(Socket);         return -1;     }     return Socket; }    int tcp_IPv4_close(int Socket){     close(Socket);     return 0; }    int tcp_IPv4_receive(int Socket, char *Message, int Message_Length){     int Receiver = (int)read(Socket, Message, Message_Length);     if (Receiver == -1){         close(Socket);         return -1;     }     return Receiver; }    int tcp_IPv4_send(int Socket, char *Message,int Message_Length){     int Sender = (int)write(Socket, Message, Message_Length);     if (Sender == -1) {         close(Socket);         return -1;     }     return Sender; }
友情提示: 问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
0条回答

一周热门 更多>