Ads Here

Tuesday, March 26, 2019

TCP Concurrent Server



TCP Concurrent Server
/*ConcurrentServer.c: This is concurrent server which handles the multiple client requests at a time, this server always run, it doesn't end...*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "pthread.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "pthread.h"
void* ThreadFunction(int*);
int main(int argc, char* argv[])
{
    int SockFD, NewSockFD, ClientLen;
    pthread_t ThreadId;
    struct sockaddr_in ServerAddress, ClientAddress;
    if(argc < 3)
    {   printf("Usage is:%s <IP-Address> <Port> \n", argv[0]);    exit(0);     }
   
    SockFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);   
    perror("socket() ");

    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_port = htons(atoi(argv[2]));
    ServerAddress.sin_addr.s_addr = inet_addr(argv[1]);

    bind(SockFD, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress));   
    perror("bind() ");

    listen(SockFD, 1);
    perror("listen() ");   

    ClientLen = sizeof(ClientAddress);
    while(1)
    {
        NewSockFD = accept(SockFD, (struct sockaddr*)&ClientAddress, &ClientLen);
        perror("accept() ");
        if(NewSockFD > 0)
        {
            pthread_create(&ThreadId, NULL, (void*)ThreadFunction, &NewSockFD);
            perror("pthread_create()");
        }
        else
        {
            printf("accept() is failed \n");
        }
    }

}/*End of the main() functionality*/

void* ThreadFunction(int* FD)
{
    char RecvBuff[50];
    char SendBuff[50]="I am a server";
    int NumOfBytesRead, NumOfBytesSent;
    printf("Thread Created for one client of FD %d \n", *FD);
#if 1
    NumOfBytesRead = recv(*FD, RecvBuff, 50, 0);   
    perror("recv()");
   
    if(NumOfBytesRead == 0)
    {
        printf("peer/client of FD(%d) has been closed.\n", *FD);   
        close(*FD);   
        pthread_exit(NULL);
    }
    else if(NumOfBytesRead > 0)
    {
        printf("Received is: %s\n", RecvBuff);
        NumOfBytesSent = send(*FD, SendBuff, strlen(SendBuff), 0);   
    }
#endif
}

/*Client.c:: */
#include "stdio.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "netinet/in.h"
int main(int argc, char* argv[])
{
    int SockFd, NewSockFd, ClientLen;
    struct sockaddr_in ServerAddress;
    int sockopt = 1;
    char SendBuff[50]="I AM A CLIENT", RecvBuff[50];
    int SentBytes, RecvedBytes;
    if(argc < 3)
    { 
   printf("Usage is:%s <Server-IPAddress> <PortNo>\n", argv[0]);   exit(0);     }
    SockFd = socket(AF_INET, SOCK_STREAM, 0);   perror("\nsocket()");
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_port = htons(atoi(argv[2]));
    ServerAddress.sin_addr.s_addr = inet_addr(argv[1]);
    connect(SockFd, (struct sockaddr*)&ServerAddress, sizeof(ServerAddress));   
        perror("\nconnect()");

    while(1)
    {
    sleep(2);
    SentBytes = send(SockFd, SendBuff, sizeof(SendBuff), 0);
    if(SentBytes == -1)
    {      perror("\nsend()");        close(SockFd);       break;    }

    RecvedBytes = recv(SockFd, RecvBuff, 50, 0);   
    if((RecvedBytes == 0)||(RecvedBytes == -1))
    {    
perror("\nrecv()");       
close(SockFd);          
break;        }   
    else
        printf("%s\n", RecvBuff);
    }
}

No comments:

Post a Comment