diff options
author | Ian C <ianc@noddybox.co.uk> | 2010-11-08 15:34:07 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2010-11-08 15:34:07 +0000 |
commit | e3c10bc9343baa5ea3dcc57063d28b3742479245 (patch) | |
tree | 778bfec9d4e901f7cf5cc432b085de40c9fa2c89 | |
parent | d901c4d5078192e113542648cb49e8700db3b142 (diff) |
Tidied up serv.c
-rw-r--r-- | serv.c | 118 |
1 files changed, 63 insertions, 55 deletions
@@ -1,105 +1,113 @@ /* A simple server to attach to */ -#include <sys/types.h> - +#include <stdlib.h> #include <stdio.h> +#include <string.h> #include <errno.h> +#include <sys/types.h> + #include <sys/socket.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <netdb.h> -char *name; -int sock_fd; -int connect_fd; +static const char *name; + +static int Socket(int p) +{ + struct sockaddr_in addr; + int sock_fd; + + if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1) + { + perror("socket"); + exit(EXIT_FAILURE); + } + + /* Bind port to address + */ + addr.sin_family=AF_INET; + addr.sin_addr.s_addr=INADDR_ANY; + addr.sin_port=htons(p); + + if (bind(sock_fd,(void*)&addr,sizeof(addr))==-1) + { + perror("bind"); + exit(EXIT_FAILURE); + } -void Socket(short); + if (listen(sock_fd,5)==-1) + { + perror("listen"); + exit(EXIT_FAILURE); + } -main(argc,argv) -int argc; -char *argv[]; + return sock_fd; +} +int main(int argc, char *argv[]) { struct sockaddr_in addr; char buff[1024]; int len; int addrlen; + int sock_fd; + int connect_fd; name=argv[0]; if (argc!=2) - { - fprintf(stderr,"%s: usage %s port\n",name,name); + { + fprintf(stderr,"%s: usage %s port\n", name, name); exit(1); - } + } - Socket((short)atoi(argv[1])); + sock_fd = Socket((short)atoi(argv[1])); printf("%s: socket bound\n",name); while(1) - { + { printf("%s: accepting\n",name); + if ((connect_fd=accept(sock_fd,NULL,0))==-1) - { - perror(name); + { + perror("accept"); exit(1); - } + } addrlen=sizeof(addr); - if (getsockname(connect_fd,&addr,&addrlen)!=0) - perror(name); + + if (getsockname(connect_fd,(void*)&addr,&addrlen)!=0) + { + perror("getsockname"); + } printf("%s: connection ON port %d\n",name,ntohs(addr.sin_port)); addrlen=sizeof(addr); - if (getpeername(connect_fd,&addr,&addrlen)!=0) - perror(name); + + if (getpeername(connect_fd,(void*)&addr,&addrlen)!=0) + { + perror("getpeername"); + } printf("%s: connection FROM port %d\n",name,ntohs(addr.sin_port)); while((len=read(connect_fd,buff,1024))>0) - { + { buff[len]=0; printf("%s: recieved '%s'\n",name,buff); - write(connect_fd,buff,len); - } - - perror(name); - - close(connect_fd); } - return(0); -} - - -void Socket(short p) -{ - struct sockaddr_in addr; - - if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1) + if (len == -1) { - perror(name); - exit(1); + perror("read"); } - /* Bind port to address - */ - addr.sin_family=AF_INET; - addr.sin_addr.s_addr=INADDR_ANY; - addr.sin_port=htons(p); - - if (bind(sock_fd,&addr,sizeof(addr))==-1) - { - perror(name); - exit(1); - } + close(connect_fd); + } - if (listen(sock_fd,5)==-1) - { - perror(name); - exit(1); - } + return EXIT_SUCCESS; } |