title: TCP server creating client socket
body: <p>I am trying to implement a socket programming for server that could also work as a client.</p>
  
  <p>The way that I am trying to do this is while running the server, if I put a command prompt argument "connect IP PORT", it would be connected to the corresponding address as a client.</p>
  
  <p>It does work however, it does not work concurrently.</p>
  
  <p>If I want to connect to a server before this program gets connected by a incoming client, it does not respond until it gets connected with the client.</p>
  
  <p>Here is my code for program.c:</p>
  
  <pre><code>#include "clientAPI.h"
  #include "serverAPI.h"
  
  int sfd;    //socket file descriptor
  int listenv;  //if listen
  struct sockaddr_in serverAddress;   //server address
  int connectv;
  
  
  int main(int argc, char * argv[]) { //argv[0] == server //argv[1] == port number
  
          char buffer[256];
          int connection;
          int csfd;
          int n;
          char a1[5], a2[5], a3[5];
          char m1[2], m2[10], m3[50];                 //command arg 1, command arg 2, arg command 3
          int * index;
          char * info;
  
          struct cEnQuery * ieq;
  
          ...
  
          listenv = setServer(argv[1],&amp;serverAddress,&amp;sfd);
  
          while (listenv == 0) {
              connectv = connector(&amp;sfd);
  
              //while connected
              bzero(buffer,256);
              fgets(buffer,256,stdin);
              sscanf(buffer,"%s %s %50[0-9a-zA-Z .]",a1,a2,a3);
  
              if (strcmp(a1,"connect") == 0) {
              //cip = atoi(m2); cport = atoi(m3);
              connection = connectToServer(&amp;csfd,a2,a3);
              if(connection == 1) {
                  printf("Please enter the message: "); //command prompt requests [send file/ get file / send struct]
                  bzero(buffer,256);
                  fgets(buffer,255,stdin);
  
                  //request message to server
                  n = write(csfd, buffer, strlen(buffer)); //send prompt request to server to take its actions
                  sscanf(buffer,"%s %s %50[0-9a-zA-Z .]",m1,m2,m3);
                  if(n &lt; 0) {
                      cerror("ERROR writing to socket");
                  }
                  else{
                      //connection stuff
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>clientAPI and serverAPI are just header files that include functions.</p>
  
  <p>Would there be any way for me to invoke clientAPI while listening to connections?</p>
  
  <p>setServer() handles binding of server to a host and listens for connections.</p>
  
  <p>connector() accepts the connection and creates a thread and handles the corresponding connection.</p>
  
  <p>connectToServer() creates a socket and connects to the available server.</p>