File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ # socket-programming-python
2+ socket programming in python with client-server with duplex alternate chat(client-server-client-server-....)
3+
4+ # Client
5+
6+ ![ client] ( https://user-images.githubusercontent.com/29729380/55186437-8e9f0b00-51bc-11e9-86fa-5641143db32e.png )
7+
8+
9+ # Server
10+
11+ ![ server] ( https://user-images.githubusercontent.com/29729380/55186445-92cb2880-51bc-11e9-9e67-40a9368cc6c7.png )
Original file line number Diff line number Diff line change 1+ # import socket modules
2+ import socket
3+ # create TCP/IP socket
4+ s = socket .socket ()
5+ # take user input ip of server
6+ server = input ("Enter Server IP: " )
7+ # bind the socket to the port 12345, and connect
8+ s .connect ((server ,12345 ))
9+ # receive message from server connection successfully established
10+ data = s .recv (1024 ).decode ("utf-8" )
11+ print (server + ": " + data )
12+
13+ while True :
14+ # send message to server
15+ new_data = str (input ("You: " )).encode ("utf-8" )
16+ s .sendall (new_data )
17+ # receive message from server
18+ data = s .recv (1024 ).decode ("utf-8" )
19+ print (server + ": " + data )
20+
21+ # close connection
22+ s .close ()
Original file line number Diff line number Diff line change 1+ # import socket module
2+ import socket
3+ # create TCP/IP socket
4+ s = socket .socket ()
5+ # get the according IP address
6+ ip_address = socket .gethostbyname (socket .gethostname ())
7+ # binding ip address and port
8+ s .bind ((ip_address , 12345 ))
9+ # listen for incoming connections (server mode) with 3 connection at a time
10+ s .listen (3 )
11+ # print your ip address
12+ print ("Server ip address:" ,ip_address )
13+ while True :
14+ # waiting for a connection establishment
15+ print ('waiting for a connection' )
16+ connection , client_address = s .accept ()
17+ try :
18+ # show connected client
19+ print ('connected from' , client_address )
20+ # sending acknowledgement to client that you are connected
21+ connection .send (str ("Now You are connected" ).encode ("utf-8" ))
22+
23+ # receiving the message
24+ while True :
25+ data = connection .recv (1024 ).decode ("utf-8" )
26+ if data :
27+ # message from client
28+ print (list (client_address )[0 ],end = "" )
29+ print (": %s" % data )
30+ # Enter your message to send to client
31+ new_data = str (input ("You: " )).encode ("utf-8" )
32+ connection .send (new_data )
33+ finally :
34+ # Close connection
35+ connection .close ()
You can’t perform that action at this time.
0 commit comments