Created
December 25, 2016 10:03
-
-
Save miketheman/e024f69879fa4ef52653f90492ca1c79 to your computer and use it in GitHub Desktop.
Crystal Lang 0.20.3 Socket::IPAddress change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ crystal --version | |
Crystal 0.20.1 (2016-12-05) | |
$ crystal run test.cr | |
1482659875.246: 127.0.0.1:55157 "This is only a test." | |
$ crystal --version | |
Crystal 0.20.3 (2016-12-23) | |
$ crystal run test.cr | |
Error in test.cr:10: wrong number of arguments for 'Socket::IPAddress.new' (given 3, expected 2) | |
Overloads are: | |
- Socket::IPAddress.new(address : String, port : Int32) | |
- Socket::IPAddress.new(sockaddr : ::Pointer(LibC::SockaddrIn6), size) | |
- Socket::IPAddress.new(sockaddr : ::Pointer(LibC::SockaddrIn), size) | |
destination = Socket::IPAddress.new(Socket::Family::INET, host, port) | |
^~~ | |
# Modify L10 to: | |
# destination = Socket::IPAddress.new(host, port) | |
$ crystal run test.cr | |
Invalid IP address: localhost (Socket::Error) | |
0x10c2c42d2: *CallStack::unwind:Array(Pointer(Void)) at ?? | |
0x10c2c4271: *CallStack#initialize:Array(Pointer(Void)) at ?? | |
0x10c2c4248: *CallStack::new:CallStack at ?? | |
0x10c2c36e1: *raise<Socket::Error>:NoReturn at ?? | |
0x10c2f9c8b: *Socket::IPAddress#initialize<String, Int32>:Int32 at ?? | |
0x10c2f9b01: *Socket::IPAddress::new<String, Int32>:Socket::IPAddress at ?? | |
0x10c2bee47: __crystal_main at ?? | |
0x10c2c2d78: main at ?? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "socket" | |
host = "localhost" | |
port = 1234 | |
server = UDPSocket.new | |
server.bind(host, port) | |
client = UDPSocket.new | |
destination = Socket::IPAddress.new(Socket::Family::INET, host, port) | |
client.send("This is only a test.", destination) | |
message = Slice(UInt8).new(1432) | |
message_size, message_source = server.receive(message) | |
text = message[0, message_size] | |
output = (String.new(text)).inspect | |
puts "#{Time.now.to_s("%s.%L")}: #{message_source.address}:#{message_source.port} #{output}" | |
server.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment