//Unix network end file
# include & lt; Sys/types. H>
# include & lt; Sys/socket. H>
# include & lt; stdio.h>
# include & lt; Netinet/in. H>
# include & lt; ARPA/inet. H>
# include & lt; Unistd. H>
# include & lt; String. H>
# include & lt; Stdlib. H>
# include & lt; An FCNTL. H>
# include & lt; Sys/SHM. H>
# include & lt; iostream>
#include
using namespace std;
Void str_echo (int sockfd);
Int main (int agc, char * * agr)
{
Pid_t childpid;
Socklen_t clilen;
//Unix
cout <& lt; "Hello" & lt; & lt; endl;
Int sockConn sockSrv;
If ((sockSrv=socket (AF_INET SOCK_STREAM, 0))==1)
{
Printf (" socket fail ");
}
Sockaddr_in addrSrv;
Memset (& amp; AddrSrv, 0 x00, sizeof (addrSrv));
AddrSrv. Sin_family=AF_INET;
AddrSrv. Sin_addr. S_addr=htonl (INADDR_ANY);//inet_addr (" 192.168.3.39 ");
AddrSrv. Sin_port=htons (50000); Daytime server/* */
Printf (" % d ", sizeof (addrSrv));
If (bind (sockSrv, (struct sockaddr *) & amp; AddrSrv, sizeof (addrSrv))==1)
{
Printf (" bind fail ");
}
If (listen (sockSrv, 5)==1)
{
Printf (" listen fail ");
}
While (1)
{
Printf (" WHILE ");
If (sockConn=accept (sockSrv, (struct sockaddr *), NULL, NULL)==1)
{
Printf (" accept fail ");
}
If ((childpid=fork ())==0)
{
Close (sockSrv);
cout<" The CLIENT "& lt; & lt; endl;
Str_echo (sockConn);
exit(0);
}
Close (sockConn);
}
return 0;
}
Void str_echo (int sockfd)
{
ssize_t n;
Char buf [1024].
Again:
While ((n=recv (sockfd, buf, 1024, 0)) & gt; 0)
{
cout<& lt; Buf<& lt; endl;
Send (sockfd, buf, n, 0).//why did not return messages
}
If (n<0 & amp; & Errno==EINTR)
goto again;
Else if (n<0)
cout<" Read the error "& lt; & lt; endl;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Unix network end file
# include & lt; Sys/types. H>
# include & lt; Sys/socket. H>
# include & lt; stdio.h>
# include & lt; Netinet/in. H>
# include & lt; ARPA/inet. H>
# include & lt; Unistd. H>
# include & lt; String. H>
# include & lt; Stdlib. H>
# include & lt; An FCNTL. H>
# include & lt; Sys/SHM. H>
# include & lt; iostream>
# include & lt; Errno. H>
using namespace std;
Void str_cli (FILE * fp, int sockfd);
Int main (int agc, char * * agr)
{
Int sockClient;
If ((sockClient=socket (AF_INET SOCK_STREAM, 0))==1)
{
Printf (" socket fail ");
return -1;
}
Sockaddr_in addrSrv;
Memset (& amp; AddrSrv, 0 x00, sizeof (addrSrv));
AddrSrv. Sin_family=AF_INET;
AddrSrv. Sin_addr. S_addr=inet_addr (" 192.168.3.39 ");
AddrSrv. Sin_port=htons (50000); Daytime server/* */
If (int I=connect (sockClient, (struct sockaddr *) & amp; AddrSrv, sizeof (addrSrv))==1)
{
Printf (" WSAStartup failed: % d \ n ", 1);
Printf (" connect fail \ n ");
return i;
}
Str_cli (stdin, sockClient);
//send (sockClient, "This is lisi", strlen (" This is lisi ") + 1, 0).
Close (sockClient);
return 0;
}
Void str_cli (FILE * fp, int sockfd)
{
Recvline char sendline [1024], [1024].
While (the fgets (sendline, 1024, fp)!=NULL)
{
Send (sockfd, sendline, strlen (sendline), 0).
cout<& lt; Sendline<& lt; endl;
If (recv (sockfd, recvline, 1024, 0)==0)
{
cout<" AA "& lt; & lt; endl;
}
The else
{
The fputs (recvline, stdout);
}
}
}
CodePudding user response:
If (sockConn=accept (sockSrv, (struct sockaddr *), NULL, NULL)==1)===& gt;
If ( ( sockConn=accept (sockSrv, (struct sockaddr *), NULL, NULL) ) ==1)