int acceptSocket;
char buf[100];
long sentbytes;
socklen_t len;
int port = 18227;
int CreateSocket()
{
long rc;
struct sockaddr_in addr, client;
// 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(struct sockaddr_in));
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)
{
len =sizeof(client);
rc=recvfrom(acceptSocket,buf, sizeof(buf), 0, (struct sockaddr*) &client, &len);
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("something went wrong with data %s", strerror(errno));
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
}
close(acceptSocket);
return 0;
}
int main()
{
Xcp_Initialize();
CreateSocket();
return 0;
}
//API for sending the data to the client.
void XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes )
{
struct sockaddr_in client;
if ((long)XcpPort==port){
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*)&client, sizeof(client));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
I created a server side program for receiving data from the client and sending the response back to the client. I am able to receive data from the client via the recvfrom api but later I have to send the response back to the client via sendto api within the XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes ); (This is the api supported in my project for transmitting the data). Client is a tool for sending the data to the specified port and ip address. Can I use again struct sockaddr_in client; in the Transmit api ?? could some one help me ??