我的简单程序通过编译器没有给出错误并且运行良好,但是在有人连接之前它也没有给出它应该的输出。我做了很多研究和编辑,但无法弄清楚。另外我如何让多个人连接?任何帮助使其工作的帮助将不胜感激。提前致谢!!!代码如下。
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
char msg[20];
using namespace std;
int main(int argc, char *argv[])
{
cout << "Made it to main!";
int listener_d = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in name;
name.sin_family = PF_INET;
name.sin_port = (in_port_t)htons(30000);
name.sin_addr.s_addr = INADDR_ANY;
if(bind (listener_d, (struct sockaddr *) &name, sizeof(name)) == -1)
{
cout << "Can't bind the port!";
}
else
{
cout << "The port has been bound.";
}
listen(listener_d, 10);
cout << "Waiting for connection...";
while(1)
{
struct sockaddr_storage client_addr;
unsigned int address_size = sizeof(client_addr);
int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);
cin >> msg;
send(connect_d, msg, strlen(msg), 0);
}
return 0;
}