Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello. I'm a beginner trying to finish one project concerning networking. I'm working on a server-client duo, and i found a lot of obstacles. Most of them caused problems to me only due to my complete lack of knowledge and experience, so i guess that's the case also this time. I've managed to create a server which can send commands to a client. Client is currently able to execute a command, check if it was executed correctly, but now i try to send a response to a server, and that's where i have troubles. I can't figure how to do it. I will post my code down below. I would be immensely grateful for any sort of help. I'd like to know how to force it to send and recieve a response. I'm sorry for wasting your times for such trivialities, but i hope someone will want to show me the right direction. Thank you in advance and have a nice day.

What I have tried:

Server:

import socket


def socket_create():
    try:
        global host
        global port
        global s
        host = ''
        port = 9999
        s = socket.socket()
    except socket.error as msg:
        print("Socket creation error: " + str(msg))


print("Test server")


def socket_bind():
    try:
        global host
        global port
        global s
        s.bind((host, port))
        print("Binding...")
        s.listen(5)
    except socket.error as msg:
        print("Binding error: " + str(msg) + "\n" + "Retrying...")
        socket.bind()


def socket_accept():
    conn, address = s.accept()
    print("Connection established | " + "IP " + address[0] + " | Port " + str(address[1]))
    print("Give the command: ")
    send_command(conn)
    receive_command()  #try

    conn.close()


def send_command(conn):
    while True:
        cmd = input()
        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            

#try
def receive_command():
    while True:
        resp = s.recv(1024)
        if resp[:1].decode("ISO-8859-1") == 'e':
            print("test")


def main():
    socket_create()
    socket_bind()
    socket_accept()


main()


Client:

import pyautogui
import os
import socket
import threading
import time

s = socket.socket()
host = '' #place for ip
port = 9999
s.connect((host, port))

print("Test client")

path = 'D:/screen'
os.chdir(path)


def sender1():
    conn, address = s.accept()
    responder1(conn)

    conn.close()


def printer1():
    pyautogui.screenshot('D:/screen/screenshot1.png')
    if os.path.isfile('./screenshot1.png'):
        print("Screenshot1 was created")
    else:
        print("Screenshot1 was not created")


def printer2():
    pyautogui.screenshot('D:/screen/screenshot2.png')
    if os.path.isfile('./screenshot2.png'):
        print("Screenshot2 was created")
    else:
        print("Screenshot2 was not created")


def responder1():
    if os.path.isfile('./screenshot1.png'):
        s.send(str.encode("e"))


def responder2():
    if os.path.isfile('./screenshot2.png'):
        print("test 2")
    else:
        print("n. test 2")


t1 = threading.Thread(target=printer1, name='thread1')
t2 = threading.Thread(target=printer2, name='thread2')
t3 = threading.Thread(target=responder1, name='thread3')
t4 = threading.Thread(target=responder2, name='thread4')

while True:
    command = s.recv(1024)
    print(command)   #delete after test
    if command[:1].decode("ISO-8859-1") == 'a':
        t1.start()
        time.sleep(2)
        t3.start()
    if command[:1].decode("ISO-8859-1") == 'b':
        t2.start()
        time.sleep(2)
        t4.start()

s.close()
Posted
Updated 3-Apr-19 7:44am

1 solution

This looks better than your code:

Socket Programming in Python (Guide) – Real Python[^]
 
Share this answer
 
Comments
Member 14211165 3-Apr-19 17:16pm    
I didn't know people with years of experience can write a better code than me. Thank your for a great source of informations though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900