root / trunk / code / projects / mapping / python / server.c @ 902
History | View | Annotate | Download (1.3 KB)
| 1 | /* A simple server in the internet domain using TCP
|
|---|---|
| 2 | The port number is passed as an argument */ |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <strings.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <netinet/in.h> |
| 11 | |
| 12 | #include "netserv.h" |
| 13 | #include "robots.h" |
| 14 | #include "wireless.h" |
| 15 | |
| 16 | void donothin()
|
| 17 | {
|
| 18 | return;
|
| 19 | } |
| 20 | |
| 21 | |
| 22 | int main(int argc, char *argv[]) |
| 23 | {
|
| 24 | char buffer[256]; |
| 25 | char outbuf[256]; |
| 26 | int n, newsockfd, len;
|
| 27 | Packet *received; |
| 28 | |
| 29 | |
| 30 | if (argc < 2) { |
| 31 | fprintf(stderr,"usage: %s <port number>\n", argv[0]); |
| 32 | exit(1);
|
| 33 | } |
| 34 | newsockfd = make_sock(atoi(argv[1]));
|
| 35 | |
| 36 | make_listener(); |
| 37 | printf("Robot listener created.\n");
|
| 38 | |
| 39 | bzero(buffer,256);
|
| 40 | |
| 41 | while (1) |
| 42 | {
|
| 43 | wl_do(); |
| 44 | |
| 45 | /* this is a completely useless queue, since wl_do only reads one packet at a time */
|
| 46 | received = dequeue(); |
| 47 | /* oh well */
|
| 48 | |
| 49 | if (received)
|
| 50 | {
|
| 51 | fprintf(stdout, "len: %d\n", buffer[1]); |
| 52 | buffer[0] = (char) received->source; |
| 53 | buffer[1] = (char) received->length-2; |
| 54 | buffer[2] = (char) received->type; |
| 55 | memcpy(buffer+3, received->packet, received->length);
|
| 56 | |
| 57 | n = write(newsockfd, buffer, 200);
|
| 58 | free(received); |
| 59 | bzero(buffer, 256);
|
| 60 | } |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | void error(char *msg) |
| 66 | {
|
| 67 | printf(msg); |
| 68 | exit(1);
|
| 69 | } |