I wonder why my program run but not as I expected. I'm making client-server using fork() but the result is : server2: success, but not listening as I want it. I compile it with gcc -o server2 server2.c
here is the server side :
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h> // trap the child exits and prevent zombies
#include <signal.h> // trap the child exits and prevent zombies
#include <stdlib.h>
// signal handler calls waitpid()
void sigchld_handler(int signo)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
// declares variabel
int main(int argc, char *argv[])
{
struct sockaddr_in sAddr;
int listensock;
int newsock;
char buffer[25];
int result;
int nread;
int pid;
int val;
// create socket that will accept connection
listensock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// set our SO_REUSEADDR option
val = 1;
result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if(result < 0){
perror("server1 multiplexing");
return 0;
}
// bind to local port and all addresses associated with the machine
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(1972);
sAddr.sin_addr.s_addr = INADDR_ANY;
result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
if(result < 0){
perror("server2");
return 0;
}
// put the socket into listening mode for incoming connections
result = listen(listensock, 5);
if(result < 5){
perror("server2");
return 0;
}
// before start looping, we install our signal handler
signal(SIGCHLD, sigchld_handler);
// call accept() to block waiting for connection request from clients, after accept returns call fork() to create a new process
while(1){
newsock = accept(listensock, NULL, NULL);
if((pid = fork()) == 0){
// close listening socket, read char from client vice versa, close the socket and exit child process
printf("child process %i created.\n", getpid());
close(listensock);
nread = recv(newsock, buffer, 25, 0);
buffer[nread] = '\0';
printf("%s\n", buffer);
send(newsock, buffer, nread, 0);
close(newsock);
printf("child process %i finished.\n", getpid());
exit(0);
}else{
printf("child process error");
}
// parents process close
close(newsock);
}
}
and here is the client :
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
void child_func(int childnum);
// main function
int main(int argc, char *argv[])
{
int nchildren = 1;
int pid;
int x;
if (argc > 1){
nchildren = atoi(argv[1]);
}
for (x=0; x < nchildren; x++){
if((pid = fork() == 0)){
child_func(x+1);
exit(0);
}
}
wait(NULL);
return 0;
}
void child_func(int childnum){
int sock;
struct sockaddr_in sAddr;
char buffer[25];
// create client socket and bind it to local pprt
memset((void *) &sAddr, 0, sizeof(struct sockaddr_in));
sAddr.sin_family = AF_INET;
sAddr.sin_addr.s_addr = INADDR_ANY;
sAddr.sin_port = 0;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(sock, (const struct sockaddr *) &sAddr, sizeof(sAddr));
// Attempt to connect to whichever server is running on the local machine
sAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sAddr.sin_port = htons(1972);
if (connect(sock, (const struct sockaddr *) &sAddr, sizeof(sAddr)) != 0){
perror("client");
return;
}
// send some character to the server and insert sleep() function
snprintf(buffer, 128, "data from client #%i.", childnum);
sleep(1);
printf("child #%i sent %zu chars\n", childnum, send(sock, buffer, strlen(buffer), 0));
sleep(1);
printf("child #%i received %zu chars\n", childnum, recv(sock, buffer, 25, 0));
//close connection
sleep(1);
close(sock);
}
I think I know why you get the output
server2: success
and then why it exits.
It because of how you check for errors from the listen function. On error it returns -1 and on success it returns 0. Just like most other system calls. It does not return number you passed to it on success.
So the listen call succeeds and returns 0, but then you check for result < 5 and zero is indeed less than five, so you print the "error" and exit the program.
Related
I am writing echo server which have 1 parent process, and 3 child processes. The purpose of this server is...
Each child process made by the function fork() receives a string from each client.
the Parent process should concatenate 3 strings and send every child process.
every process have to send the string, concatenated by the parent process, to each client.
I have tried using functions fork, pipe, read, and write.
I made two pipes to communicate between children and parent. Each child process would use write function to pass a string. To receive the string concatenated by parent process, I write source code
After compiling and executing, i have found the logical error that only one client, first executed, receive concatenated string, two rest clients display message "Connection reset by peer".
I closed all pipes after using read and write functions. and i don't know how to manipulate this problem.
How can i make 3 clients can receive the string sent by the server correctly?
I am going to attach the source code.
Server Code
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#define MAXLINE 1024
#define PORTNUM 3600
int main(int argc, char **argv)
{
int p1[2];
int p2[2];
pipe(p1);
pipe(p2);
int index = 0;
//char channel[100];
char full_str[MAXLINE];
char recv[MAXLINE];
int isOK1 = 0;
int isOK2 = 0;
int listen_fd, client_fd;
pid_t pid;
socklen_t addrlen;
int readn;
char buf[MAXLINE];
struct sockaddr_in client_addr, server_addr;
if( (listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return 1;
}
memset((void *)&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORTNUM);
if(bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==-1)
{
perror("bind error");
return 1;
}
if(listen(listen_fd, 5) == -1)
{
perror("listen error");
return 1;
}
signal(SIGCHLD, SIG_IGN);
while(1)
{
addrlen = sizeof(client_addr);
client_fd = accept(listen_fd,
(struct sockaddr *)&client_addr, &addrlen);
if(client_fd == -1)
{
printf("accept error\n");
break;
}
pid = fork();
if(pid == 0)
{
close( listen_fd );
close(p1[0]);
close(p2[1]);
memset(buf, 0x00, MAXLINE);
while((readn = read(client_fd, buf, MAXLINE)) > 0)
{
printf("Read Data %s(%d) : %s\n", inet_ntoa(client_addr.sin_addr), client_addr.sin_port, buf);
write(p1[1], buf, strlen(buf));
close(p1[1]);
wait(NULL);
isOK2 = read(p2[0], recv, MAXLINE);
// When recv read concatenated string by p2 pipe, read function would be executed.
if(isOK2 > 0){
printf("%s I am over\n", recv);
write(client_fd, recv, MAXLINE);
close(p2[0]);
exit(1);
}
memset(buf, 0x00, MAXLINE);
}
close(client_fd);
return 0;
}
else if( pid > 0) {
close(p1[1]);
close(p2[0]);
char channel[100];
if((isOK1 = read(p1[0], channel, 100)>0)){
close(p1[0]);
strcat(full_str, channel);
index++;
full_str[strlen(full_str)] = '\0';
printf("%d \n", index);
printf("%s \n", channel);
printf("%ld \n", strlen(channel));
if(index >= 3) {
write(p2[1], full_str, strlen(full_str));
close(p2[1]);
exit(0);
}
memset(channel, 0x00, 100);
}
//wait(NULL);
}
close(client_fd);
}
return 0;
}
Client Code
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 1024
int main(int argc, char **argv)
{
struct sockaddr_in serveraddr;
int server_sockfd;
int client_len;
char buf[MAXLINE];
if ((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("error :");
return 1;
}
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serveraddr.sin_port = htons(3600);
client_len = sizeof(serveraddr);
if (connect(server_sockfd, (struct sockaddr *)&serveraddr, client_len) == -1)
{
perror("connect error :");
return 1;
}
memset(buf, 0x00, MAXLINE);
read(0, buf, MAXLINE);
buf[strlen(buf)-1] = '\0'; // trim text
if (write(server_sockfd, buf, MAXLINE) <= 0)
{
perror("write error : ");
return 1;
}
memset(buf, 0x00, MAXLINE);
if (read(server_sockfd, buf, MAXLINE) <= 0)
{
perror("read error : ");
return 1;
}
printf("read : %s\n", buf);
close(server_sockfd);
return 0;
}
You don't want to use 'pipe' and 'fork' for that.
You mentioned you have 'tried' to use these functions, which could mean, that you thought this would be a good approach, but no, it is not.
You should use 'select' or 'poll', in this way you can handle it in one single thread.
server
create server socket, bind and listen
accept until all clients connected
do poll/select for read (on client fd's) until all strings are read
concat strings
do poll/select for write (on client fd's) until string is written
close all
client
create socket
connect to server
do poll/select for read/write until string is written or read
close socket
If you want multithreading then use threads instead of 'fork'. The reason for that is, that you can share memory between parent and child processes and that eliminates the need of pipes (and additional read and writes).
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int port = 3008;
int listenfd;
extern void makelistener();
int main(int argc, char **argv)
{
makelistener();
int clientfd, nready;
socklen_t len;
struct sockaddr_in q;
int i;
// initialize allset and add listenfd to the
// set of file descriptors passed into select
fd_set allset;
fd_set rset;
int maxfd;
FD_ZERO(&allset);
FD_SET(listenfd, &allset); // set of file descriptors
maxfd = listenfd;
int ret;
while (1)
{
// make a copy of the set before we pass it into select
rset = allset;
/*select will wait until an exceptional event occurs when tv is NULL*/
nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
if (nready == 0) {
continue;
}
if (nready == -1) {
perror("select");
continue;
}
//FD_ISSET returns 1 when a new connection is attempted
if(FD_ISSET(listenfd, &rset)){
//printf("a new client is connecting\n");
len = sizeof(q); //accept connection of listenfd stream socket
if ((clientfd = accept(listenfd, (struct sockaddr *)&q, &len)) < 0) {
perror("accept");
exit(1);
}
FD_SET(clientfd, &allset);
if (clientfd > maxfd) {
maxfd = clientfd;
}
static char msg[] = "What is your name?\r\n";
write(clientfd, msg, sizeof msg - 1);
printf("connection from %s\n", inet_ntoa(q.sin_addr));
char buf[256];
ret = read(clientfd, buf, sizeof(buf));
buf[ret] = '\0';
printf("%s", buf);
}
}
}
void makelistener()
{
struct sockaddr_in r;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
memset(&r, '\0', sizeof r);
r.sin_family = AF_INET;
r.sin_addr.s_addr = INADDR_ANY;
r.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr *)&r, sizeof r)) {
perror("bind");
exit(1);
};
if (listen(listenfd, 5)) {
perror("listen");
exit(1);
}
}
above code is for the server and it does this
$ ./above.c
(does nothing but runs forever)
How to connect as a client:
$ nc 127.0.0.1 3000
What is your name?
(waiting for my input) so if I put bob, it would output it to the server
It works as intended. But I want it too work concurrently with multiple clients.
for example:
server
$ ./above.c
(does nothing but runs forever)
client1
$ nc 127.0.0.1 3000
What is your name?
client 2
$ nc 127.0.0.1 3000
What is your name? (Currently client2 wont show up until client1 is answered which is what I'm trying to fix)
How do I make it so the clients can run concurrently without waiting for the first client to finish? To explain the code a little bit, listener just binds and listens to a connection. Inside the while(1) is where select and calls are.
How do I make it so the clients can run concurrently without waiting for the first client to finish?
By paying attention to which sockets select() reports to you. You are asking select() to monitor multiple sockets for readability, but you are only checking if the listening socket is readable, you are not checking the client sockets. You need to keep track of the connected clients so you can enumerate them when needed.
Try something like this:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int port = 3008;
#define MAX_CLIENTS (FD_SETSIZE - 1)
int listenfd = -1;
extern void makelistener();
int main(int argc, char **argv)
{
int clientfd, nready;
socklen_t len;
struct sockaddr_in q;
int i, j, ret;
fd_set allset;
fd_set rset;
int clients[MAX_CLIENTS];
int num_clients = 0;
int maxfd;
char buf[256];
makelistener();
// initialize allset and add listenfd to the
// set of file descriptors passed into select
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
maxfd = listenfd;
while (1)
{
// make a copy of the set before we pass it into select
FD_COPY(&allset, &rset);
// select will wait until an exceptional event occurs when tv is NULL
nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
if (nready < 0) {
perror("select");
continue;
}
if (nready == 0) { // should never happen since no timeout was requested
continue;
}
//FD_ISSET returns 1 when a socket is readable
if (FD_ISSET(listenfd, &rset)) {
//printf("a new client is connecting\n");
len = sizeof(q); //accept connection of listenfd stream socket
if ((clientfd = accept(listenfd, (struct sockaddr *)&q, &len)) < 0) {
perror("accept");
exit(1);
}
printf("Client %d connected from %s\n", clientfd, inet_ntoa(q.sin_addr));
if (num_clients == MAX_CLIENTS) {
static char msg[] = "Max number of clients are already connected\r\n";
write(clientfd, msg, sizeof(msg)-1);
close(clientfd);
}
else {
static char msg[] = "What is your name?\r\n";
if (write(clientfd, msg, sizeof(msg)-1) < 0) {
close(clientfd);
}
else {
clients[num_clients++] = clientfd;
FD_SET(clientfd, &allset);
if (clientfd > maxfd) {
maxfd = clientfd;
}
}
}
}
for (i = 0; i < num_clients; ++i) {
clientfd = clients[i];
if (!FD_ISSET(clientfd, &rset)) {
continue;
}
ret = read(clientfd, buf, sizeof(buf));
if (ret <= 0) {
//printf("a client has disconnected\n");
close(clientfd);
FD_CLR(clientfd, &allset);
for (j = i + 1; j < num_clients; ++j) {
clients[j-1] = clients[j];
}
--num_clients;
if (clientfd == maxfd) {
maxfd = listenfd;
for (j = 0; j < num_clients; ++j) {
if (clients[j] > maxfd) {
maxfd = clients[j];
}
}
}
--i;
continue;
}
printf("Client %d: %.*s", clientfd, ret, buf);
}
}
return 0;
}
Note that poll() or epoll() would generally be a better choice to use than select().
How do I make it so the clients can run concurrently without waiting for the first client to finish?
every time that the call to accept() returns, start a thread pthread_create() to handle the actual communication with the client.
Note: creating/destroying a thread is very time consuming, so suggest learning about thread pools and how to use them.
When using threads, there is no call to select() (nor poll()) Rather the main function gets blocked on the call to accept() and when that function returns, pass the associated socket to a thread to handle
There are lots of example code for how a server should communicate with multiple clients on stackoverflow.com
I don't know whether the problem is with the client or with the server or both.This is my first client-server socket programming code. But this is not working as expected. The code which I referenced is working well although.
When the code runs, the client and server should both exchange 2 messages, but they are not doing so. The server is displaying "Listening" which is right as expected but when I run the client code, Nothing happens, It just displays nothing.
This is the client code
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
int main() {
struct sockaddr_in mysocket, servsocket;
int err;
char buf[256];
//CREATING SOCKET
int socketstatus = socket(AF_INET, SOCK_STREAM, 0);
printf("%d\n", socketstatus);
if(socketstatus < 0){
printf("socket failed\n");
scanf("%d", &err);
return 0;
}
bzero((char *) &mysocket, sizeof(mysocket));
mysocket.sin_family = AF_INET;
mysocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
mysocket.sin_port = htons(port);
//CONNECT
int connectstatus = connect(socketstatus, (struct sockaddr *) &servsocket, sizeof(servsocket));
if(connectstatus < 0){
printf("Connect failed\n");
scanf("%d", &err);
return 0;
}
//SEND
bzero(buf, 256);
strcpy(buf, "Message sent by client");
int sendstatus = send(socketstatus, buf, 256, 0);
printf("2\n"); //This is not being displayed
if(sendstatus < 0){
printf("Client send failed\n");
return 0;
}
printf("Reached for receiving\n");
//RECEIVE
bzero(buf, 256);
int recvstatus = recv(socketstatus, buf, 256, 0);
if(recvstatus < 0){
printf("Client RECEIVE failed\n");
scanf("%d", &err);
return 0;
}
printf("The message client got from server is, %s \n",buf );
scanf("%d", &err);
printf("Bye");
}
And this is the server code:
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main() {
int err;
struct sockaddr_in servsocket, clientsocket;
char sendmessage[256];
//CREATING SOCKET
int mysocket = socket(AF_INET, SOCK_STREAM, 0);
if(mysocket < 0){
printf("socket creation failed\n");
scanf("%d", &err);
return 0;
}
bzero((char*) &servsocket, sizeof(servsocket)); //initiazlizing servsocket with null
servsocket.sin_family = AF_INET;
servsocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
servsocket.sin_port = htons(port);
//BINDING
int bindstatus = bind(mysocket, (struct sockaddr*) &servsocket, sizeof(servsocket));
if(bindstatus < 0){
printf("Socket bind failed\n");
scanf("%d", &err);
return 1;
}
//LISTENING
int listenstatus = listen(mysocket, 5);
if(listenstatus < 0){
scanf("%d", &err);
return 2;
}
else
printf("LISTENING....\n");
//ACCEPTING
int clientsize = sizeof(clientsocket);
int acceptstatus = accept(mysocket, (struct sockaddr*) &clientsocket, &clientsize);
if(acceptstatus < 0){
printf("Accept failed");
scanf("%d", &err);
return 3;
}
char buf[256];
bzero(buf, 256);
//RECEIVING
int recvstatus = recv(acceptstatus, buf, 256, 0);
if(buf < 0){
printf("Error:Nothing read");
scanf("%d", &err);
return 4;
}
printf("I received this message, %s \n", buf);
printf("NOW I WILL SEND\n");
//SENDING
bzero(sendmessage, 256);
strcpy(sendmessage, "Message sent by server");
int sendstatus = send(acceptstatus, sendmessage, sizeof(sendmessage), 0);
if(sendstatus < 0){
printf("Error sending\n");
scanf("%d", &err);
return 5;
}
return 0;
}
In the client code, you initialize mysocket but pass serversocket to connect uninitialized.
You should be setting the fields of serversocket instead of mysocket.
You want to connect to the server socket inside your client (that would be serversocket in your code, not mysocket):
bzero((char *) &servsocket, sizeof(servsocket));
servsocket.sin_family = AF_INET;
servsocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
servsocket.sin_port = htons(port);
Then, I think you want your inet address to be 127.0.0.1 (what is typically default localhost address), not 127.0.0.2.
The code was "working" because you were passing a correct socket descriptor (socketstatus) to it is not correctly connected to the endpoint, so it fails on the send() call.
I have written a simple echo server and a client in C.
Here is the server code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include "unp.h"
#define SERVER_PORT 10000
void start_echo_service(int connfd);
void SIGCHLD_handler(int signum);
int main()
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in server_addr, client_addr;
pid_t child_pid;
printf("***Starting the echo server***\n");
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Failed to create connection socket. Exiting...\n");
exit(0);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
bzero(&client_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if(bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
{
strerror(errno);
exit(0);
}
if(listen(listenfd, 1000) < 0)
{
strerror(errno);
exit(0);
}
/* Add the handler for SIGCHLD */
struct sigaction sigchld_action;
sigchld_action.sa_handler = SIGCHLD_handler;
sigemptyset(&sigchld_action.sa_mask);
sigchld_action.sa_flags = 0;
if(sigaction(SIGCHLD, &sigchld_action, NULL) < 0)
{
printf("Error while adding handler for SIGCHLD\n");
exit(0);
}
while(1)
{
len = sizeof(client_addr);
if((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &len)) < 0)
{
if(errno == EINTR)
continue;
else
{
strerror(errno);
exit(0);
}
}
child_pid = fork();
if(child_pid < 0)
{
//some error occured
strerror(errno);
exit(0);
}
else if(child_pid == 0)
{
// child process
close(listenfd);
start_echo_service(connfd);
close(connfd);
exit(1);
}
close(connfd);
}
close(connfd);
return 1;
}
void SIGCHLD_handler(int signum)
{
pid_t pid;
if((pid = waitpid(-1, NULL, 0)) > 0)
printf("Harvested child (pid): %d\n", pid);
}
void start_echo_service(int connfd)
{
char buf[256];
int bytes_read;
while(1)
{
if((bytes_read = read(connfd, buf, sizeof(buf))) > 0)
{
writen(connfd, buf, bytes_read);
continue;
}
else if(bytes_read == 0)
break;
else
{
if(errno == EINTR)
continue;
else
{
printf("Read error\n");
break;
}
}
}
}
Here is the client code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "unp.h"
#define SERVER_PORT 10000
void start_echo_client(int connfd);
int main(int argc, char *argv[])
{
int connfd;
struct sockaddr_in server_addr;
if(argc != 2)
{
printf("Usage: echo_client <server-address>\n");
exit(-1);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &(server_addr.sin_addr));
server_addr.sin_port = htons(SERVER_PORT);
if((connfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
strerror(errno);
exit(0);
}
if(connect(connfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
{
strerror(errno);
exit(0);
}
start_echo_client(connfd);
return 1;
}
void start_echo_client(int connfd)
{
char buffer[256];
while(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
writen(connfd, buffer, strlen(buffer));
readn(connfd, buffer, strlen(buffer));
printf("%s", buffer);
}
}
I start my server with ./echo_server on one shell.
I start my client with ./echo_client 127.0.0.1 on another shell. So far, so good.
On my client, I type a message, say hello. The server echoes it back. Now, I terminate my server with Ctrl+C. The client is still running. Now, I type another message on the client, say zzz. I still get an echo back and zzz is printed on the shell. On typing another message on the client, it terminates.
I guess it may have something to do with the server being in uninterruptible sleep when I terminate it, but I can't be sure.
Here is the link to unp.h and unp.c
start_echo_client prints buffer irrespective of the return from readn, isn't? Even if server closed, buffer would contain contents of what happened with writen ( You use same buffer for write and read). Hence i think, you assume there was an echo from server while it was not.
The test with tcpdump as well indicates that FIN does arrive with ctrl-c on the server terminal. Client tries write ignoring the peer closure for which RST is received.
Also, read didn't yield zero on FIN. I changed to recv then the closure was caught.
Return the number of bytes read in the readn function instead what is being currently done and if its zero, then don't print buffer.
I've already read about how to prevent SIGPIPE, then I write a small program to test it. Here is the code.
server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void hdl(int sig_num, siginfo_t *sig_info, void *context)
{
printf("got you, SIGPIPE!\n");
}
int main()
{
int sfd, cfd;
struct sockaddr_in saddr, caddr;
struct sigaction act;
memset (&act, '\0', sizeof(act));
act.sa_sigaction = hdl;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGPIPE, &act, NULL) < 0) {
return 1;
}
sfd= socket(AF_INET, SOCK_STREAM, 0);
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=inet_addr("192.168.22.91");
saddr.sin_port=htons(12345);
if(bind(sfd, (struct sockaddr *)&saddr, sizeof(saddr)) )
{
printf("bind error\n");
return -1;
}
if(listen(sfd, 1))
{
printf("error\n");
return -1;
}
char buf[1024] = {0};
while(1) {
printf("Server listening...\n");
cfd=accept(sfd, (struct sockaddr *)NULL, NULL);
fcntl(cfd, F_SETFL, O_NONBLOCK);
int size = read(cfd, buf, 1024);
if(size == -1)
printf("read error\n");
sleep(2); // sleep for a while to make sure the client closed the socket
int ret;
if((ret = write(cfd, buf, strlen(buf)))<0)
{
if(errno == EPIPE)
fprintf(stderr, "SIGPIPE");
}
ret = write(cfd, buf, strlen(buf)); // write again.
printf("write return %d\n", ret);
}
close(sfd);
}
client.c
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <assert.h>
int main()
{
int ret, fd;
struct sockaddr_in sa_dst;
char buffer[] = "hello, world";
char rcv_buf[128] = {0};
fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&sa_dst, 0, sizeof(struct sockaddr_in));
sa_dst.sin_family = AF_INET;
sa_dst.sin_port = htons(12345);
sa_dst.sin_addr.s_addr = inet_addr("192.168.22.91");
ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr));
if(ret != -1)
{
send(fd, buffer, strlen(buffer), 0);
close(fd);
}
return 0;
}
When I run the server and the client on the same linux machine, on the server side, the first write() returns the number of bytes written while I expect a SIGPIPE signal because I closed the socket on the client side, the second write() does generate a SIGPIPE signal.
But when I ran the client on another linux machine or on a Windows machine(implement the same client with Winsock), I did't catch any SIGPIPE signal, and the second write() still returns the size of the buffer. Can someone tell me what's going on?
It can't happen on the first write, for two reasons:
The localhost doesn't know that the peer has closed the socket for reading. A FIN has been received but that could just be because the peer has shutdown for output. Only an RST will tell it that, and it doesn't get that util the next I/O at the earliest.
Buffering.
NB you're corrupting the value of errno by calling perror(), so testing it afterwards isn't valid.
Just Change this in SERVER and it will work
fcntl(cfd, F_SETFL, O_NONBLOCK);
int size = read(cfd, buf, 1024);
if(size == -1)
printf("read error\n");
sleep(2); // sleep for a while to make sure the client closed the socket
int ret;
ret = write(cfd, buf, strlen(buf));
sleep(2);
ret = write(cfd, buf, strlen(buf)); // write again.
printf("write return %d\n", ret);