{"id":258,"date":"2024-06-28T20:46:52","date_gmt":"2024-06-28T20:46:52","guid":{"rendered":"https:\/\/sites.wp.odu.edu\/ralar002\/?p=258"},"modified":"2025-06-20T05:47:14","modified_gmt":"2025-06-20T05:47:14","slug":"cyse-250-socket-programming-project","status":"publish","type":"post","link":"https:\/\/sites.wp.odu.edu\/ralar002\/2024\/06\/28\/cyse-250-socket-programming-project\/","title":{"rendered":"CYSE 250: Socket Programming Project"},"content":{"rendered":"\n<div data-wp-interactive=\"\" class=\"wp-block-file\"><object data-wp-bind--hidden=\"!selectors.core.file.hasPdfPreview\" hidden class=\"wp-block-file__embed\" data=\"https:\/\/sites.wp.odu.edu\/ralar002\/wp-content\/uploads\/sites\/33945\/2025\/06\/Socket-Programming-Project-Report_-Milestone-2.pdf\" type=\"application\/pdf\" style=\"width:100%;height:2000px\" aria-label=\"Embed of Socket Programming Project.\"><\/object><a id=\"wp-block-file--media-214c5eae-63f0-465b-b75c-705e1f8514be\" href=\"https:\/\/sites.wp.odu.edu\/ralar002\/wp-content\/uploads\/sites\/33945\/2025\/06\/Socket-Programming-Project-Report_-Milestone-2.pdf\">Socket Programming Project<\/a><a href=\"https:\/\/sites.wp.odu.edu\/ralar002\/wp-content\/uploads\/sites\/33945\/2025\/06\/Socket-Programming-Project-Report_-Milestone-2.pdf\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-214c5eae-63f0-465b-b75c-705e1f8514be\">Download<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator alignfull has-alpha-channel-opacity\" \/>\n\n\n\n<pre class=\"wp-block-code has-accent-color has-dark-gray-background-color has-text-color has-background has-link-color wp-elements-73fe7bef3e11773935293a1dde5c737f\"><code>#Server \n\nimport socket\nimport json\n\ndict = {\n    \"person1\" : {\n        \"Name\" : \"Sarah Smith\",\n        \"DOB\" : '2\/5\/2003',\n        \"Address\" : '321 Python Way',\n        \"Medication\" : &#091;\"TRE***\", \"AML***\"]\n\n    },\n    \"person2\" : {\n        \"Name\" : \"John Doe\",\n        \"DOB\" : '5\/17\/1980',\n        \"Address\" : '3564 Beverly Hill Rd.',\n        \"Medication\" : &#091;]  \n    },\n    \"person3\" : {\n        \"Name\" : \"Kimberly Shaw\",\n        \"DOB\" : '9\/10\/1970',\n        \"Address\" : '1245 Washington Tree Terrace',\n        \"Medication\" : &#091;\"DOX****\"]\n    },\n    \"person4\" : {\n        \"Name\" : \"Amy Lee\",\n        \"DOB\" : '4\/3\/1997',\n        \"Address\" : '943 Beach Front Ave.',\n        \"Medication\" : &#091;\"PHE****\"]\n    },\n    \"person5\" : {\n        \"Name\" : \"John Doe\",\n        \"DOB\" : '5\/17\/1980',\n        \"Address\" : '5567 Apple Farm Blvd.',\n        \"Medication\" : &#091;'ART***', 'LOW***']  \n    },\n    \"person6\" : {\n        \"Name\" : \"Amy Lee\",\n        \"DOB\" : '12\/13\/2000',\n        \"Address\" : '6324 Sunnydale Drive',\n        \"Medication\" : &#091;\"COL****\", 'NIM****']\n    },\n    \"person7\" : {\n        \"Name\" : \"Harold Johnson\",\n        \"DOB\" : '6\/9\/1945',\n        \"Address\" : '7845 Senior Citizen Ave.',\n        \"Medication\" : &#091;]\n    },\n}\n\n\ndef run_server():\n    # create a socket object\n    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n    server_ip = \"127.0.0.1\"\n    port = 8000\n\n    # bind the socket to a specific address and port\n    server.bind((server_ip, port))\n    # listen for incoming connections\n    server.listen(0)\n    print(f\"Listening on {server_ip}:{port}\")\n\n    # accept incoming connections\n    client_socket, client_address = server.accept()\n    print(f\"Accepted connection from {client_address&#091;0]}:{client_address&#091;1]}\")\n\n    # receive data from the client\n    while True:\n        request = client_socket.recv(1024)\n        request = request.decode(\"utf-8\") # convert bytes to string\n        \n        # if we receive \"close\" from the client, then we break\n        # out of the loop and close the conneciton\n        if request.lower() == \"close\":\n            # send response to the client which acknowledges that the\n            # connection should be closed and break out of the loop\n            client_socket.send(\"closed\".encode(\"utf-8\"))\n            break\n\n        if request&#091;:4] == 'Name':\n            name_list = &#091;names&#091;'Name'] for names in dict.values() if names&#091;'Name'] == request&#091;5:]]\n            print(name_list)\n\n            if len(name_list) == 0:\n                response = 'N\/A'\n                print(response)\n                client_socket.send(response.encode('utf-8'))\n            else:\n                # response = \"accepted\".encode(\"utf-8\") # convert string to bytes\n                response = '\\n'.join(name_list)\n                # convert and send accept response to the client\n                client_socket.send(response.encode('utf-8'))\n\n        elif request&#091;:3] == 'DOB':\n            dob_list = &#091;dob&#091;'DOB'] for dob in dict.values() if dob&#091;'DOB'] == request&#091;4:]]\n            print(dob_list)\n\n            if len(dob_list) == 0:\n                response = 'N\/A'\n                print(response)\n                client_socket.send(response.encode('utf-8'))\n            else:\n                # response = \"accepted\".encode(\"utf-8\") # convert string to bytes\n                response = '\\n'.join(dob_list)\n                # convert and send accept response to the client\n                client_socket.send(response.encode('utf-8'))\n\n        elif request&#091;:7] == 'Address':\n            address_list = &#091;address&#091;'Address'] for address in dict.values() if address&#091;'Address'] == request&#091;8:]]\n            print(address_list)\n\n            if len(address_list) == 0:\n                response = 'N\/A'\n                print(response)\n                client_socket.send(response.encode('utf-8'))\n            else:\n                # response = \"accepted\".encode(\"utf-8\") # convert string to bytes\n                response = '\\n'.join(address_list)\n                # convert and send accept response to the client\n                client_socket.send(response.encode('utf-8'))\n\n        else:\n            medication_available = &#091;]\n            if request&#091;4:11] == 'Address':\n                dictionary_key = &#091;key for key, value in dict.items() if value.get('Address') == request&#091;12:]]\n                print(dictionary_key)\n                print(dict&#091;dictionary_key&#091;0]]&#091;'Medication'])\n                medication_available.append(dict&#091;dictionary_key&#091;0]]&#091;'Medication'])\n\n                if len(medication_available&#091;0]) == 0:\n                    response = 'N\/A'\n                    print(response)\n                    client_socket.send(response.encode('utf-8'))\n                else:\n                    # response = \"accepted\".encode(\"utf-8\") # convert string to bytes\n                    response = '\\n'.join(medication_available&#091;0])\n                    # convert and send accept response to the client\n                    client_socket.send(response.encode('utf-8'))\n\n            else:\n                medication_available = &#091;]\n                dictionary_key = &#091;key for key, value in dict.items() if value.get('DOB') == request&#091;8:]]\n                print(dictionary_key)\n                print(dict&#091;dictionary_key&#091;0]]&#091;'Medication'])\n                medication_available.append(dict&#091;dictionary_key&#091;0]]&#091;'Medication'])\n\n                if len(medication_available&#091;0]) == 0:\n                    response = 'N\/A'\n                    print(response)\n                    client_socket.send(response.encode('utf-8'))\n                else:\n                    # response = \"accepted\".encode(\"utf-8\") # convert string to bytes\n                    response = '\\n'.join(medication_available&#091;0])\n                    # convert and send accept response to the client\n                    client_socket.send(response.encode('utf-8'))\n\n\n        print(f\"Received: {request}\")\n\n    # close connection socket with the client\n    client_socket.close()\n    print(\"Connection to client closed\")\n    # close server socket\n    server.close()\n\n\nrun_server()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-accent-color has-dark-gray-background-color has-text-color has-background has-link-color wp-elements-c3cc312ebc369263a7559d8a9e3fc142\"><code>#Client \n\nimport socket\n\n\ndef close_connection(client):\n    msg = 'close'    \n    client.send(msg.encode(\"utf-8\")&#091;:1024])\n\ndef get_meds(client, msg):\n    meds = 'Meds'\n    msg = f'{meds}{msg}'\n    client.send(msg.encode(\"utf-8\")&#091;:1024])\n\ndef run_client():\n    # create a socket object\n    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n    server_ip = \"127.0.0.1\"  # replace with the server's IP address\n    server_port = 8000  # replace with the server's port number\n    # establish connection with server\n    client.connect((server_ip, server_port))\n\n    while True:\n        # input message and send it to the server\n        wanted_info = 'Name'\n        msg = input(\"First and Last Name Please: \")\n        msg = f'{wanted_info} {msg}'\n        client.send(msg.encode(\"utf-8\")&#091;:1024])\n\n        # receive message from the server\n        response = client.recv(1024)\n        response = response.decode(\"utf-8\")\n\n        # if server sent us \"closed\" in the payload, we break out of the loop and close our socket\n        if response.lower() == \"closed\":\n            break\n\n        if response == 'N\/A':\n            print('Sorry you are not on record here. Have a good day.')\n            close_connection(client)\n            break\n\n        else:\n            wanted_info = 'DOB'\n            msg = input(\"Can I please have your DOB? \")\n            msg = f'{wanted_info} {msg}'\n            client.send(msg.encode(\"utf-8\")&#091;:1024])\n            response = client.recv(1024)\n            response = response.decode(\"utf-8\")\n\n            if response == 'N\/A':\n                msg = input(\"Can you please try again that DOB doesn't exist? \")\n                msg = f'{wanted_info} {msg}'\n                client.send(msg.encode(\"utf-8\")&#091;:1024])\n                response = client.recv(1024)\n                response = response.decode(\"utf-8\")\n\n                if response == 'N\/A':\n                    print('Sorry you are not on record here. Have a good day.')\n                    close_connection(client)\n                    break\n                else:\n                    get_meds(client, msg)\n\n                    # receive message from the server\n                    response = client.recv(1024)\n                    response = response.decode(\"utf-8\")\n                    if response == 'N\/A':\n                        print('You don\\'t have any prescriptions available today')\n                    else:\n                        print(f'You have these medications ready today: {response}. Have a good day!')\n                    \n                    close_connection(client)\n                    break\n\n            elif response.count('\/') &gt; 2:\n                wanted_info = 'Address'\n                msg = input('Can I please have your address?')\n                msg = f'{wanted_info} {msg}'\n                client.send(msg.encode(\"utf-8\")&#091;:1024])\n                # receive message from the server\n                response = client.recv(1024)\n                response = response.decode(\"utf-8\")\n\n                if response == 'N\/A':                    \n                    print('Sorry you are not on record here. Have a good day.')\n                    close_connection(client)\n                    break                         \n\n                else:\n                    get_meds(client, msg)\n\n                    # receive message from the server\n                    response = client.recv(1024)\n                    response = response.decode(\"utf-8\")\n\n                    if response == 'N\/A':\n                        print('You don\\'t have any prescriptions available today')\n                    else:\n                        print(f'You have these medications ready today: {response}. Have a good day!')\n                    \n                    close_connection(client)\n                    break\n            else:\n                get_meds(client, msg)\n\n                # receive message from the server\n                response = client.recv(1024)\n                response = response.decode(\"utf-8\")\n                if response == 'N\/A':\n                    print('You don\\'t have any prescriptions available today')\n                else:\n                    print(f'You have these medications ready today: {response}. Have a good day!')\n                \n                close_connection(client)\n                break\n\n    # close client socket (connection to the server)\n    client.close()\n    print(\"Connection to server closed\")\n\nrun_client()<\/code><\/pre>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":27351,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wds_primary_category":0},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/posts\/258"}],"collection":[{"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/users\/27351"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":5,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/posts\/258\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/ralar002\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}