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);
}
}
}
the above is a server program : I am creating a socket with ip address and port number. I am accepting a connection from the client and recieving tha data from the client. I have to call the API : XcpIp_RxCallback ((uint16) chunkLen, (char*) pChunkData, (uint16) port); when it receives a data on the port. Could anyone tell me where to call the API in the above program ??
Use recvfrom function http://msdn.microsoft.com/en-us/library/windows/desktop/ms740120%28v=vs.85%29.aspx and then call XcpIp_RxCallback
The code looks something like
iResult = recvfrom(RecvSocket,RecvBuf,
BufLen, 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
if (iResult == SOCKET_ERROR) {
wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
} else {
XcpIp_RxCallback ((uint16) iResult, (char*) RecvBuf, (uint16)htons(SenderAddr.sin_port));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.