0
您好我有下面的代碼給錯誤:SENDTO不上發送
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket(UDP): ");
return 0;
}
family = AF_INET;
if((remote_server_ent = gethostbyname2(hptr, family)) == NULL)
{
perror("gethostbyname2");
return 0;
}
if (family == AF_INET)
{
sin = (struct sockaddr_in *) addr;
sin->sin_family = AF_INET;
sin->sin_port = htons(server_port);;
bcopy((char *)remote_server_ent->h_addr, (char *)&(sin->sin_addr), remote_server_ent->h_length);
return (sizeof(struct sockaddr_in));
}
if (sendto(fd, msg_data_ptr, sizeof(Msg_data_hdr), 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) != sizeof(Msg_data_hdr))
{
fprintf(stderr, "Error in sending data to the server \n");
}
我的應用程序在端口7000 IP 127.0.0.1上市。 現在,當我停止我的應用程序仍發送數據,它不會給我錯誤。我總是得到與我正在寫入的數據大小相等的寫入字節。我怎麼知道發送失敗或不?