This question uses the code for server.c and client.c found here.
I want a server to send a message to a client when a client has connected.
As you can see from the article, what the code does is have the server wait for a client to connect, listens to it, and then prints out the message the client gives it, and also an alert to the client.
Here’s what I’ve tried to do to solve my problem (everything else is exactly the same). server:
...
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
else
{
write(newsockfd,"I got your message",18);
}
....
And on client side, (again, everything else the same):
...
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
bzero(buffer,256);
read(sockfd,buffer,255);
...
It seems I should be able to do this with just these couple lines. But the behavior of the code remains exactly the same as the original! What’s going on?
For server, insert
after
accept().For client, insert
after
connect().Is this what you want? I think you should be able to infer from the rest of the existing code.