#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
Include dependency graph for tcpServer.c:
Go to the source code of this file.
Defines | |
#define | SUCCESS 0 |
#define | ERROR 1 |
#define | END_LINE 0x0A |
#define | SERVER_PORT 10 |
#define | MAX_MSG 100 |
Functions | |
int | main (int argc, char *argv[]) |
|
Definition at line 23 of file tcpServer.c. |
|
Definition at line 21 of file tcpServer.c. Referenced by main(). |
|
Definition at line 25 of file tcpServer.c. |
|
Definition at line 24 of file tcpServer.c. |
|
Definition at line 20 of file tcpServer.c. |
|
Definition at line 27 of file tcpServer.c. References AF_INET, ERROR, htonl, htons, INADDR_ANY, inet_ntoa(), MAX_MSG, ntohs, in_addr::s_addr, SERVER_PORT, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, and SOCK_STREAM.
00027 { 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 } |
Here is the call graph for this function: