求明白人帮看看
//unix  网络头尾文件
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <iostream>
#include<errno.h>
using namespace std;
void str_echo(int sockfd);
int main(int agc, char ** agr)
{
pid_t childpid;
socklen_t clilen;
//unix
cout << "hello" << endl;
int sockConn, sockSrv;
if ((sockSrv = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket fail");
} sockaddr_in addrSrv;
memset(&addrSrv, 0x00, 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*)&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"<<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))>0)
{
cout<<buf<<endl;
    send(sockfd ,buf,n,0);//为什么没有回消息

}

if(n<0&&errno ==EINTR)
goto again;
else if (n<0)
cout<<"read error"<<endl;
}//----------------------------------------------------------------------
//unix  网络头尾文件
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <iostream>
#include <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(&addrSrv, 0x00, 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*)&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)
{
char sendline[1024],recvline[1024];
while(fgets(sendline,1024,fp)!=NULL)
{
send(sockfd,sendline,strlen(sendline),0);

cout<<sendline<<endl;
if(recv(sockfd,recvline,1024,0)==0)
{
cout<<"AA"<<endl;
}
else
{
fputs(recvline,stdout);
}
}


}