Loading...
Select Version
&pagelevel(3)&pagelevel
The following client program in the connection-oriented service is described in detail in section "Connection-oriented client/server model". The client sets up a transport connection to a server, receives data from the server and writes the data to its standard output. The connection is shut down using the orderly connection shutdown of the transport interface. The client can communicate with any of the connection-oriented servers described in the examples in this chapter.
#include <stdio.h> #include <xti.h> #include <fcntl.h> #include <netinet/in.h> #include <sys/socket.h> #define SRV_ADDR 0x7F000001 #define SRV_PORT 8888 main() { int fd; int nbytes; int flags = 0; char buf[1024]; struct t_call *sndcall; struct sockaddr_in *sin; if ((fd = t_open("/dev/tcp", O_RDWR, NULL)) < 0) { t_error("t_open() failed"); exit(1); } if (t_bind(fd, NULL, NULL) < 0) { t_error("t_bind() failed"); exit(2); } if ((sndcall = (struct t_call *)t_alloc(fd,T_CALL, T_ADDR)) == NULL) { t_error("t_alloc() failed"); exit(3); } sndcall->addr.len=sizeof(struct sockaddr_in); sin = (struct sockaddr_in *)sndcall->addr.buf; sin->sin_family=AF_INET; sin->sin_port=htons(SRV_PORT); sin->sin_addr.s_addr=htonl(SRV_ADDR); if (t_connect(fd, sndcall, NULL) < 0) { t_error("t_connect() failed for fd"); exit(4); } while ((nbytes = t_rcv(fd, buf, 1024, &flags)) != -1) { if (fwrite(buf, 1, nbytes, stdout) == 0) { fprintf(stderr, "fwrite() failed\n"); exit(5); } } if ((t_errno == TLOOK) && (t_look(fd) == T_ORDREL)) { if (t_rcvrel(fd) < 0) { t_error("t_rcvrel() failed"); exit(6); } if (t_sndrel(fd) < 0) { t_error("t_sndrel() failed"); exit(7); } exit(0); } t_error("t_rcv() failed"); exit(8); }