Does read return anything if there is nothing more to read? Because I've currently been trying something like this:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#define SOCKET_ERROR -1
#define BUFFER_SIZE 100
#define HOST_NAME_SIZE 255
int main(int argc, char* argv[])
{
int hSocket; /* handle to socket */
struct hostent* pHostInfo; /* holds info about a machine */
struct sockaddr_in Address; /* Internet socket address stuct */
long nHostAddress;
char pBuffer[BUFFER_SIZE];
unsigned nReadAmount;
char strHostName[HOST_NAME_SIZE];
int nHostPort;
char buf[1024];
int n;
if(argc < 3)
{
printf("\nUsage: client host-name host-port\n");
return 0;
}
else
{
strcpy(strHostName,argv[1]);
nHostPort=atoi(argv[2]);
}
/* make a socket */
hSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(hSocket == SOCKET_ERROR)
{
printf("\nCould not make a socket\n");
return 0;
}
/* get IP address from name */
pHostInfo=gethostbyname(strHostName);
/* copy address into long */
memcpy(&nHostAddress,pHostInfo->h_addr,pHostInfo->h_length);
/* fill address struct */
Address.sin_addr.s_addr=nHostAddress;
Address.sin_port=htons(nHostPort);
Address.sin_family=AF_INET;
printf("Connecting to %s on port %d\n",strHostName,nHostPort);
/* connect to host */
if(connect(hSocket,(struct sockaddr*)&Address,sizeof(Address))
== SOCKET_ERROR)
{
printf("\nCould not connect to host\n");
return 0;
}
/* read from socket into buffer
** number returned by read() and write() is the number of bytes
** read or written, with -1 being that an error occured */
printf("Type a ~ to exit\n");
nReadAmount=1;
while(nReadAmount > 0)
{
nReadAmount=recv(hSocket,pBuffer,BUFFER_SIZE,0);
pBuffer[nReadAmount] = '\0';
if (pBuffer[nReadAmount]='\0') {
nReadAmount = 0;
break;
} else
{
printf("%s", pBuffer);
}
}
while(nReadAmount = 0)
{
printf("\n>");
gets(buf);
if(buf[0] == '~')
break;
n = strlen(buf);
buf[n] = '\n';
buf[n+1] = 0;
send(hSocket,buf, strlen(buf),0);
nReadAmount = 1;
break;
}
/* write what we received back to the server */
printf("\nClosing socket\n");
/* close socket */
if(close(hSocket) == SOCKET_ERROR)
{
printf("\nCould not close socket\n");
return 0;
}
}
There isn't any thing wrong with it that I can see, excpet for the fact that I think I'm not getting the:
if (pBuffer[nReadAmount]='\0') {
nReadAmount = 0;
part right. Ideas?