Winsock, PYTHON and ASCII [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Winsock, PYTHON and ASCII [Resolved]

Post by Kwai chang caine »

Hello at all :D

I want talking with PYTHON on a Rasberry
For that i create a server on it and that works 8)

Code: Select all

import socket

TCP_IP = '162.22.22.22'
TCP_PORT = 5005
BUFFER_SIZE = 20

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connection address:', addr)

while 1:

	data = conn.recv(BUFFER_SIZE)

	if data:
	
	 print ("received data:", data)
	 conn.send(data)  # echo

conn.close()
But when i use this nice code of BalrogSoft in unicode with 5.70, i have "Failed to connect" like result :| and with v4.51 in ASCII all are done :D

Code: Select all

; http://www.purebasic.fr/english/viewtopic.php?p=67753#p67753

wsaData.WSADATA
iResult = WSAStartup_($101, @wsaData)

If iResult <> #NO_ERROR
 Debug "Error at WSAStartup()"
EndIf

; Create a socket for client
clientsocket = SOCKET_(#AF_INET, #SOCK_STREAM, #IPPROTO_TCP)

If clientsocket = #INVALID_SOCKET
 PrintN( "Error at socket(): " + Str(WSAGetLastError_()))
 WSACleanup_()
 End
EndIf

; Connect to a server.
*ptr = clientService.sockaddr_in

clientService\sin_family = #AF_INET
clientService\sin_addr = inet_addr_("162.22.22.22")
clientService\sin_port = htons_(5005)

If connect_(clientsocket, *Ptr, SizeOf(sockaddr_in)) = #SOCKET_ERROR

 Debug "Failed to connect"
 closesocket_(clientsocket)
 WSACleanup_()
 End

EndIf
Someone know where is the problem :cry:

Have a good day
Last edited by Kwai chang caine on Wed Jun 26, 2019 6:52 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Winsock, PYTHON and ASCII

Post by infratec »

Why you need sockets on the client side :?:

Is this working?

Code: Select all

InitNetwork()

Con = OpenNetworkConnection("162.22.22.22", 5005, #PB_Network_TCP|#PB_Network_IPv4, 3000)
If Con
  Debug "Connected"
  SendNetworkString(Con, "Hello World!", #PB_UTF8)
  CloseNetworkConnection(Con)
Else
  Debug "Connection failed"
EndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Winsock, PYTHON and ASCII

Post by Kwai chang caine »

Hello INFRATEC 8)
Master wrote:Why you need sockets on the client side
Because KCC have the IQ of an Oyster :mrgreen:

I don't know Sockets are the same thing than usual TCP/IP PB functions :oops:
I have lost several hours with Ascii(), pointers, etc ... for nothing :|

I have run your code and PYTHON receive the "Hello World!" :shock:
I continue in this way :wink:

Thanks a lot for help me INFRATEC 8)
I wish you a very good night..not too hot (I talk of the weather obviously :lol:)
ImageThe happiness is a road...
Not a destination
Post Reply