One Shot TCP Client/Server Pair
#include <sys/types.h> /******* Demo of One-Shot TCP *******/
#include <sys/socket.h> /******* Client/Server. *******/
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
int active;
main()
{
struct hostent *hptr;
struct sockaddr_in sin;
char s[256];
int readn(int active, char *buf, int bufsize);
active = socket(AF_INET, SOCK_STREAM, 0); /** Test return for -1! **/
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_port = htons(4444);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("153.18.17.11");
if (connect(active, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("Bad connection.");
exit(1);
}
printf("Enter string: ");
memset(s, 0, 256);
fgets(s, 256, stdin);
writen(active,s,256);
memset(s, 0, 256);
readn(active,s,256);
printf("From server: %s\n", s);
close(active);
}
int readn(int fd, char *buf, int bytes)
{
int nleft;
int nread;
char *mover = buf;
nleft = bytes;
while (nleft > 0){
if ((nread = read(fd,mover,nleft)) < 0)
return -1; /**** Error!! ****/
else if (nread == 0)
break; /**** EOF! ****/
nleft -= nread;
mover += nread;
}
return (bytes - nleft);
}
int writen(int fd, char *buf, int bytes)
{
int nleft;
int nwritten;
char *mover = buf;
nleft = bytes;
while (nleft > 0){
if ((nwritten = write(fd,mover,nleft)) < 0)
return -1; /**** Error!! ****/
nleft -= nwritten;
mover += nwritten;
}
return bytes;
}
#define MAXBUF 256 /********** This is the server. *********/
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
int active;
main()
{
struct hostent *hptr;
int passive, addr_len, n, status;
char buf[256];
struct sockaddr_in sin, client;
int readn(int active, char *buffer, int buffer_size);
int writen(int active, char *buffer, int buffer_size);
passive = socket(AF_INET, SOCK_STREAM, 0);
memset(&sin, 0, sizeof(sin));
sin.sin_port = htons(4444);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(passive, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("Binding error!");
exit(1);
}
if (listen(passive, 1) < 0)
{
perror("Listen error!");
exit(1);
}
addr_len = sizeof(struct sockaddr_in);
if ((active = accept(passive, (struct sockaddr *)&client, &addr_len)) < 0)
{
perror("Accept error!");
exit(1);
}
memset(buf, 0, 256);
n = readn(active, buf, 256);
printf("From client: %s\n\n", buf);
writen(active, buf, 256) ;
}
int readn(int fd, char *buf, int bytes)
{
int nleft;
int nread;
char *mover = buf;
nleft = bytes;
while (nleft > 0){
if ((nread = read(fd,mover,nleft)) < 0)
return -1; /**** Error!! ****/
else if (nread == 0)
break; /**** EOF! ****/
nleft -= nread;
mover += nread;
}
return (bytes - nleft);
}
int writen(int fd, char *buf, int bytes)
{
int nleft;
int nwritten;
char *mover = buf;
nleft = bytes;
while (nleft > 0){
if ((nwritten = write(fd,mover,nleft)) < 0)
return -1; /**** Error!! ****/
nleft -= nwritten;
mover += nwritten;
}
return bytes;
}
/******************************* Output Below ***************************/
$ s&
[1] 21447
$ c
Enter string: hello
From client: hello
From server: hello