Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdexcept>
- #include <string.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
- using namespace std;
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- std::cout << "not enough parameters" << std::endl;
- }
- const std::string iface = argv[1];
- int fd = open(iface.c_str(), O_RDWR);
- if (fd < 0)
- throw std::runtime_error(
- std::string("Could not open device: ") + iface);
- // close(fd);
- // return 0;
- unsigned long writeCount = 0;
- char buffer[256];
- while (true) {
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 20000;
- fd_set rfds;
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- fd_set wfds;
- FD_ZERO(&wfds);
- FD_SET(fd, &wfds);
- int retval = select(fd + 1, &rfds, &wfds, NULL, &tv);
- if (retval == -1)
- std::cout << "select error" << std::endl;
- else if (retval) {
- if( FD_ISSET(fd, &rfds)) {
- int readCount = read(fd, buffer, 256);
- if (readCount == -1) {
- std::cout << "Read Error: " << errno << " - " << strerror(errno)
- << std::endl;
- }
- else {
- std::cout << "Read " << readCount << std::endl;
- }
- }
- } else {
- std::cout << "Timeout" << std::endl;
- }
- std::string writeData = "abcdefg";
- int writeRc = write(fd, writeData.c_str(), writeData.size());
- if (writeRc == -1) {
- std::cout << "Write Error: " << errno << " - " << strerror(errno)
- << std::endl;
- } else
- {
- std::cout << "Write OK #" << writeCount++ << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement