What Is Client Connect


  • 19 Dec, 2011

    A client application creates a socket(3N)and then issues a connect(3N) to a service specifiedin a sockaddr_in structure: int tcpopen(host,service) char *service, *host; { int unit; struct sockaddr_in sin; struct servent *sp; struct hostent *hp;if ((sp=getservbyname(service,"tcp")) == NULL) then error... if ((hp=gethostbyname(host)) == NULL) then Ierror... bzero((char *)&sin, sizeof(sin)) etc...if ((unit=socket(AF_INET,SOCK_STREAM,0)) < 0) then error... if (connect(unit,&sin,sizeof(sin)) <0) then error... return(unit); }The result returned is a file descriptor which is connected to a server process. A communications channel on which one can conduct an application specific protocol.Client Communication:Having connected a socket to a server to establish a file descriptor communication is with the usual Unix I/O calls. You have Inter Process Communication (or IPC) to a server.Many programmers turn file descriptors into stdio(3S) streams so they can use fputs, fgets, fprintf, etc. -- use fdopen(3S). main(argc,argv) int argc; char *argv[]; { int unit,i; char buf[BUFSIZ]; FILE *sockin,*sockout;if ((unit=tcpopen(WHOHOST,WHOPORT)) < 0) then error... sockin=fdopen(unit,"r"); sockout=fdopen(unit,"w"); etc... fprintf(sockout,"%sn",argv[i]); etc... while (fgets(buf,BUFSIZ,sockin)) etc...Stdio Buffers:Stdio streams have powerful manipulation tools (eg. fscanf is amazing). But beware, streams are buffered! This means a well placed fflush(3S) is often required to flush a buffer to the peer. fprintf(sockout,"%sn",argv[i]); fflush(sockout); while (fgets(buf,BUFSIZ,sockin)) etc...Many client/server protocols are client driven -- the client sends a command and expects an answer. The server won't see the command if the client doesn't flush the output. Likewise, the client won't see the answer if the server doesn't flush it's output.Watch out for client and server blocking -- both waiting for input from the other.

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.