moryo
Posts: 7
Joined: Tue Mar 27, 2018 7:15 am
Location: Poland

Raspberry connect to pc socket

Tue Jun 26, 2018 12:06 pm

Hi,
Recently I am trying to make a tcp connection between pc to raspberry.

I have got some code that I am running on a pc

Code: Select all

import socket
import sys
from _thread import *

host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((host, port))
except socket.error as e:
    print(str(e))

s.listen(5)
print('waiting for connection')
def threaded_client(conn):
    conn.send(str.encode('welcome, type your info\n'))

    while True:
        data = conn.recv(2048)
        reply = 'server output: ' + data.decode('utf-8')
        if not data:
            break
        conn.sendall(str.encode(reply))
    conn.close()

while True:
    conn, addr = s.accept()
    print('connected to: ' + addr[0] + ':' + str(addr[1]))
    start_new_thread(threaded_client, (conn,))

when it is running I type in RSPi terminal my pc's ip to establish connection

Code: Select all

telnet 169.254.230.216 5555
Every response is:
telnet: Unable to connect to remote host: No route to host

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Raspberry connect to pc socket

Tue Jun 26, 2018 1:52 pm

Welcome to the Raspberry Pi forums.
Every response is:
telnet: Unable to connect to remote host: No route to host
This is unrelated to the code you are running on the PC.

You need to troubleshoot the underlying network connection between the two systems before you get into the complexities of debugging your code.

Is it safe to assume you are using a direct cabled Ethernet connection between the two systems?

How have you determined the IP addresses that they will use? Have you checked that those are the actual addresses being used? Are both addresses in the same IP subnet?

ping might be a useful diagnostic tool to check, in both directions, that the PC and RPi can communicate with each other.

User avatar
lmarmisa
Posts: 1233
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Raspberry connect to pc socket

Tue Jun 26, 2018 1:56 pm

You have to provide valid IP addresses (normally 192.168.x.y).

1) In the case of the python script you have not defined any host.

2) In the case of telnet you have provided a zeroconf address.

viewtopic.php?f=63&t=216742

moryo
Posts: 7
Joined: Tue Mar 27, 2018 7:15 am
Location: Poland

Re: Raspberry connect to pc socket

Wed Jun 27, 2018 11:31 am

The connection is through cable.

Ifconfig command on Raspberry is in attachments. Rspi is set to static adress now to 192.168.0.10.
I set the ip on a pc to 192.168.0.15, subnet mask to 255.255.255.0 default gateway 192.168.0.255.
pc_ping.jpg
pc_ping.jpg (53.99 KiB) Viewed 1162 times
rsp_ifconfig.jpg
rsp_ifconfig.jpg (191.01 KiB) Viewed 1162 times
As I understand i receive response in RSPi and PC so there is connection.
The question is what should I do next?

User avatar
lmarmisa
Posts: 1233
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Raspberry connect to pc socket

Wed Jun 27, 2018 11:41 am

Sorry. I have read again your post and I realize now that your script is a server socket. Therefore, you do not need to define any host in your python script. So, this line is correct:

Code: Select all

host = ''
In relation with the command telnet, use the pc ip address:

Code: Select all

telnet 192.168.0.15 5555

The command telnet should run fine both on your PC and on the RPi. But really I am not sure if windows supports the telnet command.

moryo
Posts: 7
Joined: Tue Mar 27, 2018 7:15 am
Location: Poland

Re: Raspberry connect to pc socket

Wed Jun 27, 2018 12:11 pm

oh wow... I did not wrote port in telnet command... it works:)

There is possibility to enable telnet in windows. It is hidden in advanced settings.

I will go further with this project. It is ultimately for communication with plc through Ethernet cable but I am just learning how to use it.
Thank you for now. Propably I will ask some dumb questions in the future. :P :lol:

Return to “Beginners”