osx - Socket Server Example with Swift -
I tried to make an example of simple socket server.
Build and run successfully though it does not work well.
The client could not connect to this server.
How to solve this problem? I need your help, thank you
Import the Foundation to BUFF_SIZE = 1024 function antstread & lt; S & gt; () - & gt; S {let struct_pointer = unsafePointer & lt; S & gt; .alloc (1) let struct_memory = struct_pointer.memory struct_pointer.destroy () return struct_memory} func sockaddr_cast (p: ConstUnsafePointer & lt; sockaddr_in & gt;) - & gt; UnsafePointer & LT; Sockaddr & gt; {Return unsecured pointer & lt; Socadr & gt; (P)} Faken Socon_t_cast (page: insecure pointer & lt; int & gt;) - & gt; UnsafePointer & LT; Socklen_t & gt; {Return UnsafePointer & lt; Socklen_t & gt; (P)} var server_socket: Int32 var client_socket: int32 var server_addr_size: int var client_addr_size: int var server_addr: sockaddr_in = initStruct () var client_addr: sockaddr_in = initStruct () var buff_rcv: aa < CChar & gt; = [] Var buff_snd: string server_socket = socket (PF_INET, SOCK_STREAM, 0); If server_socket == -1 {println ("[Create failed server socket"]) exit (1)} other {println ("[server] socket created [success]")} server_addr_size = sizeof (server_addr.dynamicType) memset (& server_addr , 0, UInt (server_addr_size)); Server_addr.sin_family = sa_family_t (AF_INET) server_addr.sin_port = 4000 server_addr.sin_addr.s_addr = UInt32 (0x00000000) // INADDR_ANY = (u_int32_t) 0x00000000 ----- & lt; Netinet / in.h> Bind_server = bind (server_socket, sockaddr_cast (& server_addr), socklen_t (server_addr_size)) Bind_services == -1 {println ("[failed] port off"); Exit (1); } Other {println ("[success] off port"); } If you listen (server_socket, 5) == -1 {println ("[fail] listen"); Exit (1); } Else {println ("[Success] Listening: \ (server_addr.sin_port) Port ..."); } Var n = 0 while n & lt; 1 {client_addr_size = sizeof (client_addr.dynamicType) client_socket = accept (server_socket, sockaddr_cast (& amp; client_addr), socklen_t_cast (& amp; client_addr_size)) if client_socket == -1 {println ("" [accept] Client connections accept "); Exit (1); } Other {println ("[Success] Approved Customers: \ (inet_ntoa (client_addr.sin_addr)): \ (client_addr.sin_port)"); } "(Client_socket, and buff_rcv, UInt (BUFF_SIZE)) println (" [Success] received: \ (buff_rcv) ") buff_snd =" \ (strlen (buff_rcv)): \ (buff_rcv) "type (client_socket, & amp; Buff_snd, strlen (buff_snd) + 1) Close (client_socket)}
port number Large-endian byte in socket address must be in the order:
server_addr.sin_port = UInt16 (4000) .bigEndian
then your program actually listens Not at port 40975 (hex 0xA00F) and port 4000 (hex 0x0FA0)
Another problem is here:
var buff_rcv: array & lt; CChar & gt; = [] // ... read (client_socket, and buff_rcv, UInt (BUFF_SIZE))
Your buffer is an empty array, but Recv ()
requires a buffer of size BUFF_SIZE
The behavior is undefined. To get a buffer of the required size, use
var buff_rcv = [CChar] (count: BUFF_SIZE, repealedValue: 0) // ... read (client_socket, and buff_rcv, UInt (
/ code> andsocklen_t
accept ()
function:client_socket = accept (server_socket, sockaddr_cast (& Amp; client_addr), socklen_t_cast (near & amp; client_addr_size)
This is not secure if
int
andsocklen_t
If there are different sizes, the behavior will be undefined. You declaredserver_addr_size
andclient_addr_size
assocklen_t
. And should remove thesocklen_t_cast ()
function:client_socket = Accept (server_socket, sockaddr_cast (& amp; client_addr), & amp; client_addr_size)
Comments
Post a Comment