Sockets won't block
%u
gdefty at attglobal.net
Thu Aug 31 09:37:02 PDT 2006
When I open sockets they don't seem to like blocking, and return immediately
when I do a 'receive' (with 0 bytes of data of course)
The routine (client and server in 1) is as follows (excuse all the tracing
'writefln's and a few artifacts from the original C++ version :-)
---- code ----
// $Header: /home/CVS/atm/ATMsock_t.cpp,v 1.1 1980/04/16 23:53:02 graeme Exp $
// $Header: /home/CVS/atm/ATMsock_t.cpp,v 1.1 1980/04/16 23:53:02 graeme Exp $
// Author: Graeme Defty
// $Date: 1980/04/16 23:53:02 $
/*
This module runs tests on the ATM Socket class.
*/
static const char pgmid[] =
"$Id: ATMsock_t.cpp,v 1.1 1980/04/16 23:53:02 graeme Exp $";
// $Source: /home/CVS/atm/ATMsock_t.cpp,v $ //
//#include <fstream>
//#include <signal.h>
//#include <unistd.h>
import std.c.time;
import std.stdio;
import std.string;
import std.socket;
//#include "ATMsock.h"
//#include "assure.h"
// ============ Don't think we need these any more ==========
//static void
//sig_h(int signo) {
// cout << "Caught signal " << signo << endl;
// if (signo == SIGINT) exit(0);
//}
// ========================================================
int
main (char[][] args) {
int portnum = 1234;
char [] buf;
char [] rcvbuf;
Socket mySock;
InternetAddress myAddr, farAddr;
int len;
int count;
// struct sigaction sigact;
// struct sigaction oldsigact;
// first handle the arguments . . .
if (args.length>2) {
portnum = atoi(args[2]);
}
writefln("Using port number " , portnum , ".");
// === Signal handling seems to be unneccessary now ====
// then set up signal handling . . .
// sigact.sa_handler = sig_h;
// sigemptyset (&sigact.sa_mask);
// sigact.sa_flags = 0;
// for (len=1; len<48; len++) {
// if (sigaction(len, &sigact, &oldsigact) == -1) {
// perror ("Catching Signal");
// } else {
// cout << "Signal " << len << " set." << endl;
// }
// }
if (args.length>1 && args[1] == "s") {
writefln("Test begins for server . . .");
myAddr = new InternetAddress("127.0.0.1", portnum);
mySock = new Socket(AddressFamily.INET, SocketType.STREAM);
writefln("Server created . . .");
mySock.bind(myAddr);
writefln(". . . and bound . . .");
mySock.listen(5);
writefln(". . . and listening.");
while (true) {
Socket otherSock = mySock.accept();
assert(otherSock.isAlive);
writefln("Accepted a connection . . .");
writefln("Far address is ", otherSock.remoteAddress);
otherSock.blocking = true;
assert(otherSock.isAlive);
sleep(1);
len = otherSock.receive(buf);
assert(len != Socket.ERROR);
assert(otherSock.isAlive);
writefln("Received data (", len , ") '", buf , "'.");
otherSock.send(buf);
assert(otherSock.isAlive);
writefln("Sent that data . . .");
// mySock.close;
// writefln(". . . and closed the socket.");
}
} else {
writefln("Test begins for client . . .");
myAddr = new InternetAddress("127.0.0.1", portnum);
farAddr = new InternetAddress("127.0.0.1", 1234);
for (count = 0; count < 12 ; count++) {
if (count % 1000 == 0) {writefln("Done " , count);}
mySock = new Socket(AddressFamily.INET, SocketType.STREAM);
assert(mySock.isAlive);
writefln("Client created . . .");
mySock.bind(myAddr);
assert(mySock.isAlive);
writefln(". . . and bound . . .");
mySock.connect(farAddr);
assert(mySock.isAlive);
writefln(". . . and connected.");
writefln("Far address is ", mySock.remoteAddress);
buf = format("This is data line ", count);
len = mySock.send(buf);
assert(mySock.isAlive);
writefln("Sent (", len , ") '", buf , "' and received '", rcvbuf , "'.");
len = mySock.receive(rcvbuf);
assert(mySock.isAlive);
// assert (buf == rcvbuf, "Sent and received data different!");
if (buf != rcvbuf) {
writefln("ERROR: Sent '", buf , "' and received '", rcvbuf , "'.");
}
mySock.close();
sleep(10);
}
}
writefln("Test Done!");
return 0;
}
---- end code ----
I would appreciate any guidance.
Thanks,
graeme
More information about the Digitalmars-d-learn
mailing list