<<unix Network Programming >> Source compilation DAYTIMETCPCLI.C Problem Summary

Source: Internet
Author: User
Tags socket error htons

Recently began to learn UNIX network programming, the opening of the first program DAYTIMETCPCLI.C compiled run successfully, it took a lot of thought, the problem is summed up for your reference.

FreeBSD UNIX System built VMware virtual machine, before the FreeBSD is also small white, although also played Linux, the network communication these settings ah or not familiar.

The source code of the book is as follows:

#include ".. /lib/unp.h "
#include ".. /lib/error.c "


Int
Main (int argc, char **argv)
{
Int SOCKFD, N;
Char Recvline[maxline + 1];
struct SOCKADDR_INSERVADDR;


if (argc! = 2)
Err_quit ("Usage:a.out <IPaddress>");


if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
Err_sys ("socket error");


Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port =htons (+);/* Daytime Server */
if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0)
Err_quit ("Inet_pton error for%s", argv[1]);


if (Connect (SOCKFD, (SA *) &servaddr, sizeof (SERVADDR)) < 0)
Err_sys ("Connect error");


while ((n = Read (SOCKFD, Recvline, MAXLINE)) > 0) {
Recvline[n] = 0;/* NULL Terminate */
if (fputs (recvline, stdout) = = EOF)
Err_sys ("fputs error");


}
if (n < 0)
Err_sys ("read error");


Exit (0);
}

After the successful installation of FREEBSD, Telnet and FTP services, to open themselves, in the /etc/inetd.conf, with the EE command to open, can be directly edited,

To save must be with root permission, ESC after AA is saved. Ps-ef | grep inetd View PID number, after kill, with INETD-WW heavy

New, new settings take effect.

Editing is actually removing the "#" comment for the corresponding line.

#ftp Stream TCP nowait root/usr/libexec/ftpd ftpd-l
#ftp stream TCP6 nowait root/usr/libexec/ftpd ftpd-l
#telnet Stream TCP nowait root/usr/libexec/telnetd telnetd
#telnet stream TCP6 nowait root/usr/libexec/telnetd telnetd

Anyway, cc DAYTIMETCPCLI.C, Generate A.out, run./a.out 127.0.0.1

Always prompt for connect error:connection refused.

Use Sockstat to view the ports that the server is running on, with 21,23 these common ports.

Baidu said should not open daytime service port. Look at the FreeBSD manual, which says these services are built by default boot.

Then go back to the network programming book, look at the code behind the explanation, know that connect failed to execute, and then analyze the key points in

Htons (13) This function,

13 is the service port number, change 13 to the other port to see, and then change to 21, found that execution success.

Although the execution succeeds, but is not the book daytime string, the heart still has some regrets. At least we can prove it.

The server does not have a problem, it should be configured and some are not set correctly. Continue Baidu.

Then read the explanation of the daytime protocol in the Kiwi Encyclopedia:

The Daytime protocol ( English:Daytime Protocol) is a network transport protocol defined within RFC 867.

The host can connect to a server that supports the daytime protocol with TCP or UDP port 13.

The server returns the date and time in ASCII characters. Format similar: day, month, year time zone.

It is also used to test the connectivity of the computer network, and now the method of testing the network has been switched to ping or traceroute.

Daytime protocol on inetd

In Linux, FreeBSD, or other Unix-like operating systems, the daytime time server is built into the inetd. Typically the daytime protocol service is turned off by default, or the following data is added to the /etc/inetd.conf file to open the service:

Daytime   stream  tcp     nowait  root    internaldaytime   stream  tcp6    nowait  Root    internaldaytime   dgram   udp     wait    root    internaldaytime   dgram   udp6    Wait    root    internal

Read the above explanation, then the daytime service opened, restart inetd, all OK

See inetd.conf inside there are a lot of lines in the beginning, should be a lot of services must be manually opened.

The first example in the book is the perfect execution, the beginning of which makes people happy. Little white, keep moving.

<<unix Network Programming >> Source compilation DAYTIMETCPCLI.C Problem Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.