Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

server/client help

hey im a student and im trying to program using sockets to produce a server client system.
We have been given basic programs already and have been asked to manipulate them.
I am completely lost, here's the code ive been given:

[code]
#include /* for printf() and fprintf() */
#include /* for socket(), bind(), and connect() */
#include /* for sockaddr_in and inet_ntoa() */
#include /* for atoi() and exit() */
#include /* for memset() */
#include /* for close() */

#define RCVBUFSIZE 500 /* Size of receive buffer */
#define MAXPENDING 5 /* Maximum outstanding connection requests */

void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket); /* TCP client handling function */

int main(int argc, char *argv[])
{
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */

if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s
", argv[0]);
exit(1);
}

echoServPort = atoi(argv[1]); /* First arg: local port */

/* Create socket for incoming connections */
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");

/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed");

for (;;) /* Run forever */
{
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);

/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr,
&clntLen)) < 0)
DieWithError("accept() failed");

/* clntSock is connected to a client! */

printf("Handling client %s
", inet_ntoa(echoClntAddr.sin_addr));

HandleTCPClient(clntSock);
}
/* NOT REACHED */
}//-- end main --//



//
////
//
void DieWithError(char *errorMessage)
{
perror(errorMessage);
exit(1);
}



//
////
//
/* everthing to be changed is below*/

void HandleTCPClient(int clntSocket)
{
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
int recvMsgSize; /* Size of received message */

/* Receive message from client */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");

/******** test for status here***********/

/* got incoming request from client in the echobuffer */
recvMsgSize = sprintf(echoBuffer,
"MOTOR=0,TEMPS are 3A,42,FA,99");//reply!

/* Send received string and receive again until end of transmission */
while (recvMsgSize > 0) /* zero indicates end of transmission */
{
/* Echo message back to client */
if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
DieWithError("send() failed");

/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed");
}

close(clntSocket); /* Close client socket */
}

[/code]

i run the server with the following command
./S 1200
then run the client with this command
./C 192.168.0.164 status 1200

what i need to do is be able to test for "status", and when status is present then return the reply
but i have no idea how to test for status, i know where in the above code it is to go!

any help would be great
thanks

if you need the client code i can post this also....

Comments

  • engnieerengnieer Member Posts: 2
    hello
    I have project like you program can you post the client too i want see and which os you using and you using c or c++ ?

Sign In or Register to comment.