0
void CreateSocket()
{
struct sockaddr_in server, client; // creating a socket address structure: structure contains ip address and port number
WORD wVersionRequested;
WSADATA wsaData;
int len;
int iResult;
printf("Initializing Winsock\n");
wVersionRequested = MAKEWORD (1, 1);
iResult = WSAStartup (wVersionRequested, &wsaData);
if (iResult != NO_ERROR)
printf("Error at WSAStartup()\n");
// create socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
printf("Could not Create Socket\n");
//return 0;
}
printf("Socket Created\n");
// create socket address of the server
memset(&server, 0, sizeof(server));
// IPv4 - connection
server.sin_family = AF_INET;
// accept connections from any ip adress
server.sin_addr.s_addr = htonl(INADDR_ANY);
// set port
server.sin_port = htons(52428);
//Binding between the socket and ip address
if(bind (sock, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Bind failed with error code: %d", WSAGetLastError());
}
//Listen to incoming connections
if(listen(sock, 3) == -1){
printf("Listen failed with error code: %d", WSAGetLastError());
}
printf("Server has been successfully set up - Waiting for incoming connections");
len = sizeof(client);
fd = accept(sock, (struct sockaddr*) &client, &len);
if(fd < 0)
{
printf("Accept failed\n");
}
printf("\n Process incoming connection from (%s , %d)", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
}
int main()
{
HANDLE h1,h2,h3;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
CreateSocket();
Xcp_Initialize();
while(1)
{
if(fd == -1)
{
printf("Socket error\n ");
}
else
{
recv(fd, recv_data, 99, 0);
// pChunkData = &recv_data;
//chunkLen = sizeof(pChunkData);
//XcpIp_RxCallback ((uint16) chunkLen, (char*) pChunkData, (uint16) port);
}
}
}
以上是服務器程序: 我正在用ip地址和端口號創建一個套接字。我接受客戶端的連接並從客戶端接收數據。我必須調用API:XcpIp_RxCallback((uint16)chunkLen,(char *)pChunkData,(uint16)port);當它收到端口上的數據時。任何人都可以告訴我在上面的程序中調用API的位置?在套接字程序中調用API?
爲什麼要使用recvfrom的()?套接字的發件人地址不變,它由accept()提供。 – EJP
我將使用recv api !!! – user3189297