你可以帮我解决socket编程问题。我的目的是读取包含一些ip的.txt文件,打开一个套接字并检查所有ip的连接状态。 我为此编写的代码是: -
int main()
{
char data[100];
ofstream myfile;
myfile.open("new.txt");
myfile<<"www.google.com";
myfile<<"www.yahoo.com"
myfile.close();
ifstream readfile;
readfile.open("new.txt");
while(!readfile.eof())
{
if(!readfile.eof())
{
readfile.getline(data,100);
int status;
struct addrinfo host_info;
struct addrinfo *host_info_list;
memset(&host_info, 0, sizeof host_info);
host_info.ai_family = AF_INET;
host_info.ai_socktype = SOCK_STREAM;
status = getaddrinfo("data", "22", &host_info, &host_info_list);
if (status != 0) cout << "getaddrinfo error" << gai_strerror(status) ;
else
{
int socketfd;
socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype,
host_info_list->ai_protocol);
if (socketfd == -1) cout << "socket error " ;
else
{
cout << "Connect()ing..." << std::endl;
//int status;
status = connect(socketfd, host_info_list->ai_addr, host_info_list- > ai_addrlen);
if (status == -1) cout << "connect error" ;
else
{
cout << "connection successful"<<endl;
freeaddrinfo(host_info_list);
close(socketfd);
}
}}}}
return 0;
}