Socket.connect() not working

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
User avatar
erifash
Posts: 6
Joined: Mon Nov 28, 2005 6:44 am

Socket.connect() not working

Post by erifash »

I am having a problem with a wlan-test program I modified. When it executes Socket.connect() the wlan light is on but not blinking, and it just stays that way (maybe frozen, I don't know). Can anyone look at the code and find out if this is a simple coding error or something wrong with my psp?

Code: Select all

white = Color.new(255, 255, 255)
offscreen = Image.createEmpty(480, 272)
offscreen:clear(Color.new(0, 0, 0))
y = 0
x = 0

function printstr( text )
	local char = ""
	for i = 1, string.len(text) do
		char = string.sub(text, i, i)
		if char == "\n" then
			y = y + 8
			x = 0
		elseif char ~= "\r" then
			offscreen:print(x, y, char, white)
			x = x + 8
		end
	end
	screen:blit(0, 0, offscreen)
	screen.waitVblankStart()
	screen.flip()
end

function println( text )
	printstr(text .. "\n")
end



site = "www.luaplayer.org"
file = "wlan-test.txt"
host = "www.lua.org"


println("starting wlan...")
Wlan.init()
println("")

println("current configs:")
configs = Wlan.getConnectionConfigs()
for key, value in configs do
	println(key .. ": " .. value)
end
println("")

println("using first connection...")
Wlan.useConnectionConfig(1)
println("")

println("resolving ip...")
while true do
	ip = Wlan.getIPAddress()
	if ip then break end
	System.sleep(100)
end
println("psp ip: " .. ip)
println("")

println("connecting to: " .. site)
sock, error = Socket.connect(site, 80)     -- ERROR HERE
println("")

println("waiting for connection... press start to abort")
abort = false
while not sock:isConnected() do
	if Controls.read():start() then
		abort = true
		break
	end
	System.sleep(100)
end
if abort == false then
	println("connected to " .. tostring(sock))
	println("")

	println("http connect as host: " .. host)
	println("loading page... press start to abort")
	sock:send("GET /" .. file .. " HTTP/1.0\r\n")
	sock:send("host: " .. host .. "\r\n\r\n")
	header = ""
	while true do
		buffer = sock:recv()
		if string.len(buffer) > 0 then
			header = header .. buffer
			startIndex, endIndex = string.find(header, "\r\n\r\n")
			if endIndex then
				printstr(string.sub(header, endIndex))
				break
			end
		end
		if Controls.read():start() then
			break
		end
	end
	sock:close()
end
Wlan.term()
println("")

println("press start to exit")
while not Controls.read():start() do end
Thanks for your time!
AutoIt3 scripter.
User avatar
erifash
Posts: 6
Joined: Mon Nov 28, 2005 6:44 am

Post by erifash »

Okay, I tested my wifi connection under network settings and everything checks out okay. So it is not a problem with my psp's wifi or, if anyone tells me otherwise, my code. It must be something corrupted in luaplayer... I will download it again and see if that doesn't fix it.
AutoIt3 scripter.
User avatar
erifash
Posts: 6
Joined: Mon Nov 28, 2005 6:44 am

Post by erifash »

This is messed up... Earlier (before I last posted) I modified the script and it got past the connection freeze, but it wouldn't resolve www.luaplayer.org's correct ip address and kept on giving me an error page. Now, even when I use the above code it freezes up when it says "using first connection..." The wlan light stays solid for two seconds then the light turns off. Can anyone please help me with this problem?
AutoIt3 scripter.
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

lol...site mix up

Post by be2003 »

Code: Select all

host = "www.luaplayer.org"
not lua.org, you are mixing up sites
- be2003
blog
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

I have a similar problem. I can't use sockets in my apps. If I use them the program freezes at the instruction

Code: Select all

Wlan.useConnectionConfig(1)
i use eloader 0.97, luaplayer 0.16, firmware 2.60
what's the problem?
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

i found the problem... eloader 0.97 with luaplayer needs a dhcp connection.
But now i have another problem with wifi:
when i do

Code: Select all

socket, error = Socket.connect(host, port)
while not socket:isConnected() do System.sleep(100) end
the program seems to fall in an infinite loop.
What could i do?
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

erifash wrote:This is messed up... Earlier (before I last posted) I modified the script and it got past the connection freeze, but it wouldn't resolve www.luaplayer.org's correct ip address and kept on giving me an error page. Now, even when I use the above code it freezes up when it says "using first connection..." The wlan light stays solid for two seconds then the light turns off. Can anyone please help me with this problem?
Same thing happens to me. The program freezes, but the home-button etc. still work. I guess this means it isn't necesairely my fault? Or are we doing something wrong. BTW I use 0.16 and have firmware 1.5.
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

0.16 has no DNS you will have to type the ipaddress manual. Example. 192.168.1.1
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

aaaah thanks alot. Ill try that.

BTW you use 192.168.1.1 as an example. Maybe a very stupid question, but that's for a LAN or am i wrong? So does that mean i should connect like that to my router or something and then make it go for a page or whatever?
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

0.16 has no DNS you will have to type the ipaddress manual. Example. 192.168.1.1
From Luaplayer changelog:
v0.16 (all changes for this version by Shine)
================================================
- Wlan:getIPAddress bugfix: now it returns nil on error and the
IP address otherwise
- DNS resolving. Now you can write
Socket.connect("www.luaplayer.org", 80) instead of
Socket.connect("212.227.39.202", 80),
thanks to ideas how to use the resolver lib from PSPRadio
- when loading JPEGs, the file was not closed
- System.md5sum bugfixing for the Windows version
- Wlan.useConnectionConfig index is 1 based instead of 0 based, like usual in Lua
- System.powerTick added: this prevents power off, now you can nail
your PSP on a wall for your electronic painting :-)
- System.createDirectory, System.removeDirectory and System.removeFile added
maybe you missed DNS in wlan configuration...
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

