服务器端:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <error.h>
using namespace std; int main()
{
double bs_x[] = {,-,,,-};
double bs_y[] = {,,,-,-};
//vector<vector<double> > tdoa;
double tdoa[][]; ifstream infile("otdoa.txt");
if( !infile)
{
cout << "can't open otdoa.txt" << endl;
return -;
}
int i = ;
int j = ;
while(infile)
{
string line;
double a;
//vector<double> ta;
/*infile >> line;
istringstream stream(line);
int j = 0;
while(stream >> a)
{
tdoa[i][j] = a;
j++;
}*/
if( j == )
{
i++;
j = ;
}
infile >> tdoa[i][j];
j++;
if(infile.peek() == '\n')
break;
}
infile.close(); int listenfd,confd;
struct sockaddr_in servaddr; listenfd = socket(AF_INET,SOCK_STREAM,);
if(listenfd < )
{
cout << "can't open socket" << endl;
return -;
} bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(); if( bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) < )
{
//cout << "bind error" << endl;
perror("bind error");
return -;
} if( listen(listenfd,) < )
{
cout << "listen error" << endl;
return -;
} i = ; //confd = accept(listenfd,(struct sockaddr*)NULL,NULL);
while()
{
confd = accept(listenfd,(struct sockaddr*)NULL,NULL);
if( confd < )
{
cout << "accept error" << endl;
return -;
}
pid_t pid;
pid = fork();
if( pid == )
{
close(listenfd);
cout << "child" << endl;
send(confd,bs_x,sizeof(bs_x),);
send(confd,bs_y,sizeof(bs_y),);
send(confd,tdoa[i],sizeof(tdoa[i]),);
//close(confd);
break;
}
cout << "parent" << endl;
close(confd); i++;
sleep();
}
cout << "child exit" << endl;
close(confd);
return ;
}
客户端:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std; int main()
{
struct data
{
double bs_x[];
double bs_y[];
double tdoa[];
}data_t;
struct data revdata;
char buf[];
int sockfd;
struct sockaddr_in addr; sockfd = socket(AF_INET,SOCK_STREAM,); bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(); if( connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)) < )
{
cout << "connect error" << endl;
return -;
} struct sockaddr_in testaddr;
bzero(&testaddr,sizeof(testaddr));
getsockname(sockfd,(struct sockaddr*)&testaddr,NULL);
char bbb[];
inet_ntop(AF_INET,&testaddr.sin_addr,bbb,sizeof(testaddr));
cout << bbb << endl;
cout << ntohs(testaddr.sin_port) << endl;
while() //死循环
{
//menset(&buf,sizeof(buf));
if( recv(sockfd,buf,sizeof(revdata),MSG_WAITALL) < ) //当服务器端断开连接时,recv失效
{
cout << "recieve msg erro" << endl;
return -;
} bzero(&revdata,);
memcpy(&revdata,buf,sizeof(revdata)); for(int i = ; i < ; i++) //但是这里一直在死循环中输出
{
printf("[%d]\t",i+);
cout << revdata.bs_x[i] <<" "
<< revdata.bs_y[i] << " "
<< revdata.tdoa[i] << endl;
}
}
close(sockfd);
return ; }
因为recv在客户端的死循环中,当服务端断开连接时,recv失效