Click to See Complete Forum and Search --> : Basic client server help needed


godonholiday
December 3rd, 2004, 08:40 AM
I have written a basic client server program (2 programs), and all it displays is the words hello world. the files are bellow.

what i am trying to do is this:

once the connection is astablished the user is asked for their user name, if they are reg then they get access to some information, a little text file, if they are not reg then they do not.

can anyone help me with this.
i am programming in c on linux.

thanks in advance
g

/* An example client for "Hello, World!" server*/


#include <stdio.h> /* perror() */
#include <stdlib.h> /* atoi() */
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> /* read() */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
int clientSocket,
remotePort,
status = 0;
struct hostent *hostPtr = NULL;
struct sockaddr_in serverName = { 0 };
char buffer[256] = "";
char *remoteHost = NULL;

if (3 != argc)
{
fprintf(stderr, "Usage: %s <serverHost> <serverPort>\n",
argv[0]);
exit(1);
}

remoteHost = argv[1];
remotePort = atoi(argv[2]);

clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == clientSocket)
{
perror("socket()");
exit(1);
}

/*
* need to resolve the remote server name or IP address
*/
hostPtr = gethostbyname(remoteHost);
if (NULL == hostPtr)
{
hostPtr = gethostbyaddr(remoteHost, strlen(remoteHost),
AF_INET);
if (NULL == hostPtr)
{
perror("Error resolving server address ");
exit(1);
}
}

serverName.sin_family = AF_INET;
serverName.sin_port = htons(remotePort);
(void) memcpy(&serverName.sin_addr, hostPtr->h_addr,
hostPtr->h_length);

status = connect(clientSocket, (struct sockaddr*) &serverName,
sizeof(serverName));
if (-1 == status)
{
perror("connect()");
exit(1);
}

/*
* Client application specific code goes here
*
* e.g. receive messages from server, respond, etc.
*/
while (0 < (status = read(clientSocket, buffer,
sizeof(buffer) - 1)))
{
printf("%d: %s", status, buffer);
}

if (-1 == status)
{
perror("read()");
}

close(clientSocket);

return 0;
}
=================================


/* An example client for "Hello, World!" server*/


#include <stdio.h> /* perror() */
#include <stdlib.h> /* atoi() */
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> /* read() */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
int clientSocket,
remotePort,
status = 0;
struct hostent *hostPtr = NULL;
struct sockaddr_in serverName = { 0 };
char buffer[256] = "";
char *remoteHost = NULL;

if (3 != argc)
{
fprintf(stderr, "Usage: %s <serverHost> <serverPort>\n",
argv[0]);
exit(1);
}

remoteHost = argv[1];
remotePort = atoi(argv[2]);

clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == clientSocket)
{
perror("socket()");
exit(1);
}

/*
* need to resolve the remote server name or IP address
*/
hostPtr = gethostbyname(remoteHost);
if (NULL == hostPtr)
{
hostPtr = gethostbyaddr(remoteHost, strlen(remoteHost),
AF_INET);
if (NULL == hostPtr)
{
perror("Error resolving server address ");
exit(1);
}
}

serverName.sin_family = AF_INET;
serverName.sin_port = htons(remotePort);
(void) memcpy(&serverName.sin_addr, hostPtr->h_addr,
hostPtr->h_length);

status = connect(clientSocket, (struct sockaddr*) &serverName,
sizeof(serverName));
if (-1 == status)
{
perror("connect()");
exit(1);
}

/*
* Client application specific code goes here
*
* e.g. receive messages from server, respond, etc.
*/
while (0 < (status = read(clientSocket, buffer,
sizeof(buffer) - 1)))
{
printf("%d: %s", status, buffer);
}

if (-1 == status)
{
perror("read()");
}

close(clientSocket);

return 0;
}

drewdaman
December 3rd, 2004, 09:39 AM
well.. all you have to do is send and receive... to start with, instead of saying hello world, send a message asking for the username.. then have the client send it and verify it.. then send the text file.. what exactly is the problem? look into send and recv.

godonholiday
December 3rd, 2004, 09:45 AM
iv tried that, i dont know how to keep the client open,.

i know how to send the message but not how to ask them to enter some data and then store it.

any help would be great thanks
(another novice)