PDA

View Full Version : Winsock 2 Socket Client (working on PPC and also with Bluetooth PAN)


lopez1de
10-23-2008, 03:38 PM
It was a bit work to get into this, but finally it's working. :D

All the example I found aren't working on PPC, because gethostbyaddress try to get the host via DNS. DNS isn't supported on PPC (BT/WLAN). You have to set the IP address with another way as I did it.

To use this code you have to edit the winsock.ppl (for PPC).
#ifdef _WIN32_WCE
#define sockdll "ws2.dll"

To use this code you have to set the following globals:

global(server$,port$,serverSocket$);
server$ = "192.168.0.100";
port$ = 50555; // Integer! NOT string! ;-)


then add the network.ppl to you project and connect the client with:

client_connect;

client_connect returns true if the connection was successfull.

Now you can send strings to the client:
client_send("Hello Server! What's up?");
If the connection is broken, the client tries to reconnect automatically.
client_send returns the answer from the server as string.


This is the main part you will find in network.ppl. There are some important changes to swsock.ppl:

func client_connect
local(wsaData$,sockAddr$, mAddr$, host$);

struct(wsaData$, WSADATA);
if (WSAStartup(MAKEWORD(2, 2), &wsaData$) != 0)
showmessage("wsastartup: " + WSAGetLastErrorStr);
else
serverSocket$ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket$ == INVALID_SOCKET)
showmessage("socket: " + WSAGetLastErrorStr);
else
new(mAddr$, TUINT);
mAddr$ = inet_addr(server$);
if (mAddr$ == INADDR_NONE)
showmessage("ip adress:" + WSAGetLastErrorStr);
closesocket(serverSocket$);
else
struct(sockAddr$, sockaddr_in);
sockAddr.sin_family$ = AF_INET;
sockAddr.sin_port$ = htons(port$);
sockAddr.sin_addr$ = mAddr$;
if (connect(serverSocket$, &sockAddr$, sizeof(sockAddr$)) == 0)
return(true);
else
showmessage("connect: " + WSAGetLastErrorStr);
closesocket(serverSocket$);
end;
end;
end;
end;

WSACleanup;
return(false);
end;



bye

Nicknack
10-23-2008, 04:58 PM
w00t....oh my god, it's outstanding :D
though my server still crashs after receiving the string, but that the connection on itself works is simply amazing:eek:
one of the best contributions so far, thx you so much ;)

kornalius
10-23-2008, 10:31 PM
Great addition for sure.

In version 2.0 I will be implementing a full socket object that will work on PPC and PC as well. Maybe lopez1de would like to help me write it?

lopez1de
10-24-2008, 05:11 PM
kornalius, I'm not shure I can help you. I had to read alot of stuff, and get this running was a lot of trial and error.:rolleyes:

lopez1de
11-04-2008, 11:39 AM
Edited the first thread post.

To use this code you have to edit the winsock.ppl (for PPC).

#ifdef _WIN32_WCE
#define sockdll "ws2.dll"