C++ Regarding Socket Programing - Client Side
<p>I have a issue with my socket programing.</p>

<p>I not sure why my client not receiving anything from my server, when I run Client, it just stay at the blank screen.. not ending or anything, press enter only result more space, only way is to terminate by cltrl+c , did i miss anything in my client code.</p>

<p>I am trying to connect to my server through localhost, linux.</p>

<p>My ps:</p>

<pre><code>  PID TTY          TIME CMD
 2575 pts/0    00:00:00 su
 2583 pts/0    00:00:00 bash
 5811 pts/0    00:00:00 nano
 8105 pts/0    00:00:00 CServer
 8119 pts/0    00:00:00 ps
</code></pre>

<p>My Output when running ./Client</p>

<p>root@ubuntu:/home/baoky/shell_assn2# ./CClient</p>

<p>Client..</p>

<p>My Client Code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/un.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;

#define DEFAULT_PROTOCOL 0
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif 

using namespace std;

int readLine(int fd, char* str)
{
    int j;
    do 
        {
            j = read (fd,str, 1); //Read Char 1 by 1
        }while (j &gt; 0 &amp;&amp; *str++ != 0);

    return (j &gt; 0);
}

void readServer(int fd) 
{
    char str[1000];

    while (readLine (fd, str)) //while readLine
    {
        cout &lt;&lt; str &lt;&lt; endl;
        return;
    }
}

int main()
{
cout &lt;&lt; "" &lt;&lt; endl;
cout &lt;&lt; "Client.." &lt;&lt; endl;
cout &lt;&lt; "" &lt;&lt; endl;
 int clientFd, serverLen, result;
   struct sockaddr_un serverAddress;
   struct sockaddr* serverSockAddrPtr;
   serverSockAddrPtr = (struct sockaddr*) &amp;serverAddress;
   serverLen = sizeof (serverAddress);

   /* Create a socket, bidirectional, default protocol */
   clientFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
   serverAddress.sun_family = AF_LOCAL; /* Server domain */
   strcpy (serverAddress.sun_path, "recipe"); /* Server name */
   do /* Loop until a connection is made with the server */
     {
       result = connect (clientFd, serverSockAddrPtr, serverLen);
       if (result == -1) sleep (1); /* Wait and then try again */
     }
   while (result == -1);
   readServer (clientFd); /* Read the recipe */
   close (clientFd); /* Close the socket */
   exit (/* EXIT_SUCCESS */ 0); /* Done */

return 0;
}
</code></pre>
