File tree 3 files changed +100
-0
lines changed
3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < sys/ types.h>
2
+ #include < sys/ socket.h>
3
+ #include < netdb.h>
4
+ #include < stdio.h>
5
+ #include < stdlib.h>
6
+ #include < unistd.h>
7
+ #include < string .h>
8
+
9
+ #define BUF_SIZE 500
10
+
11
+ int main():
12
+ HOST := " 127.0.0.1"
13
+ PORT := " 65432"
14
+
15
+ sockfd := socket(AF_INET, SOCK_STREAM, 0 )
16
+ struct addrinfo hints;
17
+ struct addrinfo * result, * rp;
18
+ char buf[BUF_SIZE]
19
+
20
+ memset(&hints, 0 , sizeof(struct addrinfo));
21
+ hints.ai_family = AF_UNSPEC;
22
+ hints.ai_socktype = SOCK_DGRAM;
23
+ hints.ai_flags = 0;
24
+ hints.ai_protocol = 0;
25
+
26
+ s := getaddrinfo(HOST, PORT, &hints, &result)
27
+ if s != 0:
28
+ fprintf(stderr , " getaddrinfo: %s\n" , gai_strerror(s));
29
+ exit (EXIT_FAILURE);
30
+
31
+ bytes_read := 0
32
+ while true :
33
+ if bytes_read == 0:
34
+ connect(sockfd, result-> ai_addr, result-> ai_addrlen)
35
+ message := " {\" a\" :1}\n"
36
+ write (sockfd, message , strlen(message))
37
+ bytes_read = read (sockfd, buf, BUF_SIZE-1)
38
+ buf[bytes_read] = 0
39
+ print bytes_read, buf
40
+ memset(buf, 0 , BUF_SIZE)
41
+ sleep( 1 )
42
+
Original file line number Diff line number Diff line change
1
+
2
+ import json
3
+ import socket
4
+
5
+ HOST = "127.0.0.1" # Standard loopback interface address (localhost)
6
+ PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
7
+
8
+ conn = socket .create_connection ((HOST , PORT ))
9
+
10
+ conn .send (b"{\" a\" :1}\n " )
11
+ conn .close ()
12
+ while True :
13
+ data = conn .recv (5 )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python3
2
+
3
+ import json
4
+ import socket
5
+ import threading
6
+
7
+ HOST = "127.0.0.1" # Standard loopback interface address (localhost)
8
+ PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
9
+
10
+
11
+ class ClientThread (threading .Thread ):
12
+ def __init__ (self , addr , conn ):
13
+ threading .Thread .__init__ (self )
14
+ self .conn = conn
15
+ print ("New connection added: " , addr )
16
+
17
+ def run (self ):
18
+ # self.csocket.send(bytes("Hi, This is from Server..",'utf-8'))
19
+ messages = []
20
+ message = b""
21
+ while True :
22
+ data = self .conn .recv (1024 )
23
+ chunks = data .split (b"\n " )
24
+ for chunk in chunks [:- 1 ]:
25
+ message += chunk
26
+ messages .append (json .loads (message ))
27
+ message = b""
28
+ message += chunks [- 1 ]
29
+ print (messages )
30
+ if not data :
31
+ break
32
+ conn .sendall (b"hi\n " )
33
+ conn .close ()
34
+
35
+
36
+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
37
+ s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
38
+ s .bind ((HOST , PORT ))
39
+ print ("server started" )
40
+
41
+ while True :
42
+ s .listen ()
43
+ conn , addr = s .accept ()
44
+ newthread = ClientThread (addr , conn )
45
+ newthread .start ()
You can’t perform that action at this time.
0 commit comments