Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. -- Run this file in both SV and CL to see working output.
  2.  
  3. require("bromsock")
  4.  
  5. -- SERVER EXAMPLE
  6.  
  7. if SERVER then
  8.     if server then server:Close() end
  9.     server = BromSock()
  10.    
  11.     if (not server:Listen('176.58.60.191', 6789)) then
  12.         print("[BS:S] Failed to listen!")
  13.     else
  14.         print("[BS:S] Server listening on", server:GetIP(), ":", server:GetPort())
  15.     end
  16.  
  17.     server:SetCallbackAccept(function(serversock, clientsock)
  18.         print("[BS:S] Accepted:", serversock, clientsock)
  19.        
  20.         clientsock:SetCallbackReceive(function(sock, packet)
  21.             print("[BS:S] Received:", sock, packet)
  22.             print("[BS:S] R_Num:", packet:ReadInt())
  23.            
  24.             packet:WriteString("Woop woop woop a string")
  25.             sock:Send(packet)
  26.            
  27.             -- normaly you'd want to call Receive again to read the next packet. However, we know that the client ain't going to send more, so fuck it.
  28.             -- theres only one way to see if a client disconnected, and that's when a error occurs while sending/receiving.
  29.             -- this is why most applications have a disconnect packet in their code, so that the client informs the server that he exited cleanly. There's no other way.
  30.             -- We set a timeout, so let's be stupid and hope there's another packet incomming. It'll timeout and disconnect.
  31.             sock:Receive()
  32.         end)
  33.        
  34.         clientsock:SetCallbackDisconnect(function(sock)
  35.             print("[BS:S] Disconnected:", sock)
  36.         end)
  37.        
  38.         clientsock:SetTimeout(1000) -- timeout send/recv commands in 1 second. This will generate a Disconnect event if you're using callbacks
  39.        
  40.         clientsock:Receive()
  41.        
  42.         -- Who's next in line?
  43.         serversock:Accept()
  44.     end)
  45.    
  46.     server:Accept()
  47. end
  48.  
  49. -- CLIENT EXAMPLE
  50.  
  51. if CLIENT then
  52.     if client then client:Close() end
  53.     client = BromSock()
  54.    
  55.     client:SetCallbackConnect(function(sock, ret, ip, port)
  56.         if (not ret) then
  57.             print("[BS:C] Failed to connect to: ", ret, ip, port)
  58.             return
  59.         end
  60.    
  61.         print("[BS:C] Connected to server:", sock, ret, ip, port)
  62.         local packet_client = BromPacket(client)
  63.         packet_client:WriteInt(13000)
  64.         client:Send(packet_client)
  65.        
  66.         -- we expect a response form the server after he received this, so instead of calling Receive at the connect callback, we do it here.
  67.         client:Receive()
  68.     end)
  69.  
  70.     client:SetCallbackReceive(function(sock, packet)
  71.         print("[BS:C] Received:", sock, packet, packet and packet:InSize() or -1)
  72.         print("[BS:C] R_Str:", packet:ReadString())
  73.        
  74.         -- normaly you'd call Receive here again, instead of disconnect
  75.         sock:Disconnect()
  76.     end)
  77.  
  78.     client:SetCallbackSend(function(sock, datasent)
  79.         print("[BS:C] Sent:", "", sock, datasent)
  80.     end)
  81.    
  82.  
  83.     client:Connect("127.0.0.1", 6789)
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement