Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

tcpServer.c

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /* This file has been written by Sergio Perez Alcañiz <serpeal@disca.upv.es>  */
00003 /*            Departamento de Informática de Sistemas y Computadores          */
00004 /*            Universidad Politécnica de Valencia                             */
00005 /*            Valencia (Spain)                                                */
00006 /*                                                                            */
00007 /* The RTL-lwIP project has been supported by the Spanish Government Research */
00008 /* Office (CICYT) under grant TIC2002-04123-C03-03                            */
00009 /******************************************************************************/
00010 
00011 #include <sys/types.h>
00012 #include <sys/socket.h>
00013 #include <netinet/in.h>
00014 #include <arpa/inet.h>
00015 #include <netdb.h>
00016 #include <stdio.h>
00017 #include <unistd.h> /* close */
00018 
00019 
00020 #define SUCCESS 0
00021 #define ERROR   1
00022 
00023 #define END_LINE 0x0A
00024 #define SERVER_PORT 10
00025 #define MAX_MSG 100
00026 
00027 int main (int argc, char *argv[]) {
00028   
00029   int sd, newSd, cliLen;
00030 
00031   struct sockaddr_in cliAddr, servAddr;
00032   char line[MAX_MSG];
00033 
00034 
00035   /* create socket */
00036   sd = socket(AF_INET, SOCK_STREAM, 0);
00037    if(sd<0) {
00038     perror("cannot open socket ");
00039     return ERROR;
00040   }
00041   
00042   /* bind server port */
00043   servAddr.sin_family = AF_INET;
00044   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00045   servAddr.sin_port = htons(SERVER_PORT);
00046   
00047   if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
00048     perror("cannot bind port ");
00049     return ERROR;
00050   }
00051 
00052   listen(sd,5);
00053   
00054     printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
00055 
00056     cliLen = sizeof(cliAddr);
00057     newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
00058     if(newSd<0) {
00059       perror("cannot accept connection ");
00060       return ERROR;
00061     }
00062     
00063     /* receive segments */
00064     while(read(newSd,line,10)!=0) {
00065       
00066       printf("%s: received from %s:TCP%d : %s\n", argv[0], 
00067              inet_ntoa(cliAddr.sin_addr),
00068              ntohs(cliAddr.sin_port), line);
00069     }
00070   
00071     close(sd);  
00072 
00073 }
00074 
00075 

Generated on Wed Jan 14 12:58:57 2004 for RTL-lwIP-0.4 by doxygen 1.3.4