Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Re: server programming in "C"
/com ** Sample Waterloo C sockets program. /com ** See the comment about DNS servers below. /com ** Tests functions: connect(), send(), recv(), muresolve(), /com ** ? musa2e(), muse2a() /inc watc #include <stdio.h> #include <string.h> ? /* for memset(), etc. */ #include <stdlib.h> ? /* for exit() */ #include <errno.h> ? ?/* for access to errno */ /* prototype for local function */ static void crlf2nl(unsigned char *buf, int len); int main(void) { int i,n,rc,wlen; int sd; ? ?/* socket descriptor */ unsigned long ipaddr; ?/* 32-bit IP address */ unsigned char *sitename=""; ?/* site to connect to */ unsigned char *ipnum="132.206.120.4"; ?/* numeric IP addr to use ? ? ? ? ? ? ? ? ? ? if sitename cannot be resolved by DNS */ int port=80; ? ?/* port number to connect to */ struct sockaddr_in saddr; int numreads,totbytes; unsigned char *wrdata= \ ? ? "GET /msi/http/music.html HTTP/1.0\r\x25\r\x25"; ? ? ? ?/* HTML request, to send to a web server. ? ? ? ? ? Note use of \r\x25 for CRLF. WatC Ebcdic: 0x0D 0x25. ? ? ? ? ? muse2a() and etoa() translate it to Ascii CR LF = 0x0D 0x0A. ? ? ? ?*/ unsigned char lastch,buf[400]; printf("\nWatC sockets sample program\n\n"); /* Resolve the site name to an IP address */ /* Note: The muresolve functions uses a DNS server to lookup the ? ?IP address for the given site name (domain name).? This requires ? ?that you have 1 or more DNS server IP addresses defined in ? ?your $tcp:tcpip.config file, as NAMESERV records. ? ?Normally the DNS (Domain Name System) server IP addresses are ? ?provided by your ISP (Internet Service Provider). ? ?If you don't have any DNS servers, you can add the following ? ?record to file $tcp:ip.alias : ? 132.206.120.4 ? ?Or, in this source file, set: ?sitename="132.206.120.4" */ rc = muresolve(sitename,&ipaddr); if(rc < 0) ? { ? ? ? ? ? printf("** Error: unable to resolve site name %s\n" ? ? ? ? ? ?" ? ? muresolve() gave rc=%d\n",sitename,rc); ? ? ? printf("** Using a specific numeric IP address instead: %s\n", ? ? ? ? ? ?ipnum); ? ? ?ipaddr=inet_addr(ipnum); ? } sd = socket(AF_INET,SOCK_STREAM,0); printf("sd = %d from socket() create\n",sd); if(sd < 0) ? ? ? ?{ ? ? ? ? ? printf("** errno=%d\n",errno); exit(1); ? ? ? ?} /* Set up the socket address structure for connect() */ memset(&saddr,0,sizeof(saddr)); ?/* init. it to 0 */ saddr.sin_family=AF_INET; saddr.sin_port=htons((unsigned short)port); saddr.sin_addr.s_addr=ipaddr; printf("Connecting to %s %s port %d\n",sitename, \ ? ? ? ?inet_ntoa(saddr.sin_addr),port); rc = connect(sd,(struct sockaddr *)&saddr,sizeof(saddr)); printf("rc=%d from connect()\n",rc); if(rc<0) ? { printf("** errno=%d\n",errno); ? ? if(errno==ECONNREFUSED) ? ? ? ? printf(" ? ?ECONNREFUSED - Connection refused\n"); ? ? if(errno==EHOSTUNREACH) ? ? ? ? printf(" ? ?EHOSTUNREACH - Host unreachable\n"); ? ? exit(1); ? } /* Write data to the socket */ wlen=strlen(wrdata); printf("# of bytes to be written: wlen=%d\ndata=|%s|\n",wlen,wrdata); muse2a(wrdata,wlen); ?/* convert from Ebcdic to Ascii! */ n=send(sd,wrdata,wlen,0); if(n<0) printf("** rc=%d from send(). errno=%d\n",n,errno); else printf(" %d bytes written to socket\n",n); /* Loop to read from the socket and display the data */ printf("Reading from socket...\n"); for(numreads=0,totbytes=0,lastch=0;;) ? { numreads++; ? ? n=recv(sd,buf,sizeof(buf),0); ? ? if(n<0) { printf("** rc=%d from recv(). errno=%d\n",n,errno); ? ? ? ? ? ? ? break; } ? ? if(n==0) break; ?/* end of data, server shutdown socket */ ? ? totbytes+=n; ? ? musa2e(buf,n); ?/* convert from Ascii to Ebcdic! */ ? ? crlf2nl(buf,n); ?/* then change all CRLF to (blank,\n) */ ? ? ? ? /* Fix for case of CRLF split between buffers: */ ? ? ? ? if(lastch==0x0D && buf[0]==0x25) buf[0]='\n'; ? ? ? ? lastch=buf[n-1]; ? ? ? ? if(lastch==0x0D) buf[n-1]=' '; ? ? for(i=0;i<n;i++) putc(buf[i],stdout); ? } printf(" numreads=%d totbytes=%d\n",numreads,totbytes); rc=close(sd); printf("rc=%d from close() of socket\n",rc); if(rc<0) { printf("** errno=%d\n",errno); exit(1); } return 0; } ? /* end of main */ /* ---------------------------------------------------------------- */ static void crlf2nl(unsigned char *buf, int len) /* Change all Ebcdic CRLF pairs to (blank,\n) */ { int i,n1; unsigned char ebc_crlf[2]={0x0D,0x25}; unsigned char blknl[2]={' ','\n'}; if(len<2) return; n1=len-1; for(i=0;i<n1;i++) ? { if(memcmp(buf+i,ebc_crlf,2)==0) ? ? ? { memcpy(buf+i,blknl,2); ? ? ? ? i++; ? ? ? } ? } } ?/* end of crlf2nl */ On Mon, Jun 21, 2021 at 12:48 PM Joe Monk <joemonk64@...> wrote:
|
to navigate to use esc to dismiss