Unxi domain protocol programming server (TCP/UDP)

Source: Internet
Author: User

// Unixserver. c

# Include <stdarg. h>
# Include <stdio. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <errno. h>
# Include <sys/UN. h>
Void error (INT status, int err, char * FMT ,...){
Va_list AP;
Va_start (AP, FMT );
Vfprintf (stderr, FMT, AP );
Va_end (AP );
If (ERR)
Fprintf (stderr, ": % s (% d)/n", strerror (ERR), err );
If (Status)
Exit (Status );
}

INT server (char * pathname, char * protocol ){
Struct sockaddr_un server;
Int S, type;
Const int on = 1;
If (strcmp (protocol, "TCP") = 0)
Type = sock_stream;
Else
Type = sock_dgram;
 
If (S = socket (af_local, type, 0) <= 0)
Error (1, 0, "Create socket failed! ");

Unlink (pathname );
 
Bzero (& server, sizeof (server ));
Server. sun_family = af_local;
Strcpy (server. sun_path, pathname );
 
If (BIND (S, (struct sockaddr *) & server, sizeof (server )))
Error (1, errno, "bind failed! ");
If (type = sock_stream) & listen (S, 5 ))
Error (1, errno, "Listen error! ");
Return S;
}

// Tcpunixserver. c

# Include <stdarg. h>
# Include <stdio. h>
# Include <signal. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <errno. h>
# Include <sys/time. h>
# Include <sys/Wait. H>
# Include <sys/select. h>
# Include <sys/UN. h>
Void sig_child (){
Pid_t PID;
Int status;
While (pid = waitpid (-1, & status, wnohang)> 0)
Printf ("% d terminated! /N ", pid );
// While (wait (& Status)> 0 );
}

Int main (INT argc, char ** argv ){
Struct sockaddr_un remote;
Char * pathname;
Int S, C, Len, FD;
Pid_t PID;
Fd_set rset, allset;
Int client [1024];
Int I, Maxi, maxfd, nready;
Char buff [1024];
If (argc <2)
Error (1, 0, "parameters less than 2/N ");
Else if (argc = 2 ){
Pathname = argv [1];
}

S = server (pathname, "TCP ");
Signal (sigchld, sig_child );
Len = sizeof (struct sockaddr );

Fd_zero (& allset );
Fd_zero (& rset );
Fd_set (S, & allset );
Maxi = 0;
Maxfd = s;

For (I = 0; I <1024; I ++)
Client [I] =-1;
 
While (1 ){

Rset = allset;
 
If (nready = select (maxfd + 1, & rset, null) <0)
Error (1, 0, "select Error !! ");
If (fd_isset (S, & rset )){
If (C = accept (S, (struct sockaddr *) & remote, & Len) <= 0 ){
Error (1, errno, "Accept error! ");
}
For (I = 0; I <1024; I ++ ){
If (Client [I]! =-1)
Continue;
Else {
Client [I] = C;
Break;
}
}

If (I = 1024)
Error (1, 0, "Too reported clients !! ");
Fd_set (C, & allset );
If (C> maxfd)
Maxfd = C;
If (I> Maxi)
Maxi = I;
If (-- nready = 0)
Continue;
}
For (I = 0; I <= Maxi; I ++ ){
 
If (FD = client [I]) =-1)
Continue;
If (fd_isset (FD, & rset )){
If (LEN = Recv (FD, buff, sizeof (buff), 0)> 0 ){
Buff [Len] = '/0 ';
Send (FD, buff, strlen (buff), 0 );
Bzero (buff, sizeof (buff ));
}
Else {
Close (FD );
Fd_clr (FD, & allset );
Client [I] =-1;
}
If (-- nready = 0)
Break;
}
}
}
Close (s );
Exit (0 );
}

// Unixudpserver. c

# Include <stdarg. h>
# Include <stdio. h>
# Include <netdb. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <errno. h>
# Include <sys/UN. h>
Void process_server (INT s ){
Struct sockaddr_un remote;
Char buff [128];
Int Len, N;
While (1 ){
N = recvfrom (S, buff, sizeof (buff), 0, (struct sockaddr *) & remote, & Len );
// Printf ("% s, % s/n", buff, remote. sun_path );
Buff [N] = '/0 ';
Sendto (S, buff, strlen (buff), 0, (struct sockaddr *) & remote, sizeof (remote ));
}
}

Int main (INT argc, char ** argv ){
Struct sockaddr_un remote;
Char * pathname;
Int S, C, Len;
If (argc <2)
Error (1, 0, "parameters less than 2/N ");
Else if (argc = 2 ){
Pathname = argv [1];
}
S = server (pathname, "UDP ");
Process_server (s );
Close (s );
Exit (0 );
}

 

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.