summaryrefslogtreecommitdiff
path: root/sock.c
diff options
context:
space:
mode:
Diffstat (limited to 'sock.c')
-rw-r--r--sock.c190
1 files changed, 107 insertions, 83 deletions
diff --git a/sock.c b/sock.c
index 03a7c39..f86b41a 100644
--- a/sock.c
+++ b/sock.c
@@ -1,130 +1,154 @@
/*
Reader/writer to TCP/IP socket
*/
+#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>
-#include <stdio.h>
-#include <errno.h>
-
-char *name;
-int sock;
-
-char *GetLine();
-int Connect();
-
-main(argc,argv)
-int argc;
-char *argv[];
-
-{
- struct sockaddr_in addr;
- int addrlen;
- char *p;
- char buff[1024];
- int len;
-
- name=argv[0];
-
- if (argc<3)
- {
- fprintf(stderr,"%s: usage %s host port [nowrite]\n",name,name);
- exit(1);
- }
-
- Connect(argv[1],atoi(argv[2]));
-
- /* Test to see how to get the connected local port number
- */
- addrlen=sizeof(addr);
- if (getsockname(sock,&addr,&addrlen)!=0)
- perror(name);
-
- printf("%s: bound through port %d\n",name,ntohs(addr.sin_port));
-
- while((argc==4)||(p=GetLine()))
- {
- if ((argc!=4)&&(write(sock,p,strlen(p))==-1))
- {
- perror(name);
- close(sock);
- exit(1);
- }
-
- if ((len=read(sock,buff,1024))<=0)
- {
- perror(name);
- close(sock);
- exit(1);
- }
- buff[len]=0;
- printf("%s\n",buff);
- }
-
- close(sock);
- printf("\n");
-
- return(0);
-}
-
-
-char *GetLine()
+static const char *name;
+static const char *GetLine(void)
{
static char buff[1024];
int l;
if (feof(stdin))
+ {
return(NULL);
+ }
printf("> ");
- if (!gets(buff))
+ if (!fgets(buff, sizeof buff, stdin))
+ {
return(NULL);
+ }
l=strlen(buff);
- if (buff[l-1]=='\n')
+ if (l > 0 && buff[l-1]=='\n')
+ {
buff[l-1]=0;
+ }
if (strlen(buff))
- return(buff);
+ {
+ return buff;
+ }
else
- return(GetLine());
+ {
+ return GetLine();
+ }
}
-int Connect(n,p)
-char *n;
-int p;
-
+static int Connect(const char *n, int p)
{
struct hostent *remote;
struct sockaddr_in addr;
+ int sock;
if (!(remote=gethostbyname(n)))
- {
+ {
fprintf(stderr,"%s: unknown host %s\n",name,n);
- exit(1);
- }
+ exit(EXIT_FAILURE);
+ }
- bcopy(remote->h_addr,&addr.sin_addr,remote->h_length);
+ memcpy(&addr.sin_addr, remote->h_addr, remote->h_length);
if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
+ {
perror(name);
- exit(1);
- }
+ exit(EXIT_FAILURE);
+ }
addr.sin_family=AF_INET;
addr.sin_port=htons(p);
- if (connect(sock,&addr,sizeof(addr))==-1)
- {
+ if (connect(sock,(void*)&addr,sizeof(addr))==-1)
+ {
+ perror(name);
+ exit(EXIT_FAILURE);
+ }
+
+ return sock;
+}
+
+int main(int argc, char *argv[])
+{
+ struct sockaddr_in addr;
+ int addrlen;
+ const char *p;
+ char buff[1024];
+ int len;
+ int sock;
+ int no_read;
+ int no_write;
+
+ name=argv[0];
+
+ if (argc<3)
+ {
+ fprintf(stderr,"%s: usage %s host port [nowrite|noread]\n",name,name);
+ exit(EXIT_FAILURE);
+ }
+
+ if (argc > 3)
+ {
+ no_read = strcmp(argv[3], "noread") == 0;
+ no_write = strcmp(argv[3], "nowrite") == 0;
+ }
+ else
+ {
+ no_read = 0;
+ no_write = 0;
+ }
+
+ sock = Connect(argv[1],atoi(argv[2]));
+
+ /* Test to see how to get the connected local port number
+ */
+ addrlen=sizeof(addr);
+
+ if (getsockname(sock,(void*)&addr,&addrlen)!=0)
+ {
perror(name);
- exit(1);
+ }
+
+ printf("%s: bound through port %d\n",name,ntohs(addr.sin_port));
+
+ while(no_write || (p=GetLine()))
+ {
+ if (!no_write && (write(sock,p,strlen(p))==-1))
+ {
+ perror(name);
+ close(sock);
+ exit(EXIT_FAILURE);
}
+
+ if (!no_read)
+ {
+ if ((len=read(sock,buff,1024))<=0)
+ {
+ perror(name);
+ close(sock);
+ exit(EXIT_FAILURE);
+ }
+
+ buff[len]=0;
+ printf("%s\n",buff);
+ }
+ }
+
+ close(sock);
+ printf("\n");
+
+ return EXIT_SUCCESS;
}