Jump to content

problem : application with socket

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
katerina85

katerina85

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,I am trying to write a server-client application.
My code is

//server.c
main(int argc, char *argv[]){

	//setup socket
        // socket() - bind() -listen()

	while(1)
	{
		clientptr = (struct sockaddr *)&client;
		clientlen = sizeof client;

		while((newsock = accept(sock,clientptr,&clientlen)) < 0 );
		printf("\n\nNew connection.\n");

		switch(fork()){

			case -1 :
				printf("server fork failure %d\n",errno);
				perror("Server: ");
				exit(1); 

			case 0 : 
				do{

					read(newsock,buf,sizeof buf);

					if(strcmp(buf,"one")==0)
					{

						printf("\n one \n"); 
					}

				  }while(strcmp(buf,"quit")!=0)

	}//end while(1)

}

//client
main(int argc,char *argv[])
{
	//socket()-connect() ...

	do{
		menu(); //display choices
		choice = get_choice(); //return an integer

		switch(choice)
     		{
          		case 1: write(sock,buf,sizeof buf,"one"); 
			break;

                        case 2: write(sock,buf,sizeof buf,"quit"); 
			break;
		} 
	  }
//....
}


First time I press 1 so server prints one.
Then, the menu is appearing again, but when I press 1 server does not print anything.

Could anyone help me fix it ?
Thanks a lot

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What is the while condition on your do loop?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog