Implementing Linux Select Io multiplexing c/s server code

Source: Internet
Author: User
Tags socket error htons

Verified available under Ubuntu

Server-side

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/select.h>


#define MAXBUF 256
#define Maxlisten 8
#define PORT 8888


struct MSGTEMP
{
int num;
Char *s;
};

int main ()
{
int clen,dirnum,opt=1,i,nbyte;
int listenfd,clientfd,maxfd;
int client[fd_setsize];

struct sockaddr_in client_addr,server_addr;
Char Readbuf[maxbuf],writebuf[maxbuf];
struct Msgtemp msg[fd_setsize];
Fd_set RSet, Allset;

if ((Listenfd=socket (af_inet,sock_stream,0)) <0) {
Perror ("Socket Error:");
Exit (1);
}
Clen = sizeof (CLIENT_ADDR);
Bzero (&server_addr,0);
Server_addr.sin_family =af_inet;
Server_addr.sin_addr.s_addr=htonl (Inaddr_any);
Server_addr.sin_port=htons (port);

SetSockOpt (Listenfd,sol_socket,so_reuseaddr, (char*) &opt,sizeof (opt));
if (Bind (LISTENFD, (struct sockaddr *) &server_addr,sizeof (SERVER_ADDR)) <0) {
Perror ("bind error");
Exit (1);
}
if (Listen (Listenfd,maxlisten) <0) {
Perror ("Listen error:");
Exit (1);
}

MAXFD = LISTENFD;
for (i = 0; i < fd_setsize; i++)
Client[i] =-1;

Fd_zero (&allset);
Fd_set (LISTENFD, &allset);


while (1) {
RSet = Allset;
if (select (maxfd+1, &rset, NULL, NULL, NULL) <0)
{
Perror ("select");
return-1;
}
if (Fd_isset (LISTENFD, &rset)) {/* Is there a new connection coming in */
Clientfd=accept (LISTENFD, (struct sockaddr *) &client_addr,&clen);
if (Clientfd < 0)
{
Perror ("accept");
}
printf ("New Connection FD =%d\n", CLIENTFD);
Fd_set (CLIENTFD, &allset);
MAXFD = clientfd > maxfd? CLIENTFD:MAXFD;
for (i = 0; i < fd_setsize; i++)
if (Client[i] < 0) {
Client[i] = CLIENTFD;
Break
}
}
for (i = 0; i < fd_setsize; i++)
{
if (Fd_isset (Client[i], &rset)) {
if ((Nbyte = Read (Client[i], readbuf, maxbuf)) < 0) {
Perror ("read");
Continue

}
else if (Nbyte ==0)
{
Close (Client[i]);
FD_CLR (Client[i], &allset);
printf ("Connection fd =%d closed\n", client[i]);
Client[i] =-1;
Msg[i].num = 0;
MSG[I].S = NULL;

}
else{
printf ("recv msg from FD =%d:%s\n", client[i],readbuf);

MSG[I].S = Readbuf;
sprintf (Writebuf, "%03d:%s", MSG[I].NUM,MSG[I].S);
Write (Client[i], Writebuf, strlen (WRITEBUF) +1);
Msg[i].num + +;
}

}
}






}
return 0;
}

Client

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define MAXBUF 256
#define PORT 8888

int main ()
{
int ssock;
int clen,readbytes,fd2,i;
struct sockaddr_in server_addr;
Char Writebuf[maxbuf],readbuf[maxbuf],file_path[maxbuf];

if ((Ssock=socket (af_inet,sock_stream,0)) <0) {
Perror ("Socket Error:");
Exit (1);
}
Clen = sizeof (SERVER_ADDR);
Bzero (&server_addr,0);
Server_addr.sin_family =af_inet;
SERVER_ADDR.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1");
Server_addr.sin_port =htons (port);
if (Connect (ssock, (struct sockaddr *) &server_addr,clen) <0) {
Perror ("Connect error:");
Exit (1);
}



while (1)
{
printf ("Input msg:");
Fgets (Writebuf,maxbuf,stdin);
if (write (SSOCK,WRITEBUF,MAXBUF) <0)
{
Perror ("Write Error:");
}

Readbytes=read (SSOCK,READBUF,MAXBUF);
if (readbytes <0)
{

Perror ("Read error:");
Exit (1);
}
if (readbytes ==0)
{
printf ("Connection closed \ n");
Break
}


printf ("%s\n", readbuf);
}





Close (Ssock);

return 0;
}

Implementing Linux Select Io multiplexing c/s server code

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.