Commit b1066299 authored by Xavier Thompson's avatar Xavier Thompson

Add echoserver.pyx and client.pyx demo

parent 8cc525ba
from stdlib.string cimport Str
from stdlib.socket cimport getaddrinfo, socket, AF_UNSPEC, SOCK_STREAM, AI_PASSIVE, MSG_WAITALL, SHUT_WR, SHUT_RD
from libc.stdio cimport puts, printf
def main():
cdef const char * arg
cdef Str recv
loop = True
while loop:
b = input("Message to send:\n").encode()
arg = b
with nogil:
a = getaddrinfo(NULL, Str("3490"), AF_UNSPEC, SOCK_STREAM, 0, AI_PASSIVE)[0]
s = socket(a.family, a.socktype, a.protocol)
s.connect(a.sockaddr)
msg = Str(arg)
s.sendall(msg)
s.shutdown(SHUT_WR)
recv = s.recvall()
printf('received: %s\n\n', Str.to_c_str(recv))
s.close()
if msg != recv:
puts('sent and received are different')
if msg == Str('quit'):
loop = False
main()
from stdlib.string cimport Str
from stdlib.socket cimport getaddrinfo, socket, AF_UNSPEC, SOCK_STREAM, AI_PASSIVE, SHUT_RD, SHUT_WR
from libc.stdio cimport puts, printf
def main():
cdef Str recv
with nogil:
a = getaddrinfo(NULL, Str("3490"), AF_UNSPEC, SOCK_STREAM, 0, AI_PASSIVE)[0]
s = socket(a.family, a.socktype, a.protocol)
s.bind(a.sockaddr)
s.listen(5)
loop = True
while loop:
s1 = s.accept()
recv = s1.recvall()
printf('received: %s\n\n', Str.to_c_str(recv))
if recv == Str('quit'):
loop = False
s1.shutdown(SHUT_RD)
s1.sendall(recv)
s1.shutdown(SHUT_WR)
s1.close()
s.close()
main()
...@@ -105,6 +105,13 @@ cdef extern from "<netinet/in.h>" nogil: ...@@ -105,6 +105,13 @@ cdef extern from "<netinet/in.h>" nogil:
enum: INET6_ADDRSTRLEN enum: INET6_ADDRSTRLEN
unsigned long htonl(unsigned long hostlong)
unsigned short htons(unsigned short hostshort)
unsigned long ntohl(unsigned long netlong)
unsigned short ntohs(unsigned short netshort)
cdef extern from "<arpa/inet.h>" nogil: cdef extern from "<arpa/inet.h>" nogil:
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment