Download source code Package www.unpbook.com
3. Enter unp13e to view the Readme and follow the instructions below
1) chmod u+x Configure
./configure
2) CD Lib
Make
3) CD. /libfree
Make
4) CD. /libgai
Make
4. Copy the generated libunp.a static library into/usr/lib and/usr/lib64
5. Modify the Unp.h and copy it and config.h to/usr/include for later convenience
Vim Lib/unp.h//Will be unp.h in #include ". /config.h "modified to #include" config "
7. A simple acquisition Time program:
Client program:
#include "unp.h"//contains the header file that you wrote
int main (int argc, char *argv[])
{
int SOCKFD, n;
Char Recvline[maxline + 1];
struct sockaddr_in servaddr;
if (argc!= 2)
Err_quit ("Usage:a.out");
Creates a TCP socket function that creates an internetwork (AF_INET) byte stream (sock_inet) socket that returns a small integer descriptor.
if ((SOCKFD = socket (af_inet, sock_stream, 0)), 0
Err_sys ("socket error");
Zeroing the Internet socket address structure
Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Call htons Convert binary port number, host byte order to network byte order
Servaddr.sin_port = htons (13);
Converts command line arguments to the appropriate format
if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0)
Err_quit ("Inet_pton error for%s, argv[1]");
Establish a connection with the server
if (Connect (SOCKFD, (sa*) &servaddr, sizeof (SERVADDR)) < 0)
Err_sys ("Connect error");
Read and output the response to the server
while ((n = Read (SOCKFD, Recvline, maxline)) > 0)
{
Recvline[n] = 0;
if (fputs (recvline, stdout) = EOF)
Err_sys ("fputs error");
}
if (n < 0)
{
Err_sys ("read error");
}
Terminate program
Exit (0);
}
int main (int argc, char *argv[])
{
int LISTENFD, CONNFD;
struct sockaddr_in servaddr;
Char Buff[maxline];
time_t ticks;
Create a TCP socket
LISTENFD = Socket (af_inet, sock_stream, 0);
Bundle the server's well-known ports to sockets
Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
Servaddr.sin_port = htons (13);
Bind (LISTENFD, (sa*) &servaddr, sizeof (SERVADDR));
Switch the socket to a listening socket so that the foreign connection from the customer can be accepted by the kernel on that socket
Listen (LISTENFD, Listenq);
Accept customer connection, send answer
for (;;)
{
//
CONNFD = Accept (LISTENFD, (sa*) null, NULL);
Ticks = time (NULL);
snprintf (buff, sizeof (buff), "%.24s\r\n", CTime (&ticks));
Write (CONNFD, Buff, strlen (buff));
Terminate connection
Close (CONNFD);
}
return 0;
}
Test results:
Server side:
root@ubuntu:/home/unpv13e/lib#./daytimetcpsrv
Client:
root@ubuntu:/home/unpv13e/lib#./daytimetcpcli 127.0.0.1 (native loop from test) OCT Tue 6 20:32:04 2015
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.