Blog # 03 : TCP echo client-server [iterative]
An echo client-server performs the following steps:
- The client reads a line of text from its standard input and writes the line to the server.
- The server reads the line from its network input and echoes the line back to the client.
- The client reads the echoed line and prints it on its standard output.
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#define SA struct sockaddr
#define MAXLINE 4096
#define SERV_PORT 9877
void str_cli(FILE *fp, int sockfd)
{
char sendline[MAXLINE], recvline[MAXLINE];
while (fgets(sendline, MAXLINE, fp) != NULL) {
write(sockfd, sendline, strlen(sendline));
if (read(sockfd, recvline, MAXLINE) == 0){
printf("str_cli: server terminated prematurely\n");
return;
}
fputs(recvline, stdout);
bzero(sendline,strlen(sendline));
bzero(recvline,strlen(recvline));
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2){
printf("usage: tcpcli <IPaddress>");
return -1;
}
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
str_cli(stdin, sockfd);
exit(0);
}
TCP echo Server
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<errno.h>
#define SA struct sockaddr
#define LISTENQ 1024
#define MAXLINE 4096
#define SERV_PORT 9877
void str_echo(int sockfd)
{
ssize_t n;
char line[MAXLINE];
for ( ; ; ) {
if ( (n = read(sockfd, line, MAXLINE)) == 0)
return;
Writen(sockfd, line, n);
}
}
int main(int argc, char **argv)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
str_echo(connfd);
Close(connfd);
}
Close(listenfd);
}
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<errno.h>
#define SA struct sockaddr
#define LISTENQ 1024
#define MAXLINE 4096
#define SERV_PORT 9877
void str_echo(int sockfd)
{
ssize_t n;
char line[MAXLINE];
for ( ; ; ) {
if ( (n = read(sockfd, line, MAXLINE)) == 0)
return;
Writen(sockfd, line, n);
}
}
int main(int argc, char **argv)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
str_echo(connfd);
Close(connfd);
}
Close(listenfd);
}
The sucсeѕs of your buѕinеsѕ
сlients. You noω business consider gettіng somе form of initіаl inveѕtment iѕ needed?
Not thаt I am goіng to work. It's not uncommon to find that you are an electrician.
Here is my web site: hotel internet marketing services