My Account


RSS
oZone3D.Net RSS Feeds»RSS 2.0 Feeds

Blogs
»Demoniak3D Blog
»JeGX's Infamous Lab

Sponsors


Modul8: real time video mixing and compositing


Link to Us

oZone3D.Net 100% Realtime 3D

»All Links

Web Partners

www.geeks3d.com
www.benchmarkhq.ru
www.tdt3d.com
www.steph3d.net
www.g-truc.net
www.worldpcspecs.com


Banners Exchange

www.jmax-hardware.com
cgindia.blogspot.com
grapejuice.c.la
www.game-lab.com


Links Exchange

»CYGAD's 3DXtra

Search
Google
Web
oZone3D.Net
 
The LUA SocketLib and the Coroutines

By Jerome Guinot aka 'JeGX' - jegx_AT_ozone3d(dot)net

Initial draft: May 2, 2006


[ Index ]

Introduction | Page 2 | Page 3 | Page 4 | Page 5

»Next Page



3 - Setting up a TCP/IP server

The usual procedure to create a TCP server can directly be applied with the SocketLib functions. This schema is the following one:

  • 1 - create a server half-socket
  • 2 - attach the half-socket to a port
  • 3 - listen on this port

The SocketLib being a part of Hyperion, it can be used like any other standard library provided with LUA (io, math, string, os, etc.) simply by calling its name with the socket.tcp() extension for example. The following code shows how to create the TCP server.

function createTCPServer( host, port, backlog )
	local tcp_server, err = socket.tcp();
	if tcp_server==nil then 
		return nil, err; 
	end

	tcp_server:setoption("reuseaddr", true);
	local res, err = tcp_server:bind(host, port);
	if res==nil then
		return nil, err;
	end

	res, err = tcp_server:listen(backlog);
	if res==nil then 
		return nil, err;
	end

	return tcp_server;
end

socket.tcp() allows to create a TCP server object. It is then possible to access the server functions by separating the server name and the function name by 2 points: tcp_server:setoption(). A detailed explanation on functions and objects provided with the SocketLib is available in the project at the end of this page.

All the functions used after the creation of the server object are really well known for this kind of job: setoption(), bind() and listen().

Most of the SocketLib functions return one parameter if successful and two parameters if not. If a failure occurs, the first returned value is nil and the second value is the error message.

The createTCPServer() function must be called once during ths initialization of the host program. This is done in Hyperion just by inserting both the code and the call to that function in an initialization LUA script (run_mode="INIT").

Last thing: pay attention to the host parameter in createTCPServer() function. If you set it with "localhost", the TCP server will accept only localhost incoming connections (i.e the client is localized on the same computer than the server). So if you wish to be able to connect to your server from a client placed physically in another computer, you have to set host as follow: host = "*";. It's a little trick but believe me, you may waste much time if you don't know it!

Creating a TCP server is the easiest part. Let us tackle the hardcore one: the acceptation and the management of the in going connections.





[ Index ]

Introduction | Page 2 | Page 3 | Page 4 | Page 5

»Next Page







Language:


Demoniak3D
Current Version: 1.23.0
»Demoniak3D
»Download
»Libraries and Plugins
»Demos
»Online Help - Reference Guide
»Codes Samples


GPU Caps Viewer
Current Version: 1.5.0
»GPU Caps Viewer
»GPU DB Submissions


FurMark
Current Version: 1.4.0
»FurMark
»Benchmark Submissions


PhysX FluidMark
Current Version: 1.0.0
»PhysX FluidMark
»Benchmark Submissions


Geeks3D News


Sponsors



Visitors Map

Page generated in 0.10920190811157 seconds.