giuliomoro@24: ///* UDP client in the internet domain */ giuliomoro@24: giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include "../include/client.h" giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: andrewm@70: #define MESSAGE_BUFF_LEN 1024 giuliomoro@32: #define MAX_VAR_STRING 20 giuliomoro@24: giuliomoro@24: struct sockaddr_in outServer, inServer; giuliomoro@24: int outSock, inSock, n, length; giuliomoro@24: socklen_t fromlen; giuliomoro@24: struct sockaddr_in from; giuliomoro@24: char inBuffer[1024]; giuliomoro@24: char variableString[MAX_VAR_STRING]; giuliomoro@24: giuliomoro@24: int setupSockets(int receivePort, int transmitPort, char const*serverName){ giuliomoro@24: //setup transmitter giuliomoro@24: printf("receivePort: %d; transmitPort: %d; serverName: %s\n",receivePort, transmitPort, serverName); giuliomoro@24: outSock= socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@24: outServer.sin_port = htons(transmitPort); giuliomoro@24: if (outSock < 0){ giuliomoro@24: error("Opening out socket"); giuliomoro@24: return -1; giuliomoro@24: } giuliomoro@24: outServer.sin_family = AF_INET; giuliomoro@24: inet_pton(AF_INET,serverName,&outServer.sin_addr); giuliomoro@24: giuliomoro@24: //setup receiver giuliomoro@24: inSock=socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@24: if (inSock < 0){ giuliomoro@24: return -1; giuliomoro@24: error("Opening in socket"); giuliomoro@24: } giuliomoro@24: length = sizeof(inServer); giuliomoro@24: inServer.sin_family=AF_INET; giuliomoro@24: inServer.sin_addr.s_addr=INADDR_ANY; giuliomoro@24: inServer.sin_port=htons(receivePort); giuliomoro@24: if (bind(inSock,(struct sockaddr *)&inServer,length)<0) giuliomoro@24: error("binding"); giuliomoro@24: fromlen = sizeof(struct sockaddr_in); giuliomoro@24: return 0; giuliomoro@24: } giuliomoro@24: int sendMessage(networkData message) giuliomoro@24: { giuliomoro@24: unsigned int length; andrewm@70: char buffer[MESSAGE_BUFF_LEN]; giuliomoro@24: length=sizeof(struct sockaddr_in); giuliomoro@24: int k=0; giuliomoro@24: k=sprintf(buffer+k, "%8d;",*message.counter); giuliomoro@24: for(int j=0; jMESSAGE_BUFF_LEN - 20) //safety margin giuliomoro@24: continue; giuliomoro@24: } giuliomoro@24: sprintf(buffer+k,"\n"); giuliomoro@24: // printf(buffer); giuliomoro@24: n=sendto(outSock,buffer, giuliomoro@24: strlen(buffer),0,(const struct sockaddr *)&outServer,length); giuliomoro@24: if (n < 0) error("Sendto"); giuliomoro@24: return 0; giuliomoro@24: } andrewm@70: andrewm@70: int sendAudio(networkAudio *audio) giuliomoro@24: { andrewm@70: unsigned int length; andrewm@70: length=sizeof(struct sockaddr_in); andrewm@70: // for(int k=0; kbuffers[!audio->currentBuffer],NETWORK_AUDIO_BUFFER_SIZE*sizeof(float),0,(const struct sockaddr *)&outServer,length); andrewm@70: if (n < 0) error("Sendto"); andrewm@70: audio->doneOnTime=1; andrewm@70: return 0; andrewm@70: } giuliomoro@24: giuliomoro@24: int receiveMessage(networkData message){ giuliomoro@24: struct timeval stTimeOut; giuliomoro@24: fd_set stReadFDS; giuliomoro@24: FD_ZERO(&stReadFDS); giuliomoro@24: // Timeout of one second giuliomoro@24: stTimeOut.tv_sec = 0; giuliomoro@25: stTimeOut.tv_usec = 0; giuliomoro@24: FD_SET(inSock, &stReadFDS); giuliomoro@24: giuliomoro@24: int t = select(inSock+1, &stReadFDS, NULL, NULL, &stTimeOut); giuliomoro@24: if (t == -1) { giuliomoro@24: rt_fprintf(stderr, "Call to select() failed"); giuliomoro@24: return -1; giuliomoro@24: } giuliomoro@24: else if (t != 0) { giuliomoro@24: if (FD_ISSET(inSock, &stReadFDS)) { giuliomoro@24: // printf("There is data pending to be read..."); // Read data with recv() giuliomoro@24: int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen); giuliomoro@24: if (n < 0){ giuliomoro@24: rt_fprintf(stderr,"Error while receiving"); giuliomoro@24: return -1; giuliomoro@24: } giuliomoro@24: printf("Received a datagram: "); andrewm@70: printf(inBuffer); giuliomoro@24: //the worst parser ever giuliomoro@24: int previousN=0; giuliomoro@24: int currentVariable=0; andrewm@70: for(int n=0; inBuffer[n]!=0 && currentVariable