Tag Archives: tcp

Python TCP socket programming: send returns broken pipe error?

One of the most common mistakes in socket programming is the ECONNRESET error that I mentioned in this article. Another mistake that is rarely encountered is the EPIPE error that I will talk about today. This error can occur when sending data when the send function is called, and the program throws the following exception:

socket.error: [Errno 32] Broken pipe

Why this error?First, take a look at the official man 2 write document describing this error:

EPIPE
fd is connected to a pipe or socket whose reading end is closed. When this happens the writing process will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.)

Above, when writing data to a closed reader pipe or socket, the program receives a SIGPIPE signal. We have seen the example of a pipeline in this article.
Today, we’ll take a simple socket example to explore why this error occurs.
The client code is as follows:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 2222))

s.send('hello')
time.sleep(1)
s.send('hello')
s.send('hello')

s.close()

The server code is as follows:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('127.0.0.1', 2222))
s.listen(5)

c, addr = s.accept()

c.recv(1024)

c.close()

After 1 second of running the client program, the Broken pipe error occurs:

[root@localhost python]# python client.py 
Traceback (most recent call last):
  File "client.py", line 10, in <module>
    s.send('hello')
socket.error: [Errno 32] Broken pipe

The results are as follows:

03:51:51.137047 IP 127.0.0.1.50870 > 127.0.0.1.2222: Flags [S], seq 2737957170, win 32792, options [mss 16396,sackOK,TS val 39403123 ecr 0,nop,wscale 5], length 0
03:51:51.137055 IP 127.0.0.1.2222 > 127.0.0.1.50870: Flags [S.], seq 2751472309, ack 2737957171, win 32768, options [mss 16396,sackOK,TS val 39403123 ecr 39403123,nop,wscale 5], length 0
03:51:51.137061 IP 127.0.0.1.50870 > 127.0.0.1.2222: Flags [.], ack 1, win 1025, options [nop,nop,TS val 39403123 ecr 39403123], length 0
03:51:51.137083 IP 127.0.0.1.50870 > 127.0.0.1.2222: Flags [P.], seq 1:6, ack 1, win 1025, options [nop,nop,TS val 39403123 ecr 39403123], length 5
03:51:51.137089 IP 127.0.0.1.2222 > 127.0.0.1.50870: Flags [.], ack 6, win 1024, options [nop,nop,TS val 39403123 ecr 39403123], length 0
03:51:51.137158 IP 127.0.0.1.2222 > 127.0.0.1.50870: Flags [F.], seq 1, ack 6, win 1024, options [nop,nop,TS val 39403123 ecr 39403123], length 0
03:51:51.139348 IP 127.0.0.1.50870 > 127.0.0.1.2222: Flags [.], ack 2, win 1025, options [nop,nop,TS val 39403137 ecr 39403123], length 0
03:51:52.140421 IP 127.0.0.1.50870 > 127.0.0.1.2222: Flags [P.], seq 6:11, ack 2, win 1025, options [nop,nop,TS val 39404140 ecr 39403123], length 5
03:51:52.140444 IP 127.0.0.1.2222 > 127.0.0.1.50870: Flags [R], seq 2751472311, win 0, length 0

It was observed that the error occurred as follows:

    client during sleep, the server has closed the connection normally. After the client is awakened, the first call to send to send data causes the socket to receive RST message. The second call send and then send the data causes the program to receive the SIGPIPE signal, and the Broken pipe error occurs.

Therefore, we can draw the conclusion that if the opposite end closes the connection normally and then RST message is received on the socket, then when send is called on the socket, the Broken pipe error will occur!
This error is usually caused by a bug in the program code, but it is not a serious error and can usually be avoided by ignoring the SIGPIPE signal.

TCP error code 10061: unable to connect because the target machine actively refused http://localhost :8732/Design_ Time_ Addresses/TeacherHelperServic

 
An error occurred when we added a WCF service reference to the client with the following message:
TCP Error code 10061: Unable to connect due to active rejection of target machine
http://localhost:8732/Design_Time_Addresses/TeacherHelperServic
Unable to connect to remote server due to active rejection of target machine. 127.0.0.1:8732 /

Possible reasons:
1. The corresponding service port is not opened
2. Some TCP listening services are not enabled
3. The Client does not match the Serivce configuration
Solution reference:
 
1. Check firewall Settings. Do you open the service port, such as 8732?If not, add the service port as a secure port.
2. Check whether the service hosting process is started. In this case, the service hosting program is usually run for a custom host to host the service.
3. Perhaps a service such as net.TCP Listener Adapter is not started. The Net.TCP Listener Adapter is set to start automatically.
 
 

ssh port forwarding

Haihan Zhou July 2, 2010

Goddady’s Linux virtual host is used to provide SSH. However, when using SSH-D port XXX @server, I encountered the following error of SSH printing:

channel 3: open failed: administratively prohibited: open failed

When the Firefox browser is configured with SOCKS5 pointing to a local port, browsing any web page appears blank.
What is the reason for this?
SSH, open sSHd_config and have a look.

cat /etc/ssh/sshd_config

There was a row:

AllowTcpForwarding no

The original host set the TCP port forwarding for SSH to be disabled. That wants to use SSH as agent to carry out port forwarding is certainly unsuccessful.
Look at the system configuration again, also prohibit IP forwarding;

cat /etc/sysctl.conf | grep ip_forward
net.ipv4.ip_forward = 0

Therefore, the virtual host prompt port forwarding is prohibited, normal.