TCP Server that handles multiple client requests at the same time using multiple threads
Dependencies: EthernetInterface Server mbed-rtos mbed Threads
Revision 2:fe6ad47cd3eb, committed 2013-06-11
- Comitter:
- lemniskata
- Date:
- Tue Jun 11 19:26:40 2013 +0000
- Parent:
- 1:35d97f58d4d9
- Child:
- 3:91fa989f29e2
- Commit message:
- V4. RTOS handle multiple clients working
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Jun 11 18:27:47 2013 +0000
+++ b/main.cpp Tue Jun 11 19:26:40 2013 +0000
@@ -4,72 +4,40 @@
#define SERVER_PORT 7
#define BUFFLEN 100
-int bufsize=1024; //Size of the shared memory
-char *buffer; //This is the shared memory between the child threads
+char buffer[256];
-void Debug(void const *data)
-{
- TCPSocketServer server;
- server.bind(SERVER_PORT);
- server.listen();
-
- while (true) {
- printf("\nWait for new connection...\n");
- TCPSocketConnection client;
- server.accept(client);
- client.set_blocking(false, 1500); // Timeout after (1.5)s
-
- printf("Connection from: %s\n", client.get_address());
- char buffer[256];
- while (true) {
- int n = client.receive(buffer, sizeof(buffer));
- if (n <= 0) break;
-
- client.send_all(buffer, n);
- if (n <= 0) break;
- }
-
- client.close();
- }
-}
void Handle_client(void const *socket_data) {
+
int socket;
socket = (int)socket_data;
- //pc.printf("Socket connected #%d\n",socket);
-
- //pc.printf("Shared memory message is %s\n",buffer);
- lwip_recv(socket,buffer,bufsize,0);
+ int n = lwip_recv(socket ,buffer,sizeof(buffer),0);
- if (lwip_send(socket,buffer,strlen(buffer),0)!=strlen(buffer)) //send the content of the shared memory back to the socket
+ if (lwip_send(socket ,buffer,n,0)!=n) //send the content of the shared memory back to the socket
{
- // pc.printf("failed send\n");
+
}
-
- // pc.printf("Message sent successfully to socket %d\n",socket);
+
}
+osThreadDef(Handle_client, osPriorityNormal, DEFAULT_STACK_SIZE);
+
int main (void) {
-
+
EthernetInterface eth;
eth.init(); //Use DHCP
eth.connect();
- Thread t1(Debug, (void *) 1);
- while(1)
- {
+ // Thread t1(Debug, (void *) 1);
- }
- // pc.printf("IP Address is %s\n", eth.getIPAddress());
- /*int socket_server;
+ 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)
{
- // pc.printf("failed socket init\n");
return -1;
}
localHost.sin_family = AF_INET;
@@ -77,7 +45,7 @@
localHost.sin_addr.s_addr = INADDR_ANY;
if (lwip_bind(socket_server, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
- // pc.printf("failed socket bind init\n");
+
return -1;
}
if (lwip_listen(socket_server,3)<0)
@@ -93,11 +61,11 @@
new_socket = lwip_accept(socket_server, (struct sockaddr*) &localHost, &newSockRemoteHostLen);
if (new_socket < 0)
{
- // pc.printf("failed socket accept\n");
return -1;
- }
+ }
- Thread t(Handle_client, (void *) new_socket);
- }*/
+ osThreadCreate(osThread(Handle_client), (void *) new_socket);
+
+ }
}
\ No newline at end of file