TCP Server that handles multiple client requests at the same time by using multiple threads
Dependents: ThreadServer Server_Multi_Client
Server.cpp
- Committer:
- lemniskata
- Date:
- 2013-06-12
- Revision:
- 2:1b1f3ab29984
- Parent:
- 1:e1db78d5ccc1
- Child:
- 3:edde051ab024
File content as of revision 2:1b1f3ab29984:
/* ** File name: Server.cpp ** Descriptions: TCP server that handles multiple client requests in separate threads ** **------------------------------------------------------------------------------------------------------ ** Created by: Ivan Shindev ** Created date: 06/11/2013 ** Version: 1.0 ** Descriptions: The original version ** **------------------------------------------------------------------------------------------------------ ** Modified by: ** Modified date: ** Version: ** Descriptions: ********************************************************************************************************/ #include "Server.h" #include "mbed.h" #include "Motor.h" /*Handle_client handles the client request * * Default: Echo the clients request */ Motor motor1(P1_21,P1_22); Motor motor2(P1_23,P1_24); void Handle_client(void const *socket_data) { int socket; char buffer[256]; char extracted[256]; char extracted1[256]; int i1=0; int i2=0; socket = (int)socket_data; int n = lwip_recv(socket ,buffer,sizeof(buffer),0); //read from the client while(buffer[i1]!='\0') { if(buffer[i1]!=' ') { extracted[i2]=buffer[i1]; i2++; } else { break; } i1++; } extracted[i2]='\0'; i2=0; i1++; while(buffer[i1]!='\0') { extracted1[i2]=buffer[i1]; i2++; i1++; } extracted1[i2]='\0'; if(strcmp(extracted,"Motor1")==0) { strcpy(buffer,"OK Motor1"); if (lwip_send(socket ,buffer,strlen(buffer)*sizeof(char),0)!=n) //send the content back to the socket { } int speed=atoi(extracted1); if((speed>=-100)&&(speed<=100)) { motor1.speed(((float)speed)/100); } } else if(strcmp(extracted,"Motor2")==0) { strcpy(buffer,"OK Motor2"); if (lwip_send(socket ,buffer,strlen(buffer)*sizeof(char),0)!=n) //send the content back to the socket { } int speed=atoi(extracted1); if((speed>=-100)&&(speed<=100)) { motor2.speed(((float)speed)/100); } } else { strcpy(buffer,"Unknown command"); if (lwip_send(socket ,buffer,strlen(buffer)*sizeof(char),0)!=n) //send the content back to the socket { } } lwip_close(socket); } osThreadDef(Handle_client, osPriorityNormal, DEFAULT_STACK_SIZE); Server::Server(int port, int max_number_of_clients): _port(port), _max_number_of_clients(max_number_of_clients){ } int Server::Start() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); int socket_server; struct sockaddr_in localHost; memset(&localHost, 0, sizeof(localHost)); int new_socket; if( (socket_server= lwip_socket(AF_INET, SOCK_STREAM, 0))<0) { return -1; } localHost.sin_family = AF_INET; localHost.sin_port = htons(_port); //port localHost.sin_addr.s_addr = INADDR_ANY; //localhost address if (lwip_bind(socket_server, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) { return -1; } if (lwip_listen(socket_server,5)<0) { perror("listen"); exit(EXIT_FAILURE); } socklen_t newSockRemoteHostLen = sizeof(localHost); while(1) { new_socket = lwip_accept(socket_server, (struct sockaddr*) &localHost, &newSockRemoteHostLen); if (new_socket < 0) { return -1; } osThreadCreate(osThread(Handle_client), (void *) new_socket); } }