Francis Cianfrocca wrote:
> Asterix Gallier wrote:
>  >
>> There is just one problem with it:
>> The call of IO.new() complains that the given FileNo is not valid and 
>> then exits with EBADF. (It's exact the code example you gave me). Is 
>> there some extra propagation of this filehandle to ruby necessary or 
>> something else?
>> 
>> With regards
>> Asterix
> 
> I just tested it on Windows 2KServer with Ruby 1.8.4(2005-12-24) and it 
> works fine. If you want to send me your code I can take perhaps a look.

I tried your code again by building a standard Ruby extension too, and 
it worked; no idea what SWIG is doing different.

But there is although the problem, that if I'm reading from the 
descriptor, and no data is there, the complete ruby application blocks. 
=(

Concerning the solution with sockets.
For single messages it works perfectly, but if there are a higher load, 
I experienced, that some Messages did not arrive in Ruby. Is there no 
receiving buffer in ruby? Have got anybody an idea.


Greetings
Asterix


/* Ruby-Code ******************************************/

require 'socket'

sThread = Thread.new do     # run server in a thread
   server = TCPServer.new('localhost', '4321')
   while(session = server.accept)
    puts "Created new Session"
     while(msg = session.read(1))
        puts "Recieved Message: #{msg}"
      end
   end
end

/* C-Code ******************************************/

#define PRINTERROR(s) fprintf(stdout,"\n%: %d\n", s, WSAGetLastError())

SOCKET	theSocket;
SOCKADDR_IN saServer;
#define SERVERPORT 4321


void createTCPSocket() {

	char host[] = "localhost";

	LPHOSTENT lpHostEntry;

	WORD wVersionRequested = MAKEWORD(1,1);
	WSADATA wsaData;

	WSAStartup(wVersionRequested, &wsaData);
	if (wsaData.wVersion != wVersionRequested) {
		fprintf(stderr,"\n Wrong version\n");
		return;
	}

	// Find the server
	lpHostEntry = gethostbyname(host);
	if (lpHostEntry == NULL)
	{
		PRINTERROR("gethostbyname()");
		return;
	}

	// Create a TCP/IP datagram socket
	if ( (theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == 
INVALID_SOCKET) {
		PRINTERROR("socket()");
		return;
	}

	// Fill in the address structure for the server
	saServer.sin_family = AF_INET;
	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
	saServer.sin_port = htons(SERVERPORT);

	// connect to the server
	int nRet;

	if( (nRet = connect(theSocket,
		(LPSOCKADDR)&saServer,
		sizeof(struct sockaddr))) == SOCKET_ERROR) {
			PRINTERROR("socket()");
			closesocket(theSocket);
			return;
	}

}

void sendTCPData(char *data) {
	int nRet;

	nRet = send(theSocket,				// Connected socket
		data,					// Data buffer
		strlen(data),			// Length of data
		0);						// Flags

	if (nRet == SOCKET_ERROR) {
		PRINTERROR("send()");
		closesocket(theSocket);
		return;
	}
}

-- 
Posted via http://www.ruby-forum.com/.