06-25-2006, 02:09 PM
Hi I tried to do a simple SDL chat thing using Freebasic and SDL_Net. it failed miserably. Can someone help me figure out what's wrong with the code?
Server side:
Client Side
Server side:
Code:
'$include: '..\inc\sdl\sdl.bi'
'$include: '..\inc\sdl\sdl_net.bi'
DECLARE SUB DeinitAll
DIM server_IP as IPaddress' ptr
DIM Client_IP as IPaddress ptr
DIM Shared errormsg$
errormsg$ = "Everything went fine"
DIM server as TCPSocket, client as TCPSocket
DIM message as string
message = "You really suck at life"
DIM ipaddr as Uint32
DIM port as Uint16
port = 599
z = SDL_Init(0)' Then
If z < 0 Then
errormsg$ = "Failed to start SDL"
deInitAll
END IF
If SDLNet_Init = 1 Then
errormsg$ = "Failed to start SDLNET"
deInitAll
END IF
IF SDLNet_ResolveHost (@server_ip, 0, port) = 1 Then
errormsg$ = "Failed to resolve host"
deInitAll
END IF
server = SDLNET_TCP_Open(@server_ip)
IF not server Then
errormsg$ = "Failed to open TCP socket thingy for server"
deInitAll
END IF
'print errormsg$
DO
client = SDLNET_TCP_Accept (server)
IF client Then
Print "received client thingy"
client_ip = SDLNET_TCP_GetPeerAddress (client)
IF Client_ip Then
ipaddr = SDL_SwapBE32(client_ip>host)
blagh$ = "i.cantbebotehred.coding.ipsolver"
Print "accepted a connection from" + blagh$
length = SDLNET_TCP_Send (client, @message, len(message))
'If length < len(message) Then
'errormsg$ = "Failed to send message"
SDLNET_TCP_Close client
Print "Bye"
EXIT DO
END IF
END IF
LOOP
DeinitAll
SUB DeinitAll
sdlnet_quit
sdl_quit
print errormsg$
sleep
END
END SUB
Client Side
Code:
'$include: '..\inc\sdl\sdl.bi'
'$include: '..\inc\sdl\sdl_net.bi'
DECLARE SUB DeIniTAll
DIM IP as IPAddress
DIM SHARED Sock as TCPSocket
DIM Shared errormsg$
errormsg$ = "Everything went fine"
DIM Message as string * 23'(1024) as ubyte
DIM port as Uint16
port = 699
z = SDL_Init(0)' Then
If z < 0 Then
errormsg$ = "Failed to start SDL"
deInitAll
END IF
If SDLNet_Init = 1 Then
errormsg$ = "Failed to start SDLNET"
END IF
IF SDLNet_ResolveHost(@IP,"localhost",port) Then
errormsg$ = "Failed to resolve host"
deInitAll
END IF
sock = SDLNet_TCP_Open(@ip)
IF not Sock Then
errormsg$ = "Failed to do host socket resolve thingy"' + sock
deinitall
END IF
length = SDLNet_TCP_Recv (sock, @message, 23)
If length Then
print message
ELSE
errormsg$ = "Failed to receive message"
deInitAll
END IF
DeinitAll
SUB DeinitAll
SDLNet_TCP_Close sock
sdlnet_quit
sdl_quit
print errormsg$
sleep
END
END SUB
am part of the legion of n00b. We are numerous if dumb. We will enslave you all!