{"id":12945,"date":"2023-04-25T10:00:00","date_gmt":"2023-04-25T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=12945"},"modified":"2023-04-24T19:04:07","modified_gmt":"2023-04-24T10:04:07","slug":"creating-client-and-server-sockets-using-python","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/12945","title":{"rendered":"Creating client and server sockets using Python"},"content":{"rendered":"\n<p>Today, we will explore how to create client and server sockets using Python, along with an example code.<\/p>\n\n\n\n<p>Client and server sockets are a fundamental part of network communication in computer science. A socket is essentially an endpoint that enables two-way communication between two devices over a network.<\/p>\n\n\n\n<p><strong>Server Socket<\/strong><\/p>\n\n\n\n<p>The server socket is a program that listens for incoming connections from clients. Once a connection is established, the server creates a new socket object to handle the communication with the client. Here is a basic example of a server socket program in Python:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nHOST = '127.0.0.1'  # Standard loopback interface address (localhost)\nPORT = 65432        # Port to listen on (non-privileged ports are &gt; 1023)\n\nwith socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n    s.bind((HOST, PORT))\n    s.listen()\n    conn, addr = s.accept()\n    with conn:\n        print('Connected by', addr)\n        while True:\n            data = conn.recv(1024)\n            if not data:\n                break\n            conn.sendall(data)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In the above code, we first import the socket module and define the IP address and port number for the server. Then we create a new socket object &#8216;s&#8217; using the AF_INET address family and SOCK_STREAM socket type.<\/p>\n\n\n\n<p>We then bind the socket to our defined IP address and port number, and start listening for incoming connections using the s.listen() method. Once a client connects, the s.accept() method returns a new socket object &#8216;conn&#8217; representing the connection, along with the address of the client.<\/p>\n\n\n\n<p>We then use a while loop to continuously receive data from the client using conn.recv() and send data back to the client using conn.sendall() until there is no more data to receive.<\/p>\n\n\n\n<p><strong>Client Socket<\/strong><\/p>\n\n\n\n<p>The client socket is a program that initiates a connection to the server socket. Here is a basic example of a client socket program in Python:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import socket\n\nHOST = '127.0.0.1'  # The server's hostname or IP address\nPORT = 65432        # The port used by the server\n\nwith socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n    s.connect((HOST, PORT))\n    s.sendall(b'Hello, world')\n    data = s.recv(1024)\n\nprint('Received', repr(data))<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In the above code, we again import the socket module and define the IP address and port number for the server. We then create a new socket object &#8216;s&#8217; using the AF_INET address family and SOCK_STREAM socket type.<\/p>\n\n\n\n<p>We then initiate a connection to the server using s.connect() and send data to the server using s.sendall(). Finally, we receive data from the server using s.recv() and print the received data.<\/p>\n\n\n\n<p>Conclusion<\/p>\n\n\n\n<p>We have explored how to create client and server sockets using Python along with a basic example code. Sockets are a powerful tool for network communication and are used extensively in a wide range of applications. With the above knowledge, you can build complex network applications in Python.<\/p>\n\n\n\n<p>This is all for now. Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12945\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/12945\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Creating client and server sockets using Python\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12945\" data-text=\"Creating client and server sockets using Python\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12945\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12945\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today, we will explore how to create client and server sockets using Python, along with an example code. Clien [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12945"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=12945"}],"version-history":[{"count":1,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12945\/revisions"}],"predecessor-version":[{"id":12946,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12945\/revisions\/12946"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=12945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=12945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=12945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}