Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "ibrcommon/config.h"
00009 #include "ibrcommon/net/tcpclient.h"
00010 #include <sys/types.h>
00011 #include <sys/socket.h>
00012 #include <sys/un.h>
00013 #include <netdb.h>
00014 #include <unistd.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018
00019 #include <streambuf>
00020 #include <netinet/in.h>
00021 #include <arpa/inet.h>
00022 #include <sstream>
00023
00024 namespace ibrcommon
00025 {
00026 tcpclient::tcpclient()
00027 {
00028 }
00029
00030 tcpclient::tcpclient(const ibrcommon::File &s)
00031 {
00032 open(s);
00033 }
00034
00035 void tcpclient::open(const ibrcommon::File &s)
00036 {
00037 int len = 0;
00038 struct sockaddr_un saun;
00039
00040
00041
00042
00043
00044
00045 if ((_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
00046 throw SocketException("Could not create a socket.");
00047 }
00048
00049
00050
00051
00052 saun.sun_family = AF_UNIX;
00053 strcpy(saun.sun_path, s.getPath().c_str());
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 len = sizeof(saun.sun_family) + strlen(saun.sun_path);
00066
00067 if (connect(_socket, (struct sockaddr *)&saun, len) < 0) {
00068 throw SocketException("Could not connect to the named socket.");
00069 }
00070 }
00071
00072 tcpclient::tcpclient(const string &address, const int port)
00073 {
00074 open(address, port);
00075 }
00076
00077 void tcpclient::open(const string &address, const int port)
00078 {
00079 struct addrinfo hints;
00080 memset(&hints, 0, sizeof(struct addrinfo));
00081 hints.ai_family = PF_UNSPEC;
00082 hints.ai_socktype = SOCK_STREAM;
00083
00084 struct addrinfo *res;
00085 int ret;
00086
00087 std::stringstream ss; ss << port; std::string port_string = ss.str();
00088
00089 if ((ret = getaddrinfo(address.c_str(), port_string.c_str(), &hints, &res)) != 0)
00090 {
00091 throw SocketException("getaddrinfo(): " + std::string(gai_strerror(ret)));
00092 }
00093
00094 if (res == NULL)
00095 {
00096 throw SocketException("Could not connect to the server.");
00097 }
00098
00099 struct addrinfo *walk;
00100 for (walk = res; walk != NULL; walk = walk->ai_next) {
00101 _socket = socket(walk->ai_family, walk->ai_socktype, walk->ai_protocol);
00102 if (_socket < 0){
00103
00104
00105 if (walk->ai_next == NULL)
00106 {
00107 throw SocketException("Could not create a socket.");
00108 }
00109 continue;
00110 }
00111 if (connect(_socket, walk->ai_addr, walk->ai_addrlen) != 0) {
00112 ::close(_socket);
00113 _socket = -1;
00114
00115 if (walk->ai_next == NULL)
00116 {
00117 throw SocketException("Could not connect to the server.");
00118 }
00119 continue;
00120 }
00121 break;
00122 }
00123
00124 freeaddrinfo(res);
00125 }
00126
00127 tcpclient::~tcpclient()
00128 {
00129 }
00130 }