TSocket::Recv hangs for ever

Hello,

I am trying to make a socket connection using TSocket and (being not at all a specialist) got into troubles
(even after trying hclient.c for example)

The problem is that after opening the connection I should get back an "OK"
e.g.:
telnet tasp 2911
Trying 129.129.138.16…
Connected to tasp.psi.ch.
Escape character is ‘^]’.
OK

Doing

root [0] TSocket *socket = new TSocket(“tasp.psi.ch”,2911)
root [1] char buf[256]
root [2] socket->Recv(buf,256)

hangs for ever.

Using the C-library works, but then I get an error when compiling (please see below)

char buffer[256];
char response[256];

// open socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// server information
serv_addr.sin_family = AF_INET;
server = gethostbyname("tasp.psi.ch");
//port
portno = 2911;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr((char *)inet_ntoa (*((struct in_addr *)server->h_addr)));
// make connection
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
    error("error cannot connect to TASP",sockfd);
}      

// read OK after connecting
bzero(buffer,256);
n = read(sockfd,buffer,255);
std::string sbuffer(buffer);

Compiling the above gives an error

Error: class,struct,union or type __socklen_t not defined /usr/include/bits/socket.h:35:

The error can be traced back to a relatively old post in roottalk: root.cern.ch/root/roottalk/roottalk06/1260.html, but the answer does not help me much…

Thanks for help,

Bertrand Roessli

[quote=“broessli”]Hello,

I am trying to make a socket connection using TSocket and (being not at all a specialist) got into troubles
(even after trying hclient.c for example)

The problem is that after opening the connection I should get back an "OK"
e.g.:
telnet tasp 2911
Trying 129.129.138.16…
Connected to tasp.psi.ch.
Escape character is ‘^]’.
OK

Doing

root [0] TSocket *socket = new TSocket(“tasp.psi.ch”,2911)
root [1] char buf[256]
root [2] socket->Recv(buf,256)

hangs for ever.
[/quote]
It hangs, because it blocks, until all 256b are read. You may want to use no-blocking sockets.
I am not sure about ROOT’s implementation, but maybe something like setting socket->SetOption(kNoBlock, kTRUE); and using socket->Select may help…

Hello,

Thanks a lot, it made the trick:

void socket()
{
TSocket *fSock = new TSocket(server,port);
fSock->SetOption(kNoBlock, 1);
fSock->Select();
char buffer[40] = {0};
fSock->RecvRaw(buffer,sizeof(buffer));
printf("%s (errno: %d)\n",buffer,gSystem->GetErrno());
fSock->Close();
}

Also I found this post which might be useful for similar problems:

Bertrand Roessli