aserto wrote:maybe you missed DNS in wlan configuration...
What exactly do you mean by that? Can I change something with regards to DNS or anything?
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

i mean in the connection configuration the DNS server field
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

I guess i just don't know enough about this.

Maybe you could help me with this? I used parts from the wlan example in LUA-player.

This is what i have. First it asks for a name for the session. Then it asks you to choose the connection. This all works, but when i choose one, the wlan light stays on for 2 seconds and then it freezes, like said before.

Code: Select all

wit=Color.new(255, 255, 255)
wy=1

endinput=false
dofile("OSK.lua")

function name()
while true do
screen:clear()

screen:print(0,0,"Enter a name for your game.",wit)

pad = Controls.read()
oldpad=pad
   if pad:circle() then
      ready=false
      while true do
 	screen:clear()
   	new_message()
   	read_osk_controls()
   	screen.waitVblankStart()
	screen.flip()
	if ready==true then
	   break
	end
      end
   end

   if pad:cross() then
      if text~="" then
      connect=true
            break
      end
      if text=="" then
      	 screen:print(175,132,"error: no name!",wit)
      end
   end


   if pad:start() then
	connect=false
	break
   end

   if text~="" then
      id=text
      screen:print(0,8,"Game name:"..id,wit)
   end

screen:print(0,264,"Press X to continue. Press O to enter a name.",wit)
screen.waitVblankStart()
screen.flip()
end
end

--------------------------------------------------------------------------

function connection()
Wlan.init()
configs = Wlan.getConnectionConfigs()


while true do
screen:clear()

l=8
screen:print(10,0,"Available connections:",wit)
	for key, value in configs do
		screen:print(10,l,key .. ": " .. value,wit)
		l=l+8
	end

pad = Controls.read()

if pad ~= oldpad then
if pad:up() then
	if wy>1 then
		wy=wy-1
	elseif wy==1 then
		wy=(l/8-1)
	end
end

if pad:down() then
	if wy<&#40;l/8-1&#41; then
		wy=wy+1
	elseif wy==&#40;l/8-1&#41; then
		wy=1
	end
end

if pad&#58;cross&#40;&#41; then
   connect=true
	break
end

if pad&#58;start&#40;&#41; then
	connect=false
	-- break
end
end
oldpad=pad

screen&#58;print&#40;0,wy*8-1,"o",wit&#41;
screen&#58;print&#40;350,0,"wy="..wy,wit&#41;
if connect==true then
screen&#58;print&#40;350,8,"connect=true",wit&#41;
end
screen&#58;print&#40;350,16,"l="..l,wit&#41;
screen&#58;print&#40;0,264,"Press X to select. Press start to cancel.",wit&#41;

screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
end
end

--------------------------------------------------------------------------

function connecting&#40;&#41;

Wlan.useConnectionConfig&#40;wy&#41;

-- start server socket &#40;niet nodig denk ik?&#41;
screen&#58;print&#40;0,l,"opening server socket...",wit&#41;
screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
serverSocket = Socket.createServerSocket&#40;80&#41;

-- start connection and wait until it is connected
screen&#58;print&#40;0,l,"waiting for WLAN init and determining IP address...",wit&#41;
screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
       while true do
	     ipAddress = Wlan.getIPAddress&#40;&#41;
	     if ipAddress then break end
	     System.sleep&#40;100&#41;
       end
screen&#58;print&#40;0,l,"the PSP IP address is&#58; " .. ipAddress,wit&#41;

screen&#58;print&#40;0,l,"connecting to 208.97.136.133&#58;80...",wit&#41;
screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
socket, error = Socket.connect&#40;"208.97.136.133", 80&#41;
        while not socket&#58;isConnected&#40;&#41; do System.sleep&#40;100&#41; end
screen&#58;print&#40;0,l,"connected to " .. tostring&#40;socket&#41;,wit&#41;
screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;

end

--------------------------------------------------------------------------

name&#40;&#41;
if connect==true then
connection&#40;&#41;
end
if connect==true then
connecting&#40;&#41;
end
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

if you have a 2.0+ firmware and you are using eloader 0.97 you need to make a dhcp connection. Try...
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

I have 1.5, but how can you make a dhcp connection with LUA?
aserto
Posts: 22
Joined: Mon Apr 17, 2006 10:38 pm

Post by aserto »

You have to do it in the network settings
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Do you mean setting everything to automatic?
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Still haven't figured this out. I just wanted to add this:

If I select a connection and tell it to connect, it doesn't even show "opening server socket...". So I think this means "Wlan.useConnectionConfig(wy)" isn't working. Or atleast I dont get it to work...

Also in the wiki it says to use "Socket:createServerSocket(port)" and not "Socket.createServerSocket(port)" (one is with a colon and one with a dot). While the example of the Wlan uses the one with the dot. So which one is it?
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Got it! I had to manually type in another IP adress for my PSP.
Post Reply