![]() ![]() If blocking is an issue for you, check out the readpartial and read_nonblock methods from the IO class. You may want to increase or decrease the amount of data you are reading depending on what protocol you are working with. ![]() This means it will wait until there is some data available or the server closes the connection. The reason is that if there isn’t enough data to read, your program will ‘block’. There is a small problem, you might not get any data back and your app will appear to be doing nothing. You need to pass the number of bytes that you want to read from the socket as a parameter: To read the response from the server you can use the recv method. Many of the methods you will be using come from the parent classes of TCPSocket. You should now be able to send messages through your socket, you will have to follow the protocol you are communicating with for the other end to be able to understand you. This will attempt to establish a connection, if it can’t be established then you will get a Errno::ECONNREFUSED error. To create a TCP socket you can use the TCPSocket class, as parameters you will need the destination IP address and port. Now that we know what sockets can do for us it is time to start using them.įirst, require the sockets library into your program: require 'socket' The other types of sockets are a bit more esoterical, Unix sockets allow IPC (Inter-process communication) in Unix systems without the overhead of a full TCP connection. If you have to use an UDP based protocol then you can use the UDP Socket. There are a few types of sockets available to you, the most common - the TCP Socket - will allow you to make connections to TCP-based services like HTTP or FTP. Once a connection is established you can put data into your socket and it will make its way to the other end, where the receiver will read from the socket to process incoming data. Sockets are the end points of the communication channel, both clients and servers use sockets to communicate. Join me on this tour of ruby network programming to learn the basics, and start talking to other servers and clients using Ruby! Do you want to create custom network clients & servers in Ruby? Or just understand how that works? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |