Linux Network programming Echo server

Source: Internet
Author: User
Tags socket connect socket error htons

Echo_server

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <www.qixoo.qixoo.com/sys/types.h>
#include <signal.h>
#include <memory.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>



int sockfd;
void Sign_handler (int signo)
{
if (signo==sigint)
{
printf ("Server is Stop\n");
Close (SOCKFD);
}
if (SIGNO==SIGCHLD)
{
printf ("Server child is close\n");
Wait (0);
}
}

void Server_do (int fd)
{
Char Buffer[bufsiz];
ssize_t size;
while (1) {
printf ("Ready to read\n");
memset (buffer,0,sizeof (buffer));
if ((Size=read (fd,buffer,sizeof (buffer))) <0) {
Perror ("Protocal error\n");
Break
}else if (size==0) {
Break
}else{
printf ("Number of received bytes=%ld\n", size);
buffer[size]= ' + ';
printf ("%s\n", buffer);
if (Write (fd,buffer,sizeof (buffer)) <0) {
if (errno==epipe) {
Break
}
Perror ("Patrol error\n");
}
}
}
}

int main (int argc,char *argv[])
{
if (argc<2)
{
printf ("Usage:%s #port \ n", argv[0]);
Exit (1);
}
Signal Registration
Signal (Sigint,sign_handler);
Signal (Sigchld,sign_handler);
/*create socket*/
Sockfd=socket (af_inet,sock_stream,0);
/* BIND socket Address */
struct sockaddr_in serveraddr;
Serveraddr.sin_family=af_inet;
Serveraddr.sin_port=htons (Atoi (argv[1));
Serveraddr.sin_addr.s_addr=inaddr_any;
if (Bind (SOCKFD, (struct sockaddr*) &serveraddr,sizeof (serveraddr)) <0) {
Perror ("Socket bind fail\n");
Exit (1);
}
/*listen*/
if (Listen (sockfd,10) <0) {
Perror ("Socket Listen fail\n");
Exit (1);
}
/*accept*/
struct sockaddr_in clientaddr;
socklen_t clientaddr_len=sizeof (CLIENTADDR);
while (1)
{
int Fd=accept (SOCKFD, (struct sockaddr*) &clientaddr,&clientaddr_len);
if (fd<0) {
Perror ("Socket Accpet fail\n");
Exit (1);
}
/*fork Child progress*/
pid_t pid=fork ();
if (pid<0) {
Perror ("fork Child pid error\n");
Continue
}else if (pid==0) {
Close (SOCKFD);//SOCKFD off in a child process
printf ("Client from%s\n", Inet_ntoa (CLIENTADDR.SIN_ADDR));
SERVER_DO (FD);
Close (FD);
Break
}else{
Close (FD);
}
}
Close (SOCKFD);
return 0;
}



Echo_client

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>



int main (int argc,char *argv[])
{
if (argc<3)
{
printf ("Usage #ip #port \ n");
Exit (1);
}

/* Create socket*/
int Sockfd=socket (af_inet,sock_stream,0);
if (sockfd<0)
{
Perror ("socket error");
Exit (1);
}
struct sockaddr_in serveraddr;
memset (&serveraddr,0,sizeof (SERVERADDR));
Serveraddr.sin_family=af_inet;
Serveraddr.sin_port=htons (Atoi (argv[2));
Inet_pton (AF_INET,ARGV[1],&AMP;SERVERADDR.SIN_ADDR.S_ADDR);
/**connect**/
if (Connect (SOCKFD, (struct sockaddr*) &serveraddr,sizeof (serveraddr)) <0)
{
Perror ("Socket connect fail\n");
Exit (1);
}
/* Bidirectional Communication */
Char Buff[bufsiz];
size_t size;
Char *prompt= ">";
while (1)
{
memset (buff,0,sizeof (buff));
Write (stdout_fileno,prompt,1);
Size=read (stdin_fileno,buff,sizeof (buff));
if (size<0) continue;
buff[size-1]= ' + ';

if (Write (sockfd,buff,sizeof (buff)) <0) {
Perror ("Write msg error\n");
Continue
}else{
if (read (sockfd,buff,sizeof (buff)) <0) {
Perror ("read msg error\n");
Continue
}else{
printf ("%s\n", buff);
}
}
}

}

Linux Network programming Echo server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.