int acceptSocket;
struct sockaddr_in addr, client, dest;
char buf[256];
long rc, sentbytes;
int port = 18017;
int CreateSocket()
{
if(rc!=0)
{
printf("socket failure code: %ld\n",rc);
return 1;
}
else
{
printf("socket started!\n");
}
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc==-1)
{
printf("Failure: listen, failure code:\n");
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
while(rc!=-1)
{
rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, sizeof(client));
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("failure: recv, failure code\n");
break;
}
XcpIp_RxCallback((uint16) rc, (uint8*) buf, (uint16) port);
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
// buf[rc]='\0';
// printf("Client sendet: %s\n",buf);
// sprintf(buf2,"Du mich auch %s",buf);
// rc=sendto(connectedSocket,buf2,strlen(buf2),0);
}
close(acceptSocket);
return 0;
}
我爲udp層創建了一個套接字,它正在等待來自客戶端的連接,並且稍後將數據發送到客戶端。我的程序在監聽模式下崩潰。即:綁定後:我得到錯誤,因爲c socket編程錯誤?
printf("Failure: listen, failure code:\n");
有誰能告訴我爲什麼? @JoachimPileborg:它不工作!再次是同樣的問題。我無法在此發表評論。所有的
當你得到一個錯誤,你可以看到* *什麼錯誤是打印['errno']的值(http://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html)。您可以通過調用['strerror'](http://pubs.opengroup.org/onlinepubs/009695399/functions/strerror.html)從錯誤代碼中獲得可打印的字符串。請編輯您的問題以包含錯誤代碼(和字符串)。 –
UDP不會連接。 –
鍵盤上的Enter鍵有問題。 –