Re: Linux sockets

new topic     » goto parent     » topic index » view thread      » older message » newer message

On  0, Peter.Blue at legend-i.demon.co.uk wrote:
> Hello everyone,
> 
> Does anyone know how to send text/data through Linux sockets using 
> Euphoria ?
> 
> Regards

Yes, but I wouldnt recommend doing it directly if you are new to it.

RDC.e is a good lib to look at, also, take a look at this simple
socket lib by Irv Mullins: (personally, I'd like NONBLOCKING
gethostbyname though - probably have to use emulated threads for that
tho)

---Cut Here---
-- Simple Sockets Lib by jbrown
-- Access to unix sockets and internet protocols
-- based on Simple Sockets Demo by Irv Mullins

include get.e
include dll.e
include misc.e
include machine.e
without warning

constant DEBUG = 0 -- 0 = off, 1 = on

constant
   AF_UNIX = 1, AF_INET = 2,	    -- DOMAINS;
   SOCK_STREAM = 1, SOCK_DGRAM =2,  -- SOCKET TYPES;
   F_SETFL = 4, O_NONBLOCK = 04000  -- PORT MODE PARAMS;

-- Links to routines in the socks library

constant
lib = open_dll(""),
fcntl = define_c_func(lib,"fcntl",{C_POINTER,C_INT,C_INT},C_POINTER),
sock  = define_c_func(lib,"socket",{C_INT,C_INT,C_INT},C_POINTER),
htons = define_c_func(lib,"htons",{C_USHORT},C_USHORT),
connect = define_c_func(lib,"connect",{C_INT,C_POINTER,C_INT},C_INT),
read_ = define_c_func(lib,"read",{C_INT,C_POINTER,C_INT},C_INT),
write_ = define_c_func(lib,"write",{C_INT,C_POINTER,C_INT},C_INT),
close_ = define_c_proc(lib,"close",{C_INT}),
gethostbyname = define_c_func(lib,"gethostbyname",{C_POINTER},C_POINTER),
getservbyname =
define_c_func(lib,"getservbyname",{C_POINTER,C_POINTER},C_POINTER)

function deallocate_string(atom str)
	object result
	integer i
	i = 0
	result = ""
	while peek(str+i) != 0 do
		result &= peek(str+i)
	i += 1
	end while
	return result
end function

global function SockRead(atom sockfd)
	atom buffer, char, bytes_read
	object line
	buffer = allocate(1)
	line = ""
	while 1 do
		bytes_read = c_func(read_,{sockfd,buffer,1})
		if bytes_read < 1 then
			exit
		else
			char = peek(buffer)
			if char = 10 then
				char = '\n'
			elsif char = 13 then
				char = ' '
			end if
			line &= char
		end if
	end while
	if DEBUG then
		puts(DEBUG,"READ: "&line&'\n')
	end if
	return line
end function

global procedure SockWrite(atom sockfd, sequence string)
	atom str, fn
	string &= 13&10
	str = allocate_string(string)
	fn = c_func(write_,{sockfd,str,length(string)})
	if DEBUG then
		puts(DEBUG,"WRITE: "&string&'\n')
	end if
	free(str)
end procedure

global procedure SockClose(atom sockfd)
	c_proc(close_,{sockfd})
end procedure

global function GetHostbyName(sequence hostname) 
-- Returns {name, #addr, node}
	object host
	atom str, fn
	object addr
-- host info structure is;
	object name,
			aliases,
			addrtype,
			length,
			node

	str = allocate_string(hostname)
	host = c_func(gethostbyname,{str})
	free(str)

	if host > 0 then
		name = deallocate_string(peek4u(host))
		length = peek4u(host+12)
		addr = peek4u(peek4u(host+16))
		node = peek4u(addr)
		addr = sprintf("%d.%d.%d.%d",peek({addr,length}))
		return {name,addr,node}
	else
		return -1
	end if
end function

global function GetServbyName(sequence service, sequence proto)
-- Returns {name, #port, protocol}
	atom str1, str2, fn
-- server info structure;
	object name,
			port,
			protocol

	str1 = allocate_string(service)
	str2 = allocate_string(proto)
	fn = c_func(getservbyname,{str1,str2})
	free(str2)
	free(str1)

	if fn > 0 then
		name = deallocate_string(peek4u(fn))
		port = peek({fn+8,4})
		port = port[1]*256+port[2]
		protocol = deallocate_string(peek4u(fn+12))
		return {name,port,protocol}
	else
		return -1
	end if
end function

global function SockConnect(atom hostnode, atom port)
	atom sockaddr, SOCKET

	SOCKET = c_func(sock,{AF_INET, SOCK_STREAM,0})
	if SOCKET < 0 then
		return -1
	else
		sockaddr = allocate(16) -- thanks Pete Eberlein!
		poke4(sockaddr, {
				AF_INET +
				c_func(htons,{port}) * #10000,
				hostnode,0,0})
	end if
	if c_func(connect,{SOCKET, sockaddr,16}) = 0 then
		return SOCKET
	else
		c_proc(close_,{SOCKET})
		return -1
	end if
end function

constant URL = 1, URI = 2, NODE = 3, -- host info
		 SERVICE = 1, PORT = 2, PROTOCOL = 3 -- service info

global function Get(sequence hostname, sequence filename)
	object host, service, file, SOCKET

	host = GetHostbyName(hostname)
	if atom(host) then
		return -1
	elsif DEBUG then
		printf(DEBUG,"Host: %s Addr: %s  Node: %d\n",host)
	end if

	service = GetServbyName("http","tcp")
	if atom(service) then
		return -2
	else
		SOCKET = SockConnect(host[NODE],service[PORT])
		if SOCKET < 0 then
			return -3
		end if
		if DEBUG then
			printf(DEBUG,"Service: %s Port: %d Protocol: %s\n",service)
		end if
	end if

	--write(SOCKET, sprintf("GET /%s HTTP/1.0\n\n",{filename}))
	SockWrite(SOCKET, sprintf("GET /%s HTTP/1.1\nHost: %s\n\n",
		{filename,hostname}))
	file = SockRead(SOCKET)
	c_proc(close_,{SOCKET})
	return file
end function

---Cut Here---


--

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu