Problems with client in a simple server/client set-up

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 8319 Views - Last Post: 30 July 2014 - 04:33 AM Rate Topic: -----

#1 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:17 AM

Based on litterature I've made two small programs, a server and a client.
The server seems to run fine, but when running the client I get an error massage.

The code was written for Python 2.7 and has been modified (by me) to run Python 3.3

I use IDLE as editor and console.

The server code:
'
import socket, sys

host = socket.gethostname()
data_payload = 2048
backlog = 5

def echo_server(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_address = (host, port)
    print("Starting up echo server on ", server_address, " port %s" %sock.bind(server_address))
    sock.listen(backlog)
    while True:
        print("Waiting to receive message from client")
        client, address = sock.accept()
        data = client.recv(data_payload)
        if data:
            print("Data : %s" %data)
            client.send(data)
            print("Send ", data, " bytes back to %s" %address)
        client.close()

echo_server(9900)



The client code:
import socket, sys

host = '127.0.0.1'

def echo_client(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (host, port)
    print("Connecting to %s port %s" %server_address)
    sock.connect(server_address)

    try:
        message = "Test message. This will be echoed"
        print("Sennding %s" % message)
        sock.sendall(message)
        amount_received = 0
        amount_expected = len(message)
        while amount_received < amount_expected:
            data = sock.recv(16)
            amount_received += len(data)
            print("Received : %s" % data)
    except (socket.errno, e):
        print("Sockert error: %s" %str(e))
    except (Exception, e):
        print("Other exception: %s" %str(e))
    finally:
        print("Closing connection to server")
        sock.close()

echo_client(9900)



.. and the error message upon running the client:
Traceback (most recent call last):
  File "C:\Python33\Client.py", line 29, in <module>
    echo_client(9900)
  File "C:\Python33\Client.py", line 9, in echo_client
    sock.connect(server_address)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it


Any help to resolve this will be appreciated :-)

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with client in a simple server/client set-up

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:27 AM

That looks like an issue on the receiver not the caller. Is the server running when you call it?
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:29 AM

Yes, I have started the server first.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:31 AM

Then is the port open?
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:34 AM

How do I check this ?
I'm pretty good at Python but this communication issue is totally new to me..
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:44 AM

Check here: http://stackoverflow...ction-in-python
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:49 AM

Try modifying this line,

Quote

09   sock.connect(server_address)


To

 sock.connect(( host, port ))

Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 09:56 AM

I did integrate the StackOverflow code and now see that the port is probably not open - furthermore, now I get a different error message...

import socket, sys

host = socket.gethostname()
data_payload = 2048
backlog = 5

def echo_server(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    
    result = sock.connect_ex((host, port))
    if result > 0:
        print ("problem with socket!")
    else:
        print ("everything it's ok!")

    server_address = (host, port)
    print("Starting up echo server on ", server_address, " port %s" %sock.bind(server_address))
    sock.listen(backlog)
    while True:
        print("Waiting to receive message from client")
        client, address = sock.accept()
        data = client.recv(data_payload)
        if data:
            print("Data : %s" %data)
            client.send(data)
            print("Send ", data, " bytes back to %s" %address)
        client.close()

echo_server(9900)


Outputs:
problem with socket!
Traceback (most recent call last):
  File "C:\Python33\Server.py", line 30, in <module>
    echo_server(9900)
  File "C:\Python33\Server.py", line 18, in echo_server
    print("Starting up echo server on ", server_address, " port %s" %sock.bind(server_address))
OSError: [WinError 10022] An invalid argument was supplied

Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 10:01 AM

Change it to a plain print statement for now. Your attempting to bind it again.
Was This Post Helpful? 1
  • +
  • -

#10 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 10:11 AM

Sorry, - I did things too fast.

I thnik my programs are in line with your instructions, but still same problem..

import socket, sys

host = socket.gethostname()
data_payload = 2048
backlog = 5

def echo_server(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_address = (host, port)
    print("Starting up echo server on %s port %s" %server_address)
    sock.bind((host, port))
    sock.listen(backlog)

    result = sock.connect_ex((host, port))
    if result > 0:
        print ("problem with socket!")
    else:
        print ("everything it's ok!")
    
    while True:
        print("Waiting to receive message from client")
        client, address = sock.accept()
        data = client.recv(data_payload)
        if data:
            print("Data : %s" %data)
            client.send(data)
            print("Send ", data, " bytes back to %s" %address)
        client.close()

echo_server(9900)

import socket, sys

host = '127.0.0.1'

def echo_client(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = (host, port)
    print("Connecting to %s port %s" %server_address)
    sock.connect((host, port))

    try:
        message = "Test message. This will be echoed"
        print("Sennding %s" % message)
        sock.sendall(message)
        amount_received = 0
        amount_expected = len(message)
        while amount_received < amount_expected:
            data = sock.recv(16)
            amount_received += len(data)
            print("Received : %s" % data)
    except (socket.errno, e):
        print("Sockert error: %s" %str(e))
    except (Exception, e):
        print("Other exception: %s" %str(e))
    finally:
        print("Closing connection to server")
        sock.close()

echo_client(9900)



Will be away from keybord for 1h

This post has been edited by DK3250: 27 July 2014 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 12:44 PM

Change your result variable assignment. This is what comes up for connect_ex():

Quote

socket.connect_ex(address)¶
Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as “host not found,” can still raise exceptions). The error indicator is 0 if the operation succeeded, otherwise the value of the errno variable. This is useful to support, for example, asynchronous connects.


So I don't see how it would return true. So, you could change the line to,
If result != 0
:

This post has been edited by astonecipher: 27 July 2014 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 01:15 PM

...but I don't check for the bolean True, I check for
if result > 0:


if I print the result value it is 10022

I have a feeling that this issue may fundamentally not ba a Python issue, but rather a firewall issue. Not sure, have tried the server program on two different machines with the same result..

I much appreciate your effort and will vote you up - despite the solution to my problem is still waiting..

Does my server program run on your machine ?
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 27 July 2014 - 02:00 PM

No, it doesn't... Here is another example code to try and round out.


Client side:

HOST = '127.0.0.1'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))


Server side:

HOST = '127.0.0.1'                 
PORT = 50007              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

Was This Post Helpful? 2
  • +
  • -

#14 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 562
  • View blog
  • Posts: 1,798
  • Joined: 27-December 13

Re: Problems with client in a simple server/client set-up

Posted 28 July 2014 - 01:45 AM

Once again, thank you for your effort.
I modified your code (see below) but still get the same problem.

I will try on yet another machine and if the problem persists I plan to start a new thread addressing the underlying windows/firewall problem.

So, just for the records, the modified program and output:

import socket, sys

def echo_server():

    HOST = '127.0.0.1'                 
    PORT = 50007              
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)

    result = s.connect_ex((HOST, PORT))
    if result > 0:
        print ("Error number:",result, " >> problem with socket!")
        return
        
    conn, addr = s.accept()
    print('Connected by', addr)
    while True:
        data = conn.recv(1024)
        if not data: break
        conn.sendall(data)
    conn.close()

echo_server()

#output:
Error number: 10022  >> problem with socket!


Was This Post Helpful? 0
  • +
  • -

#15 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3039
  • View blog
  • Posts: 11,680
  • Joined: 03-December 12

Re: Problems with client in a simple server/client set-up

Posted 28 July 2014 - 07:25 AM

Does the other program run for you, before modification?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2