any one help me to solve this code??

this is client server socket program. I have to use in C program code. What I have to do.

  1. client connect to the server.

  2. than client will first prompt a welcome message that asks the user to enter a username using the keyboard. This username will then be sent to the server.

  3. than server, after receiving the username from client, it will send an acknowledgment message to the client.

  4. client, after receiving the acknowledgment message from server, it will prompt a message that asks the user to enter the corresponding password.

  5. This password will then be sent to the server. after receiving the password from client, will verify the received username and password. If the result is positive, the server will send a success message to the client. If the result is negative, the server will send a failure message to the client.

5.client, after receiving the result message, will print out the result and close the socket. server will close the socket following the client, and keep listening for the next client request.

This is the scenario of the program. kindly please help me to solve this program.

Here I can connnect client and server but the rest of work I can not. My code is below.

Client code is

Code:

#include	"myfile.h"

int
main(int argc, char **argv)
{
int	 sockfd, n;
char	 recvline[MAXLINE + 1];
struct sockaddr_in	servaddr;

if (argc != 2) {
perror("usage: a.out <IPaddress>");
exit (1);
}


if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);	/* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("inet_pton error for %s", argv[1]);
exit(0);
}

if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
perror("connect error");
exit(1);
}

while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = 0;	/* null terminate */
if (fputs(recvline, stdout) == EOF) {
perror("fputs error");
exit(1);
}
}
if (n < 0) {
perror("read error");
}

exit(0);
}

Server code is


Code:
#include	"myfile.h"
#include	<time.h>

int
main(int argc, char **argv)
{
int	 listenfd, connfd;
struct sockaddr_in	servaddr;
char	 buff[MAXLINE];
time_t	 ticks;

listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
exit(0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);	/* daytime server */

if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
exit(0);

if( listen(listenfd, LISTENQ) <0)
exit(0);

for ( ; ; ) {
connfd = accept(listenfd, (struct sockaddr *) NULL, NULL);
if (connfd<0) {
perror("connection failure");
continue;
}

ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
if( write(connfd, buff, strlen(buff)) < 0) {
perror("error in writing");
}

close(connfd);
}
}

It looks to me like you copied code from somewhere. Probably a BSD or SCO code site.

Anyway: socket programming 101:

Beej's Guide to Network Programming

This explains how to do everything you need - what you asked for help with.

Also, while you're at it, re-read the homework rules.