Hello!
Immediately I say how you probably can notice, I am not a master in this topic ….
The problem is that when I want to run some kind of site in the terminal using Telnet (I use Ubuntu), something happens:
$ telnet en.wikipedia.org 80
Trying 91.198.174.192 ...
Connected to dyna.wikimedia.org.
Escape Character is '^]'.
Get Connection Closed by Foreign Host.
That is, the connection is set, but as soon as I want to enter something, the inscription “Connection closed by Foreign Host” comes out and everything ends.
At the same time. When I run my server, it works great:
$ telnet 127.0.0.1 30000
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape Character is '^]'.
Internet Knock-Knock Protocol Server
Version 1.0.
Knock-knock!
& gt; Who is there?
DOCTOR.
& gt; Doctor WHO?
HA-HA.
Connection closed by Foreign Host.
and what is interesting:
$ netstat -tapnl | Grep 443.
(Not all processes have been identified, information about processes without owner
It will not be displayed, you need a superuser rule (root) to see all the information.)
TCP 0 0 0.0.0.0:443 0.0.0.0:* Listen -
I did not change any Telnet settings and I have no idea what it can be connected with.
I tried with other sites, I tried to change the port, but nothing helped.
What can it be connected with, and, most importantly, how to solve this problem?
Answer 1, Authority 100%
That is, the connection is set, but as soon as I want to enter something, the inscription “Connection closed by Foreign Host” comes out and everything ends.
HTTP server from which you are trying to get information on the HTTP protocol, a very small pause of waiting for information from the client. The program telnet for such experiments is better to replace something more suitable. For example, netcat . This program information that it will send the server can be transferred to its standard input (stdin ).
An example of sending a request:
$ echo -ne 'get / http / 1.0 \ r \ nhost: en.wikipedia.org \ r \ n \ r \ n '| NC En.Wikipedia.org 80.
What we get quite a reasonable answer from the HTTP server:
http / 1.1 301 TLS Redirect
Date: Sat, 27 Jun 2020 19:43:24 GMT
Server: Varnish
X-Varnish: 531026174
X-Cache: CP3056 INT
X-Cache-Status: int-Front
Server-Timing: Cache; DESC = "INT-FRONT"
Set-Cookie: WMF-Last-Access = 27-Jun-2020; path = /; httponly; secure; expires = wed, 29 jul 2020 12:00:00 GMT
Set-Cookie: WMF-Last-Access-Global = 27-Jun-2020; path = /; domain = .wikipedia.org; httponly; Secure; expires = WED, 29 Jul 2020 12:00:00 GMT
X-Client-IP: 188.134.16.218
Location: https://en.wikipedia.org/
Content-Length: 0
Connection: Close.
Answer 2, Authority 50%
The answer is incurred on the issue of Yabi said on the implementation of my advice.
My Example of Customer Implementation and Server on C.
server
/ * defines data types * /
#Include & lt; sys / Types.h & gt;
/ * "Chief" on sockets * /
#Include & lt; sys / socket.h & gt;
/ * SOCKADDR_IN STRUCT, SIN_FAMILY, SIN_PORT, IN_ADDR_T, IN_PORT_T, ... * /
#Include & lt; netinet / in.h & gt;
#Include & lt; stdio.h & gt;
#Include & lt; Memory.h & gt;
#Include & lt; String.h & gt;
#Include & lt; errno.h & gt;
/ ** @ brief gets from the client a sequence byte, not longer than 30 and prints it on the screen
* Completion of the connection. The client sends "Hi, Dear!" * /
Int Main (Int Argc, Char * Argv)
{
/ * Create socket * /
INT S = Socket (AF_INET, SOCK_STREAM, 0);
if (S & LT; 0)
{
PERROR ("Error Calling Socket");
Return 0;
}
/ * Determine the listened port and address * /
STRUCT SOCKADDR_IN ADDR;
addr.sin_family = af_inet;
addr.sin_port = HTNS (18666);
addr.sin_addr.s_addr = htonl (inaddr_any);
if (bind (s, (struct sockaddr *) & amp; addr, sizeof (addr)) & lt; 0)
{
PERROR ("Error Calling Bind");
Return 0;
}
/ * We mark the socket as passive - it will listen to the port * /
If (Listen (S, 5))
{
perror ("Error Calling Listen");
Return 0;
}
/ * We start listening to the connection, create another socket in which we can communicate. * /
INT S1 = Accept (S, NULL, NULL);
IF (S1 & LT; 0)
{
perror ("Error Calling Accept");
Return 0;
}
/ * Read the data from the socket * /
Char Buffer [31];
INT Counter = 0;
for (;;)
{
Memset (Buffer, 0, Sizeof (Char) * 31);
/ * It should be remembered that the data comes unevenly * /
INT RC = RECV (S1, Buffer, 30, 0);
IF (RC & LT; 0)
{
/ * Reading can be interrupted by a system call, this is normal * /
if (errno == EINTR)
Continue;
perror ("CAN't Receive Data");
Return 0;
}
if (rc == 0)
Break;
PrintF ("% s \ n", buffer);
}
Char Response [] = "Hi, Dear!";
IfTo (S1, Response, Sizeof (Response), 0, (struct sockaddr *) & amp; Addr, Sizeof (Addr)) & lt; 0)
perror ("Error Sending Response");
Printf ("Response Send \ n");
Return 0;
}
Client
include & lt; sys / typees.h & gt;
#Include & lt; sys / socket.h & gt;
#Include & lt; netinet / in.h & gt;
/ * HTON, NTOH and so on. * /
#Include & lt; arpa / inet.h & gt;
#Include & lt; Memory.h & gt;
#Include & lt; stdio.h & gt;
Int Main (int argc, char * argv [])
{
/ * declare socket * /
INT S = Socket (AF_INET, SOCK_STREAM, 0);
if (S & LT; 0)
{
PERROR ("Error Calling Socket");
Return 0;
}
/ * Connect on a specific port with host * /
STRUCT SOCKADDR_IN PEER;
peer.sin_family = af_inet;
peer.sin_port = htons (18666);
peer.sin_addr.s_addr = inet_addr ("172.16.8.169"); // Here, replace your address to your address, you can find out the Sudo IFConfig team
INT Result = Connect (s, (struct sockaddr *) & amp; peer, sizeof (peer));
IF (Result)
{
PERROR ("Error Calling Connect");
Return 0;
}
/ * send data
*
* To be accurate, the data is not sent, and are recorded somewhere in the stack when and how they will
* Save to implement the TCP / IP stack more visible. But we immediately get control, not
* waiting for weather by the sea. * /
Char buf [] = "Hello, World!";
Result = Send (S, "Hello, World!", 13, 0);
if (result & lt; = 0)
{
PERROR ("Error Calling Send");
Return 0;
}
/ * Close the connections to send data * /
if (shutdown (s, 1) & lt; 0)
{
perror ("Error Calling Shutdown");
Return 0;
}
/ * Read server response * /
FD_SET READMASK;
FD_SET AllRads;
FD_ZERO (& amp; allreads);
FD_SET (0, & amp; allreads);
FD_SET (S, & amp; allreads);
for (;;)
{
READMASK = AllRads;
if (SELECT (S + 1, & AMP; READMASK, NULL, NULL, NULL) & LT; = 0)
{
perror ("Error Calling SELECT");
Return 0;
}
if (FD_ISSET (S, & Amp; ReadMask))
{
Char Buffer [20];
Memset (Buffer, 0, 20 * Sizeof (CHAR));
INT Result = Recv (S, Buffer, Sizeof (Buffer) - 1, 0);
if (result & lt; 0)
{
PERROR ("Error Calling Recv");
Return 0;
}
if (result == 0)
{
perror ("Server disconnected");
Return 0;
}
if (StrnCMP (Buffer, "Hi, Dear!", 9) == 0)
PrintF ("Got Answer. Success. \ n");
ELSE.
PERROR ("WRONG ANSWER!");
}
if (FD_ISSET (0, & amp; ReadMask))
{
PrintF ("No Server Response");
Return 0;
}
}
Return 0;
}
We collect the server and run it
dima @ komp: ~ / mita / tcp_ip $ gcc client.c -o client
Dima @ Komp: ~ / Mita / TCP_IP $ ./client
In creating a separate terminal and collect the client:
dima @ komp: ~ / mita / tcp_ip $ gcc client.c -o client
Dima @ Komp: ~ / Mita / TCP_IP $ ./client
The server will display
Hello, world!
Response Send.
Client will display
Got Answer. Success.
Server Disconnected: Success