i want to write a sever application.my sever have some service to client. some of the answer of my server is long(transfer file) and some of them is short(list of online client).
when server transferring file to a client , other client request to get online client list. i want first job pause and answer the second then return first job.

and my server code is below:
what should i do?!

int runServer(char* pathDir, int portNum)
{
    int tcp_socket,maxSelect,activity,newSocket;
    struct sockaddr_in server_addr;
    int server_addr_length;

    tcp_socket = socket(AF_INET,SOCK_STREAM,0); // IPV4 , TCP , IP
    if ( tcp_socket==-1 )
    {
        char handle_error[] = "socket not created so application terminate, please run again.";
        write(1,handle_error,strlen(handle_error));
        return -1;
    }
    int opt=TRUE;
    if(setsockopt(tcp_socket,SOL_SOCKET,SO_REUSEADDR,(char*)&opt,sizeof(opt))<0)
    {
        char handle_error ="ERROR: problem in set socket option occurred\n";
        write(1,handle_error,strlen(handle_error));
    }
    server_addr.sin_family = AF_INET;                // family address ipv4
    server_addr.sin_port= htons(portNum);                    // port number
    server_addr.sin_addr.s_addr = INADDR_ANY;        // recieve message from all address

    int mybind = bind(tcp_socket,(struct  sockaddr*)&server_addr,sizeof(server_addr));
    if(mybind==-1)
    {
        char handle_error[] = " ERROR in binding\n";
        write(1,handle_error,strlen(handle_error));
        return 0;
    }
    listen(tcp_socket,MAX_USER);
    while(TRUE)
    {
        FD_ZERO(&readfd);//clear the socket set
        FD_SET(tcp_socket,&readfd);//add socket
        maxSelect=tcp_socket;

        // add child socket
        int k;
        for(k=0;k<MAX_USER;k++)
        {

            if(clientList[k].isThere==1)
            {
                FD_SET(clientList[k].userIdNumber,&readfd);
                if(clientList[k].userIdNumber>maxSelect)
                    maxSelect=clientList[k].userIdNumber;
            }
        }
        activity= select(maxSelect+1,&readfd,NULL,NULL,NULL);

        if(FD_ISSET(tcp_socket,&readfd))                                //request for connecting
        {
            function that answer conection request;
        }
        int y;
        for(y=0;y<MAX_USER;y++)
        {
            find who request and answer to him
        }
    }
}

When server gets a request from client the server should create another thread and process it there. The exact way to do that will depend on the compiler and operating system you are using. There is no standard way to create threads in C language. Now if you used c++ you could use os-independent functions to create threads.

One of the best texts on the subject is Comer and Stevens "Internetworking with TCP/IP". There are 3 or 4 volumes to the set, and they aren't cheap (running $30-100USD each these days - low price is for used volumes), but I refer to them regularly in my job as a systems engineer for a major network development organization.

This article has been dead for over six months. Start a new discussion instead.