File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ import socket
2+
3+ # Create a socket object
4+ client_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
5+
6+ # Get local machine name
7+ host = socket .gethostname ()
8+
9+ # Define the port on which you want to connect
10+ port = 12345
11+
12+ # Connect to the server
13+ client_socket .connect ((host , port ))
14+
15+ while True :
16+ message = input ('Enter your message: ' )
17+ client_socket .send (message .encode ('utf-8' ))
18+ data = client_socket .recv (1024 ).decode ('utf-8' )
19+ print ('Received:' , data )
20+
21+ # Close the socket
22+ client_socket .close ()
Original file line number Diff line number Diff line change 1+ import socket
2+
3+ # Create a socket object
4+ server_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
5+
6+ # Get local machine name
7+ host = socket .gethostname ()
8+
9+ # Define the port on which you want to connect
10+ port = 12345
11+
12+ # Bind the socket to the address and port
13+ server_socket .bind ((host , port ))
14+
15+ # Now wait for a client to connect
16+ server_socket .listen (1 )
17+
18+ print (f'Waiting for connections on { host } :{ port } ...' )
19+
20+ # Accept the connection
21+ client_socket , addr = server_socket .accept ()
22+
23+ print ('Got connection from' , addr )
24+
25+ while True :
26+ data = client_socket .recv (1024 ).decode ('utf-8' )
27+ print ('Received:' , data )
28+ message = input ('Enter your message: ' )
29+ client_socket .send (message .encode ('utf-8' ))
30+
31+ # Close the socket
32+ client_socket .close ()
You can’t perform that action at this time.
0 commit comments