0
在下一個代碼中,雖然我嘗試連接客戶端,但服務器顯示以下錯誤: 「invalid argument」,我看不到錯誤。爲什麼在嘗試接受連接時出現「無效參數」?
if((l_sock=socket(AF_INET,SOCK_STREAM,0))!=-1)
{
struct sockaddr_in srv_dir;
srv_dir.sin_family=AF_INET;
srv_dir.sin_port=8500;
srv_dir.sin_addr.s_addr=INADDR_ANY;
if((bind(l_sock,(struct sockaddr *)&srv_dir,sizeof(struct sockaddr_in)))!=-1)
{
if(!(listen(l_sock,5)))
{
signal(SIGINT,cerraje);
int t_sock;
struct sockaddr_in cli_dir;
socklen_t tam;
time_t tstmp;
struct tm * res;
res=(struct tm *)malloc(sizeof(struct tm));
while(!key)
{
if((t_sock=accept(l_sock,(struct sockaddr *)&cli_dir,&tam))!=-1)
{
tstmp=time(&tstmp);
res=gmtime(&tstmp);
send(t_sock,res,sizeof(struct tm),0);
wr_hora(*res,cli_dir.sin_addr);
}
else
perror("Petición no atendida");//The error is printed here.
非常感謝。
就是這樣,非常感謝。我認爲變量tam只是一個輸出值,所以我甚至不認爲它需要一個初始值。 – 2011-02-04 14:38:59