LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Blogs > XiaoMei
User Name
Password

Notices



Rate this Entry

server and client

Posted 12-03-2011 at 08:37 PM by XiaoMei

Hi, i be requested to sent out one server and three client, and i done my coding, but it didn't work, can somebody point out my mistake? thanks
server.c
#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h>

#define DEFAULT_PROTOCOL 0

/*****************************************************************/

main()

{
int serverFd, clientFd, serverLen, clientLen;
struct sockaddr_in serverAddress; /*server address*/
struct sockaddr_in clientAddress; /*client address*/
struct sockaddr* serverSockAddrPtr; /*Ptr to server address*/
struct sockaddr* clientSockAddrPtr; /*Ptr to client address*/

/*ignore death-of-child signals to prevent zombies*/
signal(SIGCHLD,SIG_IGN);

serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);

clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);

/*create a socket, bidirectional, default protocol*/
serverFd = socket(PF_INET,SOCK_STREAM,DEFAULT_PROTOCOL);
serverAddress.sun_family=PF_INET; /*set domain type*/
strcpy(serverAddress.sun_path,"recipe");/*set name*/
unlink("recipe");/*remove file if it already exists*/
bind(serverFd, serverSockAddrPtr, serverLen);
listen(serverFd, 5);/*maximum pending connection length*/

while(1)/*Loop forever*/
{
/*accept a client conection*/
clientFd = accept(serverFd,clientSockAddrPtr, &clientLen);

if(fork()==0)/*create a child to send recipe*/
{
writeRecipe(clientFd);/*send the recipe*/
close(clientFd);
exit(/*EXIT_SUCCESS*/0);
}
else
close(clientFd);/*close the client descriptor*/
}
}
/**************************************************************************/

writeRecipe (fd)

int fd;

{
static char* line1 = "spam, spam, spam, spam,";
static char* line2 = "spam, and spam.";
write(fd, line1, strlen(line1) + 1); /*write first line*/
write(fd, line2, strlen(line2) + 1); /*write second line*/
}

client.c
#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/un.h> /*for sockaddr_un struct*/

#define DEFAULT_PROTOCOL 0

/*****************************************************************/

main()

{
int clientFd, serverLen, result;
struct sockaddr_un serverAddress; /*server address*/
struct sockaddr* serverSockAddrPtr; /*Ptr to server address*/


serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);


/*create a socket, bidirectional, default protocol*/
clientFd = accept(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family=PF_INET; /*server domain*/
strcpy(serverAddress.sun_path,"recipe");/*server name*/

do/*loop until a connection is made with the server*/
{
result=connect(clientFd, serverSockAddrPtr, serverLen);
if(result==-1)sleep(1);
}
while(result==-1);

readRecipe (clientFd);/*Read the recipe*/
close(clientFd);/*close the socket*/
exit(/*Exit_success*/0);/*Done*/

}
/**************************************************************************/

readRecipe (fd)

int fd;

{
char str[200];

while(readline(fd,str))/*Read lines until end-of-input*/
printf("%s\n",str);/*Encho line from socket*/
}
/************************************************************************/

readLine(fd,str)

int fd;
char* str;

/*Read a single NULL-terminated line*/

{
int n;

do/*Read characters until NULL or end-of-input*/
{
n= read(fd,str,1);
}
while(n>0 && *str++ !=0);
return(n>0); /*return false if end-of-input*/
}
Posted in Uncategorized
Views 439 Comments 1
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment
    The connection is over the networking
    Posted 12-03-2011 at 08:39 PM by XiaoMei XiaoMei is offline
 

  



All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration