Python Forum
python socket connect only sometimes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python socket connect only sometimes
#1
Hallo,
I have program in python to simply connect to TCP server and disconnect:
import socket

HOST = '192.168.1.6'  # The server's hostname or IP address
PORT = 5684 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.close()
Then I have program for ESP8266:
#include "ESP8266WiFi.h"
 
const char* ssid = "home_wifi";
const char* password =  "12345678";
 
WiFiServer wifiServer(5684);

byte message_buffer[10];
int data_index;
int client_count;

void setup() {
 
  Serial.begin(115200);
 
  delay(1000);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }
 
  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());
 
  wifiServer.begin();
  client_count = 0;
}

void loop() 
{
  WiFiClient client = wifiServer.available();
  //Serial.println("čekám");
  while(!(client = wifiServer.available())){}
    if(client.connected())
    {
      Serial.println("Client Connected");
    }
    
    while(client.connected()){      
      while(client.available()>0){
        // read data from the connected client
        Serial.write(client.read()); 
      }
      //Send Data to connected client
      while(Serial.available()>0)
      {
        client.write(Serial.read());
      }
    }
    client.stop();
    Serial.println("\nClient disconnected");  
}
Program in ESP8266 correctli prints (to serial output) "Client Connected" when I connect to it via Putty (and then "Client disconnected" when I disconnect), but not when I run my python program. It appears only sometimes - I must run my python program few times to get right result. So in most cases, the ESP8266 probably not register socket comming from my PC (which run my python program)

Do anybody know why?
Please help, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.connect question about parenthesis pace 1 2,376 Jan-30-2021, 08:17 AM
Last Post: Gribouillis
  Socket won't connect, giving me a typeerror GalaxyCoyote 1 3,415 Apr-07-2020, 10:28 AM
Last Post: Mateusz
  Python 2.7 vs Python 3.8 (Socket Module) MattB 8 6,675 Mar-18-2020, 01:02 PM
Last Post: MattB
  how to get your own ip using python (NO SOCKET:) ) julio2000 3 2,319 Mar-02-2020, 09:35 PM
Last Post: buran
  Can I connect python/django to nginx "after the fact"? david503 2 2,241 Feb-06-2020, 04:47 PM
Last Post: david503
  For Xilinx EthernetLite LWIP:Need help in Python SOCKET Coding Saras 1 3,003 Oct-01-2018, 05:16 AM
Last Post: Saras
  Python Socket programming with packets sourabhjaiswal92 1 4,140 Sep-18-2018, 06:24 AM
Last Post: martingever
  Python socket : Error receive data quanglnh1993 1 13,047 Mar-14-2018, 11:59 AM
Last Post: avorane
  Python // C # - SOCKET connection is constantly interrupted raspberryPiBRA 0 2,428 Feb-01-2018, 09:53 AM
Last Post: raspberryPiBRA

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020