我正在开发 IDE Momentics Tool 中的服务器客户端应用程序。
这是服务器部分:
int sockfd, portno, a;
char *IP[16];
int server_to_client(struct AllPack allmess)
{
int portno;
struct sockaddr_in serv_addr;
struct hostent *server;
if (a < 2)
{
fprintf(stderr,"ERROR, no IP and port provided\n");
exit(1);
}
portno = atoi(IP[2]);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) error ("ERROR opening socket");
{
server = gethostbyname(IP[1]);
}
if (server == NULL)
{
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server -> h_addr, (char *)&serv_addr.sin_addr.s_addr, server -> h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) error ("ERROR connecting");
int n = write(sockfd, &allmess, sizeof(allmess));
if (n < 0) error ("ERROR writing socket");
close(sockfd);
return (EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
a = argc;
for(int i = 0; i < a; i++)
{
IP[i] = argv[i];
}
server_to_client(allmess);
return (EXIT_SUCCESS);
}
我正在尝试创建portno = atoi(IP[2]);
一组端口和server = gethostbyname(IP[1]);
一组 IP 地址。
我正在像这样启动我的服务器部分:
/home/Server 192.168.0.21 70003 192.168.0.22 70004
所以我必须同时通过多个端口向多个 IP 发送数据?