Perhatikan bahwa jika terjadi kesalahan selama pembuatan soket, maka soket. kesalahan dilemparkan dan kami hanya dapat terhubung ke server dengan mengetahui IP-nya. Anda dapat menemukan IP server dengan menggunakan ini. Show $ ping www.google.com Anda juga dapat menemukan IP menggunakan python. import socket ip = socket.gethostbyname('www.google.com') print ip_ Berikut adalah contoh skrip untuk menghubungkan ke Google Python3
$ ping www.google.com0 $ ping www.google.com1 $ ping www.google.com2 $ ping www.google.com3 $ ping www.google.com1 $ ping www.google.com5
$ ping www.google.com6 $ ping www.google.com7 $ ping www.google.com8 $ ping www.google.com9 import socket ip = socket.gethostbyname('www.google.com') print ip0 import socket ip = socket.gethostbyname('www.google.com') print ip1 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 import socket ip = socket.gethostbyname('www.google.com') print ip5 import socket ip = socket.gethostbyname('www.google.com') print ip6 import socket ip = socket.gethostbyname('www.google.com') print ip7 import socket ip = socket.gethostbyname('www.google.com') print ip8 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 Socket successfully created the socket has successfully connected to google on port == 173.194.40.192 Socket successfully created the socket has successfully connected to google on port == 173.194.40.193 Socket successfully created the socket has successfully connected to google on port == 173.194.40.194
Socket successfully created the socket has successfully connected to google on port == 173.194.40.195 ________24______6 import socket ip = socket.gethostbyname('www.google.com') print ip0 Socket successfully created the socket has successfully connected to google on port == 173.194.40.198
$ ping www.google.com6 $ ping www.google.com7 $ ping www.google.com8 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123452 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123454 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123455 import socket ip = socket.gethostbyname('www.google.com') print ip6 import socket ip = socket.gethostbyname('www.google.com') print ip7 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123458
$ ping www.google.com8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)0 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)4 import socket ip = socket.gethostbyname('www.google.com') print ip6 $ ping www.google.com8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)7
# in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)_9
import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.2 import socket ip = socket.gethostbyname('www.google.com') print ip6 Keluaran. Socket successfully created the socket has successfully connected to google on port == 173.194.40.19
Program server-klien sederhana.Server. Server memiliki metode bind() yang mengikatnya ke IP dan port tertentu sehingga dapat mendengarkan permintaan masuk pada IP dan port tersebut. Server memiliki metode listen() yang menempatkan server ke mode mendengarkan. Hal ini memungkinkan server untuk mendengarkan koneksi masuk. Dan terakhir server memiliki metode accept() dan close(). Metode accept memulai koneksi dengan klien dan metode close menutup koneksi dengan klien. Python3# In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.4 $ ping www.google.com1 # In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.6
# In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.7 $ ping www.google.com9 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)0 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 import socket ip = socket.gethostbyname('www.google.com') print ip5 import socket ip = socket.gethostbyname('www.google.com') print ip6
# start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)5 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)6 Socket successfully created the socket has successfully connected to google on port == 173.194.40.196 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)9
# start the client: $ python client.py Thank you for connecting_0 # start the client: $ python client.py Thank you for connecting_1 # start the client: $ python client.py Thank you for connecting_2 # start the client: $ python client.py Thank you for connecting_3 # start the client: $ python client.py Thank you for connecting_4 # start the client: $ python client.py Thank you for connecting_5 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # start the client: $ python client.py Thank you for connecting8 Socket successfully created the socket has successfully connected to google on port == 173.194.40.193 # An example script to connect to Google using socket 0
________115 $ ping www.google.com_115_______3 # An example script to connect to Google using socket 4import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # An example script to connect to Google using socket 7# An example script to connect to Google using socket 8
$ ping www.google.com_00 $ ping www.google.com01 $ ping www.google.com02 $ ping www.google.com7
$ ping www.google.com_04 $ ping www.google.com05 $ ping www.google.com06 import socket ip = socket.gethostbyname('www.google.com') print ip0 $ ping www.google.com08 $ ping www.google.com05 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 $ ping www.google.com12 $ ping www.google.com13
$ ping www.google.com05 $ ping www.google.com15 $ ping www.google.com05 $ ping www.google.com17 $ ping www.google.com18 $ ping www.google.com19
$ ping www.google.com05 $ ping www.google.com21 $ ping www.google.com05 $ ping www.google.com23 $ ping www.google.com_05 $ ping www.google.com05 $ ping www.google.com26 $ ping www.google.com05 $ ping www.google.com28
Klien. # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 12345 Jika 'telnet' tidak dikenali. Di windows cari fitur windows dan aktifkan fitur "klien telnet". Bagaimana cara menutup server soket?close() call mematikan soket yang terkait dengan soket deskriptor soket, dan membebaskan sumber daya yang dialokasikan ke soket. Jika soket mengacu pada koneksi TCP terbuka, koneksi ditutup. Jika soket aliran ditutup saat ada data input yang antri, koneksi TCP diatur ulang bukannya ditutup dengan bersih.
Bagaimana Anda menutup soket dari klien?Ketika Anda telah selesai menangani klien, Anda harus close() soket itu, (itu close(cfd) dalam terminologi Anda). Anda juga dapat mematikan () soket - yang akan memengaruhi cara soket ditutup pada tingkat TCP.
Bagaimana cara menutup koneksi TCP dengan python?Catatan. close() melepaskan sumber daya yang terkait dengan koneksi tetapi tidak serta merta menutup koneksi dengan segera. Jika Anda ingin menutup koneksi secara tepat waktu, panggil shutdown() sebelum close() .
Apa itu shutdown soket?Setelah soket tidak lagi diperlukan, program pemanggil dapat membuang soket dengan menerapkan subrutin dekat ke deskriptor soket . Jika soket pengiriman yang andal memiliki data yang terkait dengannya saat penutupan terjadi, sistem akan terus berupaya mentransfer data. |